1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caxy\HtmlDiff; |
4
|
|
|
|
5
|
|
|
abstract class AbstractDiff |
6
|
|
|
{ |
7
|
|
|
public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
|
|
|
|
8
|
|
|
public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
9
|
|
|
public static $defaultGroupDiffs = true; |
|
|
|
|
10
|
|
|
|
11
|
|
|
protected $content; |
12
|
|
|
protected $oldText; |
13
|
|
|
protected $newText; |
14
|
|
|
protected $oldWords = array(); |
15
|
|
|
protected $newWords = array(); |
16
|
|
|
protected $encoding; |
17
|
|
|
protected $specialCaseOpeningTags = array(); |
18
|
|
|
protected $specialCaseClosingTags = array(); |
19
|
|
|
protected $specialCaseTags; |
20
|
|
|
protected $specialCaseChars; |
21
|
|
|
protected $groupDiffs; |
22
|
|
|
protected $matchThreshold = 80; |
23
|
|
|
|
24
|
|
|
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
25
|
|
|
{ |
26
|
|
|
if ($specialCaseTags === null) { |
27
|
|
|
$specialCaseTags = static::$defaultSpecialCaseTags; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($groupDiffs === null) { |
31
|
|
|
$groupDiffs = static::$defaultGroupDiffs; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->oldText = $this->purifyHtml(trim($oldText)); |
|
|
|
|
35
|
|
|
$this->newText = $this->purifyHtml(trim($newText)); |
|
|
|
|
36
|
|
|
$this->encoding = $encoding; |
|
|
|
|
37
|
|
|
$this->content = ''; |
|
|
|
|
38
|
|
|
$this->groupDiffs = $groupDiffs; |
39
|
|
|
$this->setSpecialCaseTags($specialCaseTags); |
40
|
|
|
$this->setSpecialCaseChars(static::$defaultSpecialCaseChars); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function getMatchThreshold() |
47
|
|
|
{ |
48
|
|
|
return $this->matchThreshold; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param int $matchThreshold |
53
|
|
|
* |
54
|
|
|
* @return AbstractDiff |
55
|
|
|
*/ |
56
|
|
|
public function setMatchThreshold($matchThreshold) |
57
|
|
|
{ |
58
|
|
|
$this->matchThreshold = $matchThreshold; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
public function setSpecialCaseChars(array $chars) |
66
|
|
|
{ |
67
|
|
|
$this->specialCaseChars = $chars; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getSpecialCaseChars() |
71
|
|
|
{ |
72
|
|
|
return $this->specialCaseChars; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addSpecialCaseChar($char) |
76
|
|
|
{ |
77
|
|
|
if (!in_array($char, $this->specialCaseChars)) { |
78
|
|
|
$this->specialCaseChars[] = $char; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function removeSpecialCaseChar($char) |
83
|
|
|
{ |
84
|
|
|
$key = array_search($char, $this->specialCaseChars); |
85
|
|
|
if ($key !== false) { |
86
|
|
|
unset($this->specialCaseChars[$key]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setSpecialCaseTags(array $tags = array()) |
91
|
|
|
{ |
92
|
|
|
$this->specialCaseTags = $tags; |
93
|
|
|
|
94
|
|
|
foreach ($this->specialCaseTags as $tag) { |
95
|
|
|
$this->addSpecialCaseTag($tag); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function addSpecialCaseTag($tag) |
100
|
|
|
{ |
101
|
|
|
if (!in_array($tag, $this->specialCaseTags)) { |
102
|
|
|
$this->specialCaseTags[] = $tag; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$opening = $this->getOpeningTag($tag); |
106
|
|
|
$closing = $this->getClosingTag($tag); |
107
|
|
|
|
108
|
|
|
if (!in_array($opening, $this->specialCaseOpeningTags)) { |
109
|
|
|
$this->specialCaseOpeningTags[] = $opening; |
110
|
|
|
} |
111
|
|
|
if (!in_array($closing, $this->specialCaseClosingTags)) { |
112
|
|
|
$this->specialCaseClosingTags[] = $closing; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function removeSpecialCaseTag($tag) |
117
|
|
|
{ |
118
|
|
|
if (($key = array_search($tag, $this->specialCaseTags)) !== false) { |
119
|
|
|
unset($this->specialCaseTags[$key]); |
120
|
|
|
|
121
|
|
|
$opening = $this->getOpeningTag($tag); |
122
|
|
|
$closing = $this->getClosingTag($tag); |
123
|
|
|
|
124
|
|
|
if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) { |
125
|
|
|
unset($this->specialCaseOpeningTags[$key]); |
126
|
|
|
} |
127
|
|
|
if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) { |
128
|
|
|
unset($this->specialCaseClosingTags[$key]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getSpecialCaseTags() |
134
|
|
|
{ |
135
|
|
|
return $this->specialCaseTags; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getOldHtml() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
return $this->oldText; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getNewHtml() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return $this->newText; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getDifference() |
149
|
|
|
{ |
150
|
|
|
return $this->content; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setGroupDiffs($boolean) |
154
|
|
|
{ |
155
|
|
|
$this->groupDiffs = $boolean; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function isGroupDiffs() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
return $this->groupDiffs; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function getOpeningTag($tag) |
164
|
|
|
{ |
165
|
|
|
return "/<".$tag."[^>]*/i"; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
protected function getClosingTag($tag) |
169
|
|
|
{ |
170
|
|
|
return "</".$tag.">"; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function getStringBetween($str, $start, $end) |
174
|
|
|
{ |
175
|
|
|
$expStr = explode( $start, $str, 2 ); |
176
|
|
|
if ( count( $expStr ) > 1 ) { |
177
|
|
|
$expStr = explode( $end, $expStr[ 1 ] ); |
178
|
|
|
if ( count( $expStr ) > 1 ) { |
179
|
|
|
array_pop( $expStr ); |
180
|
|
|
|
181
|
|
|
return implode( $end, $expStr ); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return ''; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function purifyHtml($html, $tags = null) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
if ( class_exists( 'Tidy' ) && false ) { |
191
|
|
|
$config = array( 'output-xhtml' => true, 'indent' => false ); |
192
|
|
|
$tidy = new tidy(); |
|
|
|
|
193
|
|
|
$tidy->parseString( $html, $config, 'utf8' ); |
194
|
|
|
$html = (string) $tidy; |
195
|
|
|
|
196
|
|
|
return $this->getStringBetween( $html, '<body>' ); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $html; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
protected function splitInputsToWords() |
203
|
|
|
{ |
204
|
|
|
$this->oldWords = $this->convertHtmlToListOfWords( $this->explode( $this->oldText ) ); |
205
|
|
|
$this->newWords = $this->convertHtmlToListOfWords( $this->explode( $this->newText ) ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
protected function isPartOfWord($text) |
209
|
|
|
{ |
210
|
|
|
return ctype_alnum(str_replace($this->specialCaseChars, '', $text)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
protected function convertHtmlToListOfWords($characterString) |
214
|
|
|
{ |
215
|
|
|
$mode = 'character'; |
|
|
|
|
216
|
|
|
$current_word = ''; |
|
|
|
|
217
|
|
|
$words = array(); |
|
|
|
|
218
|
|
|
foreach ($characterString as $i => $character) { |
219
|
|
|
switch ($mode) { |
220
|
|
|
case 'character': |
221
|
|
|
if ( $this->isStartOfTag( $character ) ) { |
222
|
|
|
if ($current_word != '') { |
|
|
|
|
223
|
|
|
$words[] = $current_word; |
|
|
|
|
224
|
|
|
} |
225
|
|
|
$current_word = "<"; |
|
|
|
|
226
|
|
|
$mode = 'tag'; |
|
|
|
|
227
|
|
|
} elseif ( preg_match( "[^\s]", $character ) > 0 ) { |
228
|
|
|
if ($current_word != '') { |
|
|
|
|
229
|
|
|
$words[] = $current_word; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
$current_word = $character; |
|
|
|
|
232
|
|
|
$mode = 'whitespace'; |
|
|
|
|
233
|
|
|
} else { |
234
|
|
|
if ( |
235
|
|
|
(ctype_alnum($character) && (strlen($current_word) == 0 || $this->isPartOfWord($current_word))) || |
|
|
|
|
236
|
|
|
(in_array($character, $this->specialCaseChars) && isset($characterString[$i+1]) && $this->isPartOfWord($characterString[$i+1])) |
237
|
|
|
) { |
238
|
|
|
$current_word .= $character; |
|
|
|
|
239
|
|
|
} else { |
240
|
|
|
$words[] = $current_word; |
|
|
|
|
241
|
|
|
$current_word = $character; |
|
|
|
|
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
break; |
245
|
|
|
case 'tag' : |
|
|
|
|
246
|
|
|
if ( $this->isEndOfTag( $character ) ) { |
247
|
|
|
$current_word .= ">"; |
|
|
|
|
248
|
|
|
$words[] = $current_word; |
|
|
|
|
249
|
|
|
$current_word = ""; |
|
|
|
|
250
|
|
|
|
251
|
|
|
if ( !preg_match('[^\s]', $character ) ) { |
252
|
|
|
$mode = 'whitespace'; |
253
|
|
|
} else { |
254
|
|
|
$mode = 'character'; |
255
|
|
|
} |
256
|
|
|
} else { |
257
|
|
|
$current_word .= $character; |
|
|
|
|
258
|
|
|
} |
259
|
|
|
break; |
260
|
|
|
case 'whitespace': |
261
|
|
|
if ( $this->isStartOfTag( $character ) ) { |
262
|
|
|
if ($current_word != '') { |
|
|
|
|
263
|
|
|
$words[] = $current_word; |
|
|
|
|
264
|
|
|
} |
265
|
|
|
$current_word = "<"; |
|
|
|
|
266
|
|
|
$mode = 'tag'; |
|
|
|
|
267
|
|
|
} elseif ( preg_match( "[^\s]", $character ) ) { |
268
|
|
|
$current_word .= $character; |
|
|
|
|
269
|
|
|
} else { |
270
|
|
|
if ($current_word != '') { |
|
|
|
|
271
|
|
|
$words[] = $current_word; |
|
|
|
|
272
|
|
|
} |
273
|
|
|
$current_word = $character; |
|
|
|
|
274
|
|
|
$mode = 'character'; |
|
|
|
|
275
|
|
|
} |
276
|
|
|
break; |
277
|
|
|
default: |
278
|
|
|
break; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
if ($current_word != '') { |
|
|
|
|
282
|
|
|
$words[] = $current_word; |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $words; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
protected function isStartOfTag($val) |
289
|
|
|
{ |
290
|
|
|
return $val == "<"; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
protected function isEndOfTag($val) |
294
|
|
|
{ |
295
|
|
|
return $val == ">"; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
protected function isWhiteSpace($value) |
299
|
|
|
{ |
300
|
|
|
return !preg_match( '[^\s]', $value ); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
protected function explode($value) |
304
|
|
|
{ |
305
|
|
|
// as suggested by @onassar |
306
|
|
|
return preg_split( '//u', $value ); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.