1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caxy\HtmlDiff; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AbstractDiff. |
7
|
|
|
*/ |
8
|
|
|
abstract class AbstractDiff |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
* |
13
|
|
|
* @deprecated since 0.1.0 |
14
|
|
|
*/ |
15
|
|
|
public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
* |
20
|
|
|
* @deprecated since 0.1.0 |
21
|
|
|
*/ |
22
|
|
|
public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
* |
27
|
|
|
* @deprecated since 0.1.0 |
28
|
|
|
*/ |
29
|
|
|
public static $defaultGroupDiffs = true; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var HtmlDiffConfig |
33
|
|
|
*/ |
34
|
|
|
protected $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $content; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $oldText; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $newText; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $oldWords = array(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $newWords = array(); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var DiffCache[] |
63
|
|
|
*/ |
64
|
|
|
protected $diffCaches = array(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \HTMLPurifier |
68
|
|
|
*/ |
69
|
|
|
protected $purifier; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var \HTMLPurifier_Config|null |
73
|
|
|
*/ |
74
|
|
|
protected $purifierConfig = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* AbstractDiff constructor. |
78
|
|
|
* |
79
|
|
|
* @param string $oldText |
80
|
|
|
* @param string $newText |
81
|
|
|
* @param string $encoding |
82
|
|
|
* @param null|array $specialCaseTags |
83
|
|
|
* @param null|bool $groupDiffs |
84
|
|
|
*/ |
85
|
14 |
|
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
86
|
|
|
{ |
87
|
14 |
|
mb_substitute_character(0x20); |
88
|
|
|
|
89
|
14 |
|
$this->setConfig(HtmlDiffConfig::create()->setEncoding($encoding)); |
90
|
|
|
|
91
|
14 |
|
if ($specialCaseTags !== null) { |
92
|
13 |
|
$this->config->setSpecialCaseTags($specialCaseTags); |
93
|
13 |
|
} |
94
|
|
|
|
95
|
14 |
|
if ($groupDiffs !== null) { |
96
|
|
|
$this->config->setGroupDiffs($groupDiffs); |
97
|
|
|
} |
98
|
|
|
|
99
|
14 |
|
$this->oldText = $oldText; |
100
|
14 |
|
$this->newText = $newText; |
101
|
14 |
|
$this->content = ''; |
102
|
14 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool|string |
106
|
|
|
*/ |
107
|
|
|
abstract public function build(); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Initializes HTMLPurifier with cache location. |
111
|
|
|
* |
112
|
|
|
* @param null|string $defaultPurifierSerializerCache |
113
|
|
|
*/ |
114
|
14 |
|
public function initPurifier($defaultPurifierSerializerCache = null) |
115
|
|
|
{ |
116
|
14 |
|
$HTMLPurifierConfig = null; |
|
|
|
|
117
|
|
|
|
118
|
14 |
|
if (null !== $this->purifierConfig) { |
119
|
2 |
|
$HTMLPurifierConfig = $this->purifierConfig; |
120
|
2 |
|
} else { |
121
|
14 |
|
$HTMLPurifierConfig = \HTMLPurifier_Config::createDefault(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Cache.SerializerPath defaults to Null and sets |
125
|
|
|
// the location to inside the vendor HTMLPurifier library |
126
|
|
|
// under the DefinitionCache/Serializer folder. |
127
|
14 |
|
if (!is_null($defaultPurifierSerializerCache)) { |
128
|
2 |
|
$HTMLPurifierConfig->set('Cache.SerializerPath', $defaultPurifierSerializerCache); |
129
|
2 |
|
} |
130
|
|
|
|
131
|
14 |
|
$this->purifier = new \HTMLPurifier($HTMLPurifierConfig); |
132
|
14 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Prepare (purify) the HTML |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
14 |
|
protected function prepare() |
140
|
|
|
{ |
141
|
14 |
|
$this->initPurifier($this->config->getPurifierCacheLocation()); |
142
|
|
|
|
143
|
14 |
|
$this->oldText = $this->purifyHtml($this->oldText); |
|
|
|
|
144
|
14 |
|
$this->newText = $this->purifyHtml($this->newText); |
|
|
|
|
145
|
14 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return DiffCache|null |
149
|
|
|
*/ |
150
|
1 |
|
protected function getDiffCache() |
151
|
|
|
{ |
152
|
|
|
if (!$this->hasDiffCache()) { |
153
|
1 |
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$hash = spl_object_hash($this->getConfig()->getCacheProvider()); |
157
|
|
|
|
158
|
1 |
|
if (!array_key_exists($hash, $this->diffCaches)) { |
159
|
|
|
$this->diffCaches[$hash] = new DiffCache($this->getConfig()->getCacheProvider()); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this->diffCaches[$hash]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
14 |
|
protected function hasDiffCache() |
169
|
|
|
{ |
170
|
14 |
|
return null !== $this->getConfig()->getCacheProvider(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return HtmlDiffConfig |
175
|
|
|
*/ |
176
|
14 |
|
public function getConfig() |
177
|
1 |
|
{ |
178
|
14 |
|
return $this->config; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param HtmlDiffConfig $config |
183
|
|
|
* |
184
|
|
|
* @return AbstractDiff |
185
|
|
|
*/ |
186
|
14 |
|
public function setConfig(HtmlDiffConfig $config) |
187
|
|
|
{ |
188
|
14 |
|
$this->config = $config; |
189
|
|
|
|
190
|
14 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return int |
195
|
|
|
* |
196
|
|
|
* @deprecated since 0.1.0 |
197
|
|
|
*/ |
198
|
|
|
public function getMatchThreshold() |
199
|
|
|
{ |
200
|
|
|
return $this->config->getMatchThreshold(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param int $matchThreshold |
205
|
|
|
* |
206
|
|
|
* @return AbstractDiff |
207
|
|
|
* |
208
|
|
|
* @deprecated since 0.1.0 |
209
|
|
|
*/ |
210
|
|
|
public function setMatchThreshold($matchThreshold) |
211
|
|
|
{ |
212
|
|
|
$this->config->setMatchThreshold($matchThreshold); |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param array $chars |
219
|
|
|
* |
220
|
|
|
* @deprecated since 0.1.0 |
221
|
|
|
*/ |
222
|
|
|
public function setSpecialCaseChars(array $chars) |
223
|
|
|
{ |
224
|
|
|
$this->config->setSpecialCaseChars($chars); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return array|null |
229
|
|
|
* |
230
|
|
|
* @deprecated since 0.1.0 |
231
|
|
|
*/ |
232
|
|
|
public function getSpecialCaseChars() |
233
|
|
|
{ |
234
|
|
|
return $this->config->getSpecialCaseChars(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $char |
239
|
|
|
* |
240
|
|
|
* @deprecated since 0.1.0 |
241
|
|
|
*/ |
242
|
|
|
public function addSpecialCaseChar($char) |
243
|
|
|
{ |
244
|
|
|
$this->config->addSpecialCaseChar($char); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param string $char |
249
|
|
|
* |
250
|
|
|
* @deprecated since 0.1.0 |
251
|
|
|
*/ |
252
|
|
|
public function removeSpecialCaseChar($char) |
253
|
|
|
{ |
254
|
|
|
$this->config->removeSpecialCaseChar($char); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param array $tags |
259
|
|
|
* |
260
|
|
|
* @deprecated since 0.1.0 |
261
|
|
|
*/ |
262
|
|
|
public function setSpecialCaseTags(array $tags = array()) |
263
|
|
|
{ |
264
|
|
|
$this->config->setSpecialCaseChars($tags); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param string $tag |
269
|
|
|
* |
270
|
|
|
* @deprecated since 0.1.0 |
271
|
|
|
*/ |
272
|
|
|
public function addSpecialCaseTag($tag) |
273
|
|
|
{ |
274
|
|
|
$this->config->addSpecialCaseTag($tag); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param string $tag |
279
|
|
|
* |
280
|
|
|
* @deprecated since 0.1.0 |
281
|
|
|
*/ |
282
|
|
|
public function removeSpecialCaseTag($tag) |
283
|
|
|
{ |
284
|
|
|
$this->config->removeSpecialCaseTag($tag); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return array|null |
289
|
|
|
* |
290
|
|
|
* @deprecated since 0.1.0 |
291
|
|
|
*/ |
292
|
|
|
public function getSpecialCaseTags() |
293
|
|
|
{ |
294
|
|
|
return $this->config->getSpecialCaseTags(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
public function getOldHtml() |
301
|
|
|
{ |
302
|
|
|
return $this->oldText; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
public function getNewHtml() |
309
|
|
|
{ |
310
|
|
|
return $this->newText; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
public function getDifference() |
317
|
|
|
{ |
318
|
|
|
return $this->content; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Clears the diff content. |
323
|
|
|
* |
324
|
|
|
* @return void |
325
|
|
|
*/ |
326
|
|
|
public function clearContent() |
327
|
|
|
{ |
328
|
|
|
$this->content = null; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param bool $boolean |
333
|
|
|
* |
334
|
|
|
* @return $this |
335
|
|
|
* |
336
|
|
|
* @deprecated since 0.1.0 |
337
|
|
|
*/ |
338
|
|
|
public function setGroupDiffs($boolean) |
339
|
|
|
{ |
340
|
|
|
$this->config->setGroupDiffs($boolean); |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @return bool |
347
|
|
|
* |
348
|
|
|
* @deprecated since 0.1.0 |
349
|
|
|
*/ |
350
|
|
|
public function isGroupDiffs() |
351
|
|
|
{ |
352
|
|
|
return $this->config->isGroupDiffs(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param \HTMLPurifier_Config $config |
357
|
|
|
*/ |
358
|
2 |
|
public function setHTMLPurifierConfig(\HTMLPurifier_Config $config) |
359
|
|
|
{ |
360
|
2 |
|
$this->purifierConfig = $config; |
361
|
2 |
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param string $tag |
365
|
|
|
* |
366
|
|
|
* @return string |
367
|
|
|
*/ |
368
|
|
|
protected function getOpeningTag($tag) |
369
|
|
|
{ |
370
|
|
|
return '/<'.$tag.'[^>]*/i'; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param string $tag |
375
|
|
|
* |
376
|
|
|
* @return string |
377
|
|
|
*/ |
378
|
|
|
protected function getClosingTag($tag) |
379
|
|
|
{ |
380
|
|
|
return '</'.$tag.'>'; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @param string $str |
385
|
|
|
* @param string $start |
386
|
|
|
* @param string $end |
387
|
|
|
* |
388
|
|
|
* @return string |
389
|
|
|
*/ |
390
|
|
|
protected function getStringBetween($str, $start, $end) |
391
|
|
|
{ |
392
|
|
|
$expStr = explode($start, $str, 2); |
393
|
|
|
if (count($expStr) > 1) { |
394
|
|
|
$expStr = explode($end, $expStr[ 1 ]); |
395
|
|
|
if (count($expStr) > 1) { |
396
|
|
|
array_pop($expStr); |
397
|
|
|
|
398
|
|
|
return implode($end, $expStr); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
return ''; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @param string $html |
407
|
|
|
* |
408
|
|
|
* @return string |
|
|
|
|
409
|
|
|
*/ |
410
|
14 |
|
protected function purifyHtml($html) |
411
|
|
|
{ |
412
|
14 |
|
if (class_exists('Tidy') && false) { |
413
|
|
|
$config = array('output-xhtml' => true, 'indent' => false); |
414
|
|
|
$tidy = new tidy(); |
415
|
|
|
$tidy->parseString($html, $config, 'utf8'); |
416
|
|
|
$html = (string) $tidy; |
417
|
|
|
|
418
|
|
|
return $this->getStringBetween($html, '<body>'); |
|
|
|
|
419
|
|
|
} |
420
|
|
|
|
421
|
14 |
|
return $this->purifier->purify($html); |
422
|
|
|
} |
423
|
|
|
|
424
|
14 |
|
protected function splitInputsToWords() |
425
|
|
|
{ |
426
|
14 |
|
$this->oldWords = $this->convertHtmlToListOfWords($this->explode($this->oldText)); |
427
|
14 |
|
$this->newWords = $this->convertHtmlToListOfWords($this->explode($this->newText)); |
428
|
14 |
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @param string $text |
432
|
|
|
* |
433
|
|
|
* @return bool |
434
|
|
|
*/ |
435
|
14 |
|
protected function isPartOfWord($text) |
436
|
|
|
{ |
437
|
14 |
|
return ctype_alnum(str_replace($this->config->getSpecialCaseChars(), '', $text)); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* @param array $characterString |
442
|
|
|
* |
443
|
|
|
* @return array |
444
|
|
|
*/ |
445
|
14 |
|
protected function convertHtmlToListOfWords($characterString) |
446
|
|
|
{ |
447
|
14 |
|
$mode = 'character'; |
448
|
14 |
|
$current_word = ''; |
449
|
14 |
|
$words = array(); |
450
|
14 |
|
foreach ($characterString as $i => $character) { |
451
|
|
|
switch ($mode) { |
452
|
14 |
|
case 'character': |
453
|
14 |
|
if ($this->isStartOfTag($character)) { |
454
|
13 |
|
if ($current_word != '') { |
455
|
12 |
|
$words[] = $current_word; |
456
|
12 |
|
} |
457
|
|
|
|
458
|
13 |
|
$current_word = '<'; |
459
|
13 |
|
$mode = 'tag'; |
460
|
14 |
|
} elseif (preg_match("/\s/", $character)) { |
461
|
12 |
|
if ($current_word !== '') { |
462
|
12 |
|
$words[] = $current_word; |
463
|
12 |
|
} |
464
|
12 |
|
$current_word = preg_replace('/\s+/S', ' ', $character); |
465
|
12 |
|
$mode = 'whitespace'; |
466
|
12 |
|
} else { |
467
|
|
|
if ( |
468
|
14 |
|
(ctype_alnum($character) && (strlen($current_word) == 0 || $this->isPartOfWord($current_word))) || |
|
|
|
|
469
|
14 |
|
(in_array($character, $this->config->getSpecialCaseChars()) && isset($characterString[$i + 1]) && $this->isPartOfWord($characterString[$i + 1])) |
|
|
|
|
470
|
14 |
|
) { |
471
|
14 |
|
$current_word .= $character; |
472
|
14 |
|
} else { |
473
|
14 |
|
$words[] = $current_word; |
474
|
14 |
|
$current_word = $character; |
475
|
|
|
} |
476
|
|
|
} |
477
|
14 |
|
break; |
478
|
14 |
|
case 'tag' : |
|
|
|
|
479
|
14 |
|
if ($this->isEndOfTag($character)) { |
480
|
14 |
|
$current_word .= '>'; |
481
|
14 |
|
$words[] = $current_word; |
482
|
14 |
|
$current_word = ''; |
483
|
|
|
|
484
|
14 |
|
if (!preg_match('[^\s]', $character)) { |
485
|
14 |
|
$mode = 'whitespace'; |
486
|
14 |
|
} else { |
487
|
|
|
$mode = 'character'; |
488
|
|
|
} |
489
|
14 |
|
} else { |
490
|
14 |
|
$current_word .= $character; |
491
|
|
|
} |
492
|
14 |
|
break; |
493
|
14 |
|
case 'whitespace': |
494
|
14 |
|
if ($this->isStartOfTag($character)) { |
495
|
12 |
|
if ($current_word !== '') { |
496
|
12 |
|
$words[] = $current_word; |
497
|
12 |
|
} |
498
|
12 |
|
$current_word = '<'; |
499
|
12 |
|
$mode = 'tag'; |
500
|
14 |
|
} elseif (preg_match("/\s/", $character)) { |
501
|
10 |
|
$current_word .= $character; |
502
|
10 |
|
$current_word = preg_replace('/\s+/S', ' ', $current_word); |
503
|
10 |
|
} else { |
504
|
14 |
|
if ($current_word != '') { |
505
|
12 |
|
$words[] = $current_word; |
506
|
12 |
|
} |
507
|
14 |
|
$current_word = $character; |
508
|
14 |
|
$mode = 'character'; |
509
|
|
|
} |
510
|
14 |
|
break; |
511
|
|
|
default: |
512
|
|
|
break; |
513
|
|
|
} |
514
|
14 |
|
} |
515
|
14 |
|
if ($current_word != '') { |
516
|
|
|
$words[] = $current_word; |
517
|
|
|
} |
518
|
|
|
|
519
|
14 |
|
return $words; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* @param string $val |
524
|
|
|
* |
525
|
|
|
* @return bool |
526
|
|
|
*/ |
527
|
14 |
|
protected function isStartOfTag($val) |
528
|
|
|
{ |
529
|
14 |
|
return $val == '<'; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* @param string $val |
534
|
|
|
* |
535
|
|
|
* @return bool |
536
|
|
|
*/ |
537
|
14 |
|
protected function isEndOfTag($val) |
538
|
|
|
{ |
539
|
14 |
|
return $val == '>'; |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* @param string $value |
544
|
|
|
* |
545
|
|
|
* @return bool |
546
|
|
|
*/ |
547
|
|
|
protected function isWhiteSpace($value) |
548
|
|
|
{ |
549
|
|
|
return !preg_match('[^\s]', $value); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* @param string $value |
554
|
|
|
* |
555
|
|
|
* @return array |
556
|
|
|
*/ |
557
|
14 |
|
protected function explode($value) |
558
|
|
|
{ |
559
|
|
|
// as suggested by @onassar |
560
|
14 |
|
return preg_split('//u', $value); |
561
|
|
|
} |
562
|
|
|
} |
563
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.