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