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 string[] |
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
|
17 |
|
public static function create() |
97
|
|
|
{ |
98
|
17 |
|
return new self(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* HtmlDiffConfig constructor. |
103
|
|
|
*/ |
104
|
17 |
|
public function __construct() |
105
|
|
|
{ |
106
|
17 |
|
$this->setSpecialCaseTags($this->specialCaseTags); |
107
|
17 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return int |
111
|
|
|
*/ |
112
|
9 |
|
public function getMatchThreshold() |
113
|
|
|
{ |
114
|
9 |
|
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
|
|
|
public function setSpecialCaseChars(array $chars) |
130
|
|
|
{ |
131
|
|
|
$this->specialCaseChars = $chars; |
132
|
|
|
} |
133
|
|
|
|
134
|
17 |
|
public function getSpecialCaseChars() : array |
135
|
|
|
{ |
136
|
17 |
|
return $this->specialCaseChars; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $char |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function addSpecialCaseChar($char) |
145
|
|
|
{ |
146
|
|
|
if (!in_array($char, $this->specialCaseChars)) { |
147
|
|
|
$this->specialCaseChars[] = $char; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $char |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function removeSpecialCaseChar($char) |
159
|
|
|
{ |
160
|
|
|
$key = array_search($char, $this->specialCaseChars); |
161
|
|
|
if ($key !== false) { |
162
|
|
|
unset($this->specialCaseChars[$key]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param array $tags |
170
|
|
|
* |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
17 |
|
public function setSpecialCaseTags(array $tags = array()) |
174
|
|
|
{ |
175
|
17 |
|
$this->specialCaseTags = $tags; |
176
|
17 |
|
$this->specialCaseOpeningTags = array(); |
177
|
17 |
|
$this->specialCaseClosingTags = array(); |
178
|
|
|
|
179
|
17 |
|
foreach ($this->specialCaseTags as $tag) { |
180
|
17 |
|
$this->addSpecialCaseTag($tag); |
181
|
|
|
} |
182
|
|
|
|
183
|
17 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $tag |
188
|
|
|
* |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
17 |
|
public function addSpecialCaseTag($tag) |
192
|
|
|
{ |
193
|
17 |
|
if (!in_array($tag, $this->specialCaseTags)) { |
194
|
|
|
$this->specialCaseTags[] = $tag; |
195
|
|
|
} |
196
|
|
|
|
197
|
17 |
|
$opening = $this->getOpeningTag($tag); |
198
|
17 |
|
$closing = $this->getClosingTag($tag); |
199
|
|
|
|
200
|
17 |
|
if (!in_array($opening, $this->specialCaseOpeningTags)) { |
201
|
17 |
|
$this->specialCaseOpeningTags[] = $opening; |
202
|
|
|
} |
203
|
17 |
|
if (!in_array($closing, $this->specialCaseClosingTags)) { |
204
|
17 |
|
$this->specialCaseClosingTags[] = $closing; |
205
|
|
|
} |
206
|
|
|
|
207
|
17 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $tag |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
public function removeSpecialCaseTag($tag) |
216
|
|
|
{ |
217
|
|
|
if (($key = array_search($tag, $this->specialCaseTags)) !== false) { |
218
|
|
|
unset($this->specialCaseTags[$key]); |
219
|
|
|
|
220
|
|
|
$opening = $this->getOpeningTag($tag); |
221
|
|
|
$closing = $this->getClosingTag($tag); |
222
|
|
|
|
223
|
|
|
if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) { |
224
|
|
|
unset($this->specialCaseOpeningTags[$key]); |
225
|
|
|
} |
226
|
|
|
if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) { |
227
|
|
|
unset($this->specialCaseClosingTags[$key]); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return array|null |
236
|
|
|
*/ |
237
|
|
|
public function getSpecialCaseTags() |
238
|
|
|
{ |
239
|
|
|
return $this->specialCaseTags; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return bool |
244
|
|
|
*/ |
245
|
17 |
|
public function isGroupDiffs() |
246
|
|
|
{ |
247
|
17 |
|
return $this->groupDiffs; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param bool $groupDiffs |
252
|
|
|
* |
253
|
|
|
* @return HtmlDiffConfig |
254
|
|
|
*/ |
255
|
|
|
public function setGroupDiffs($groupDiffs) |
256
|
|
|
{ |
257
|
|
|
$this->groupDiffs = $groupDiffs; |
258
|
|
|
|
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
1 |
|
public function getEncoding() |
266
|
|
|
{ |
267
|
1 |
|
return $this->encoding; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string $encoding |
272
|
|
|
* |
273
|
|
|
* @return HtmlDiffConfig |
274
|
|
|
*/ |
275
|
17 |
|
public function setEncoding($encoding) |
276
|
|
|
{ |
277
|
17 |
|
$this->encoding = $encoding; |
278
|
|
|
|
279
|
17 |
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return bool |
284
|
|
|
*/ |
285
|
|
|
public function isInsertSpaceInReplace() |
286
|
|
|
{ |
287
|
|
|
return $this->insertSpaceInReplace; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param bool $insertSpaceInReplace |
292
|
|
|
* |
293
|
|
|
* @return HtmlDiffConfig |
294
|
|
|
*/ |
295
|
|
|
public function setInsertSpaceInReplace($insertSpaceInReplace) |
296
|
|
|
{ |
297
|
|
|
$this->insertSpaceInReplace = $insertSpaceInReplace; |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return bool |
304
|
|
|
*/ |
305
|
|
|
public function isKeepNewLines() |
306
|
|
|
{ |
307
|
|
|
return $this->keepNewLines; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param bool $keepNewLines |
312
|
|
|
*/ |
313
|
|
|
public function setKeepNewLines($keepNewLines) |
314
|
|
|
{ |
315
|
|
|
$this->keepNewLines = $keepNewLines; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
17 |
|
public function getIsolatedDiffTags() |
322
|
|
|
{ |
323
|
17 |
|
return $this->isolatedDiffTags; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param array $isolatedDiffTags |
328
|
|
|
* |
329
|
|
|
* @return HtmlDiffConfig |
330
|
|
|
*/ |
331
|
|
|
public function setIsolatedDiffTags($isolatedDiffTags) |
332
|
|
|
{ |
333
|
|
|
$this->isolatedDiffTags = $isolatedDiffTags; |
334
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param string $tag |
340
|
|
|
* @param null|string $placeholder |
341
|
|
|
* |
342
|
|
|
* @return $this |
343
|
|
|
*/ |
344
|
|
|
public function addIsolatedDiffTag($tag, $placeholder = null) |
345
|
|
|
{ |
346
|
|
|
if (null === $placeholder) { |
347
|
|
|
$placeholder = sprintf('[[REPLACE_%s]]', mb_strtoupper($tag)); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
if ($this->isIsolatedDiffTag($tag) && $this->isolatedDiffTags[$tag] !== $placeholder) { |
351
|
|
|
throw new \InvalidArgumentException( |
352
|
|
|
sprintf('Isolated diff tag "%s" already exists using a different placeholder', $tag) |
353
|
|
|
); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
$matchingKey = array_search($placeholder, $this->isolatedDiffTags, true); |
357
|
|
|
if (false !== $matchingKey && $matchingKey !== $tag) { |
358
|
|
|
throw new \InvalidArgumentException( |
359
|
|
|
sprintf('Placeholder already being used for a different tag "%s"', $tag) |
360
|
|
|
); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
if (!array_key_exists($tag, $this->isolatedDiffTags)) { |
364
|
|
|
$this->isolatedDiffTags[$tag] = $placeholder; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return $this; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @param string $tag |
372
|
|
|
* |
373
|
|
|
* @return $this |
374
|
|
|
*/ |
375
|
|
|
public function removeIsolatedDiffTag($tag) |
376
|
|
|
{ |
377
|
|
|
if ($this->isIsolatedDiffTag($tag)) { |
378
|
|
|
unset($this->isolatedDiffTags[$tag]); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @param string $tag |
386
|
|
|
* |
387
|
|
|
* @return bool |
388
|
|
|
*/ |
389
|
15 |
|
public function isIsolatedDiffTag($tag) |
390
|
|
|
{ |
391
|
15 |
|
return array_key_exists($tag, $this->isolatedDiffTags); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @param string $text |
396
|
|
|
* |
397
|
|
|
* @return bool |
398
|
|
|
*/ |
399
|
17 |
|
public function isIsolatedDiffTagPlaceholder($text) |
400
|
|
|
{ |
401
|
17 |
|
return in_array($text, $this->isolatedDiffTags, true); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @param string $tag |
406
|
|
|
* |
407
|
|
|
* @return null|string |
408
|
|
|
*/ |
409
|
15 |
|
public function getIsolatedDiffTagPlaceholder($tag) |
410
|
|
|
{ |
411
|
15 |
|
return $this->isIsolatedDiffTag($tag) ? $this->isolatedDiffTags[$tag] : null; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @return array |
416
|
|
|
*/ |
417
|
7 |
|
public function getSpecialCaseOpeningTags() |
418
|
|
|
{ |
419
|
7 |
|
return $this->specialCaseOpeningTags; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @return array |
424
|
|
|
*/ |
425
|
7 |
|
public function getSpecialCaseClosingTags() |
426
|
|
|
{ |
427
|
7 |
|
return $this->specialCaseClosingTags; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @return bool |
432
|
|
|
*/ |
433
|
10 |
|
public function isUseTableDiffing() |
434
|
|
|
{ |
435
|
10 |
|
return $this->useTableDiffing; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @param bool $useTableDiffing |
440
|
|
|
* |
441
|
|
|
* @return HtmlDiffConfig |
442
|
|
|
*/ |
443
|
|
|
public function setUseTableDiffing($useTableDiffing) |
444
|
|
|
{ |
445
|
|
|
$this->useTableDiffing = $useTableDiffing; |
446
|
|
|
|
447
|
|
|
return $this; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
452
|
|
|
* |
453
|
|
|
* @return $this |
454
|
|
|
*/ |
455
|
|
|
public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
456
|
|
|
{ |
457
|
|
|
$this->cacheProvider = $cacheProvider; |
458
|
|
|
|
459
|
|
|
return $this; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @return null|\Doctrine\Common\Cache\Cache |
464
|
|
|
*/ |
465
|
17 |
|
public function getCacheProvider() |
466
|
|
|
{ |
467
|
17 |
|
return $this->cacheProvider; |
468
|
|
|
} |
469
|
|
|
|
470
|
17 |
|
public function isPurifierEnabled(): bool |
471
|
|
|
{ |
472
|
17 |
|
return $this->purifierEnabled; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
public function setPurifierEnabled(bool $purifierEnabled = true): self |
476
|
|
|
{ |
477
|
|
|
$this->purifierEnabled = $purifierEnabled; |
478
|
|
|
|
479
|
|
|
return $this; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @param null|string |
484
|
|
|
* |
485
|
|
|
* @return $this |
486
|
|
|
*/ |
487
|
2 |
|
public function setPurifierCacheLocation($purifierCacheLocation = null) |
488
|
|
|
{ |
489
|
2 |
|
$this->purifierCacheLocation = $purifierCacheLocation; |
490
|
|
|
|
491
|
2 |
|
return $this; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @return null|string |
496
|
|
|
*/ |
497
|
17 |
|
public function getPurifierCacheLocation() |
498
|
|
|
{ |
499
|
17 |
|
return $this->purifierCacheLocation; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* @param string $tag |
504
|
|
|
* |
505
|
|
|
* @return string |
506
|
|
|
*/ |
507
|
17 |
|
protected function getOpeningTag($tag) |
508
|
|
|
{ |
509
|
17 |
|
return '/<'.$tag.'[^>]*/i'; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* @param string $tag |
514
|
|
|
* |
515
|
|
|
* @return string |
516
|
|
|
*/ |
517
|
17 |
|
protected function getClosingTag($tag) |
518
|
|
|
{ |
519
|
17 |
|
return '</'.$tag.'>'; |
520
|
|
|
} |
521
|
|
|
} |
522
|
|
|
|
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.