1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caxy\HtmlDiff; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class HtmlDiffConfig. |
7
|
|
|
*/ |
8
|
|
|
class HtmlDiffConfig |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $specialCaseChars = array('.', ',', '(', ')', '\''); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $groupDiffs = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $insertSpaceInReplace = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Whether to keep newlines in the diff |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $keepNewLines = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $encoding = 'UTF-8'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $isolatedDiffTags = array( |
45
|
|
|
'ol' => '[[REPLACE_ORDERED_LIST]]', |
46
|
|
|
'ul' => '[[REPLACE_UNORDERED_LIST]]', |
47
|
|
|
'sub' => '[[REPLACE_SUB_SCRIPT]]', |
48
|
|
|
'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
49
|
|
|
'dl' => '[[REPLACE_DEFINITION_LIST]]', |
50
|
|
|
'table' => '[[REPLACE_TABLE]]', |
51
|
|
|
'strong' => '[[REPLACE_STRONG]]', |
52
|
|
|
'b' => '[[REPLACE_STRONG]]', |
53
|
|
|
'em' => '[[REPLACE_EM]]', |
54
|
|
|
'i' => '[[REPLACE_EM]]', |
55
|
|
|
'a' => '[[REPLACE_A]]', |
56
|
|
|
'img' => '[[REPLACE_IMG]]', |
57
|
|
|
'pre' => '[[REPLACE_PRE]]', |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
|
|
protected $matchThreshold = 80; |
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $specialCaseOpeningTags = array(); |
68
|
|
|
/** |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $specialCaseClosingTags = array(); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var bool |
75
|
|
|
*/ |
76
|
|
|
protected $useTableDiffing = true; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var null|\Doctrine\Common\Cache\Cache |
80
|
|
|
*/ |
81
|
|
|
protected $cacheProvider; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var bool |
85
|
|
|
*/ |
86
|
|
|
protected $purifierEnabled = true; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var null|string |
90
|
|
|
*/ |
91
|
|
|
protected $purifierCacheLocation = null; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return HtmlDiffConfig |
95
|
|
|
*/ |
96
|
16 |
|
public static function create() |
97
|
|
|
{ |
98
|
16 |
|
return new self(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* HtmlDiffConfig constructor. |
103
|
|
|
*/ |
104
|
16 |
|
public function __construct() |
105
|
|
|
{ |
106
|
16 |
|
$this->setSpecialCaseTags($this->specialCaseTags); |
107
|
16 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return int |
111
|
|
|
*/ |
112
|
8 |
|
public function getMatchThreshold() |
113
|
|
|
{ |
114
|
8 |
|
return $this->matchThreshold; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param int $matchThreshold |
119
|
|
|
* |
120
|
|
|
* @return AbstractDiff |
|
|
|
|
121
|
|
|
*/ |
122
|
|
|
public function setMatchThreshold($matchThreshold) |
123
|
|
|
{ |
124
|
|
|
$this->matchThreshold = $matchThreshold; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $chars |
131
|
|
|
*/ |
132
|
|
|
public function setSpecialCaseChars(array $chars) |
133
|
|
|
{ |
134
|
|
|
$this->specialCaseChars = $chars; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array|null |
139
|
|
|
*/ |
140
|
16 |
|
public function getSpecialCaseChars() |
141
|
|
|
{ |
142
|
16 |
|
return $this->specialCaseChars; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $char |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function addSpecialCaseChar($char) |
151
|
|
|
{ |
152
|
|
|
if (!in_array($char, $this->specialCaseChars)) { |
153
|
|
|
$this->specialCaseChars[] = $char; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $char |
161
|
|
|
* |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function removeSpecialCaseChar($char) |
165
|
|
|
{ |
166
|
|
|
$key = array_search($char, $this->specialCaseChars); |
167
|
|
|
if ($key !== false) { |
168
|
|
|
unset($this->specialCaseChars[$key]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array $tags |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
16 |
|
public function setSpecialCaseTags(array $tags = array()) |
180
|
|
|
{ |
181
|
16 |
|
$this->specialCaseTags = $tags; |
182
|
16 |
|
$this->specialCaseOpeningTags = array(); |
183
|
16 |
|
$this->specialCaseClosingTags = array(); |
184
|
|
|
|
185
|
16 |
|
foreach ($this->specialCaseTags as $tag) { |
186
|
16 |
|
$this->addSpecialCaseTag($tag); |
187
|
|
|
} |
188
|
|
|
|
189
|
16 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $tag |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
16 |
|
public function addSpecialCaseTag($tag) |
198
|
|
|
{ |
199
|
16 |
|
if (!in_array($tag, $this->specialCaseTags)) { |
200
|
|
|
$this->specialCaseTags[] = $tag; |
201
|
|
|
} |
202
|
|
|
|
203
|
16 |
|
$opening = $this->getOpeningTag($tag); |
204
|
16 |
|
$closing = $this->getClosingTag($tag); |
205
|
|
|
|
206
|
16 |
|
if (!in_array($opening, $this->specialCaseOpeningTags)) { |
207
|
16 |
|
$this->specialCaseOpeningTags[] = $opening; |
208
|
|
|
} |
209
|
16 |
|
if (!in_array($closing, $this->specialCaseClosingTags)) { |
210
|
16 |
|
$this->specialCaseClosingTags[] = $closing; |
211
|
|
|
} |
212
|
|
|
|
213
|
16 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $tag |
218
|
|
|
* |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
|
|
public function removeSpecialCaseTag($tag) |
222
|
|
|
{ |
223
|
|
|
if (($key = array_search($tag, $this->specialCaseTags)) !== false) { |
224
|
|
|
unset($this->specialCaseTags[$key]); |
225
|
|
|
|
226
|
|
|
$opening = $this->getOpeningTag($tag); |
227
|
|
|
$closing = $this->getClosingTag($tag); |
228
|
|
|
|
229
|
|
|
if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) { |
230
|
|
|
unset($this->specialCaseOpeningTags[$key]); |
231
|
|
|
} |
232
|
|
|
if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) { |
233
|
|
|
unset($this->specialCaseClosingTags[$key]); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return array|null |
242
|
|
|
*/ |
243
|
|
|
public function getSpecialCaseTags() |
244
|
|
|
{ |
245
|
|
|
return $this->specialCaseTags; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
16 |
|
public function isGroupDiffs() |
252
|
|
|
{ |
253
|
16 |
|
return $this->groupDiffs; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param bool $groupDiffs |
258
|
|
|
* |
259
|
|
|
* @return HtmlDiffConfig |
260
|
|
|
*/ |
261
|
|
|
public function setGroupDiffs($groupDiffs) |
262
|
|
|
{ |
263
|
|
|
$this->groupDiffs = $groupDiffs; |
264
|
|
|
|
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
1 |
|
public function getEncoding() |
272
|
|
|
{ |
273
|
1 |
|
return $this->encoding; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param string $encoding |
278
|
|
|
* |
279
|
|
|
* @return HtmlDiffConfig |
280
|
|
|
*/ |
281
|
16 |
|
public function setEncoding($encoding) |
282
|
|
|
{ |
283
|
16 |
|
$this->encoding = $encoding; |
284
|
|
|
|
285
|
16 |
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function isInsertSpaceInReplace() |
292
|
|
|
{ |
293
|
|
|
return $this->insertSpaceInReplace; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param bool $insertSpaceInReplace |
298
|
|
|
* |
299
|
|
|
* @return HtmlDiffConfig |
300
|
|
|
*/ |
301
|
|
|
public function setInsertSpaceInReplace($insertSpaceInReplace) |
302
|
|
|
{ |
303
|
|
|
$this->insertSpaceInReplace = $insertSpaceInReplace; |
304
|
|
|
|
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @return bool |
310
|
|
|
*/ |
311
|
16 |
|
public function isKeepNewLines() |
312
|
|
|
{ |
313
|
16 |
|
return $this->keepNewLines; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param bool $keepNewLines |
318
|
|
|
*/ |
319
|
|
|
public function setKeepNewLines($keepNewLines) |
320
|
|
|
{ |
321
|
|
|
$this->keepNewLines = $keepNewLines; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return array |
326
|
|
|
*/ |
327
|
16 |
|
public function getIsolatedDiffTags() |
328
|
|
|
{ |
329
|
16 |
|
return $this->isolatedDiffTags; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param array $isolatedDiffTags |
334
|
|
|
* |
335
|
|
|
* @return HtmlDiffConfig |
336
|
|
|
*/ |
337
|
|
|
public function setIsolatedDiffTags($isolatedDiffTags) |
338
|
|
|
{ |
339
|
|
|
$this->isolatedDiffTags = $isolatedDiffTags; |
340
|
|
|
|
341
|
|
|
return $this; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @param string $tag |
346
|
|
|
* @param null|string $placeholder |
347
|
|
|
* |
348
|
|
|
* @return $this |
349
|
|
|
*/ |
350
|
|
|
public function addIsolatedDiffTag($tag, $placeholder = null) |
351
|
|
|
{ |
352
|
|
|
if (null === $placeholder) { |
353
|
|
|
$placeholder = sprintf('[[REPLACE_%s]]', mb_strtoupper($tag)); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if ($this->isIsolatedDiffTag($tag) && $this->isolatedDiffTags[$tag] !== $placeholder) { |
357
|
|
|
throw new \InvalidArgumentException( |
358
|
|
|
sprintf('Isolated diff tag "%s" already exists using a different placeholder', $tag) |
359
|
|
|
); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
$matchingKey = array_search($placeholder, $this->isolatedDiffTags, true); |
363
|
|
|
if (false !== $matchingKey && $matchingKey !== $tag) { |
364
|
|
|
throw new \InvalidArgumentException( |
365
|
|
|
sprintf('Placeholder already being used for a different tag "%s"', $tag) |
366
|
|
|
); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
if (!array_key_exists($tag, $this->isolatedDiffTags)) { |
370
|
|
|
$this->isolatedDiffTags[$tag] = $placeholder; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param string $tag |
378
|
|
|
* |
379
|
|
|
* @return $this |
380
|
|
|
*/ |
381
|
|
|
public function removeIsolatedDiffTag($tag) |
382
|
|
|
{ |
383
|
|
|
if ($this->isIsolatedDiffTag($tag)) { |
384
|
|
|
unset($this->isolatedDiffTags[$tag]); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
return $this; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @param string $tag |
392
|
|
|
* |
393
|
|
|
* @return bool |
394
|
|
|
*/ |
395
|
14 |
|
public function isIsolatedDiffTag($tag) |
396
|
|
|
{ |
397
|
14 |
|
return array_key_exists($tag, $this->isolatedDiffTags); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @param string $text |
402
|
|
|
* |
403
|
|
|
* @return bool |
404
|
|
|
*/ |
405
|
16 |
|
public function isIsolatedDiffTagPlaceholder($text) |
406
|
|
|
{ |
407
|
16 |
|
return in_array($text, $this->isolatedDiffTags, true); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param string $tag |
412
|
|
|
* |
413
|
|
|
* @return null|string |
414
|
|
|
*/ |
415
|
14 |
|
public function getIsolatedDiffTagPlaceholder($tag) |
416
|
|
|
{ |
417
|
14 |
|
return $this->isIsolatedDiffTag($tag) ? $this->isolatedDiffTags[$tag] : null; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @return array |
422
|
|
|
*/ |
423
|
6 |
|
public function getSpecialCaseOpeningTags() |
424
|
|
|
{ |
425
|
6 |
|
return $this->specialCaseOpeningTags; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @return array |
430
|
|
|
*/ |
431
|
6 |
|
public function getSpecialCaseClosingTags() |
432
|
|
|
{ |
433
|
6 |
|
return $this->specialCaseClosingTags; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @return bool |
438
|
|
|
*/ |
439
|
10 |
|
public function isUseTableDiffing() |
440
|
|
|
{ |
441
|
10 |
|
return $this->useTableDiffing; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @param bool $useTableDiffing |
446
|
|
|
* |
447
|
|
|
* @return HtmlDiffConfig |
448
|
|
|
*/ |
449
|
|
|
public function setUseTableDiffing($useTableDiffing) |
450
|
|
|
{ |
451
|
|
|
$this->useTableDiffing = $useTableDiffing; |
452
|
|
|
|
453
|
|
|
return $this; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
458
|
|
|
* |
459
|
|
|
* @return $this |
460
|
|
|
*/ |
461
|
|
|
public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
462
|
|
|
{ |
463
|
|
|
$this->cacheProvider = $cacheProvider; |
464
|
|
|
|
465
|
|
|
return $this; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @return null|\Doctrine\Common\Cache\Cache |
470
|
|
|
*/ |
471
|
16 |
|
public function getCacheProvider() |
472
|
|
|
{ |
473
|
16 |
|
return $this->cacheProvider; |
474
|
|
|
} |
475
|
|
|
|
476
|
16 |
|
public function isPurifierEnabled(): bool |
477
|
|
|
{ |
478
|
16 |
|
return $this->purifierEnabled; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
public function setPurifierEnabled(bool $purifierEnabled = true): self |
482
|
|
|
{ |
483
|
|
|
$this->purifierEnabled = $purifierEnabled; |
484
|
|
|
|
485
|
|
|
return $this; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @param null|string |
490
|
|
|
* |
491
|
|
|
* @return $this |
492
|
|
|
*/ |
493
|
2 |
|
public function setPurifierCacheLocation($purifierCacheLocation = null) |
494
|
|
|
{ |
495
|
2 |
|
$this->purifierCacheLocation = $purifierCacheLocation; |
496
|
|
|
|
497
|
2 |
|
return $this; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* @return null|string |
502
|
|
|
*/ |
503
|
16 |
|
public function getPurifierCacheLocation() |
504
|
|
|
{ |
505
|
16 |
|
return $this->purifierCacheLocation; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* @param string $tag |
510
|
|
|
* |
511
|
|
|
* @return string |
512
|
|
|
*/ |
513
|
16 |
|
protected function getOpeningTag($tag) |
514
|
|
|
{ |
515
|
16 |
|
return '/<'.$tag.'[^>]*/i'; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @param string $tag |
520
|
|
|
* |
521
|
|
|
* @return string |
522
|
|
|
*/ |
523
|
16 |
|
protected function getClosingTag($tag) |
524
|
|
|
{ |
525
|
16 |
|
return '</'.$tag.'>'; |
526
|
|
|
} |
527
|
|
|
} |
528
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.