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