|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* (c) Steve Nebes <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SN\DaisyDiff\Html\Dom; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Creates a DOM tree from SAX-like events. |
|
15
|
|
|
*/ |
|
16
|
|
|
class DomTreeBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var TextNode[] */ |
|
19
|
|
|
private $textNodes = []; |
|
20
|
|
|
|
|
21
|
|
|
/** @var BodyNode */ |
|
22
|
|
|
private $bodyNode; |
|
23
|
|
|
|
|
24
|
|
|
/** @var TagNode */ |
|
25
|
|
|
private $currentParent; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $newWord = ''; |
|
29
|
|
|
|
|
30
|
|
|
/** @var bool */ |
|
31
|
|
|
private $documentStarted = false; |
|
32
|
|
|
|
|
33
|
|
|
/** @var bool */ |
|
34
|
|
|
private $documentEnded = false; |
|
35
|
|
|
|
|
36
|
|
|
/** @var bool */ |
|
37
|
|
|
private $bodyStarted = false; |
|
38
|
|
|
|
|
39
|
|
|
/** @var bool */ |
|
40
|
|
|
private $bodyEnded = false; |
|
41
|
|
|
|
|
42
|
|
|
/** @var bool */ |
|
43
|
|
|
private $whiteSpaceBeforeThis = false; |
|
44
|
|
|
|
|
45
|
|
|
/** @var int */ |
|
46
|
|
|
private $numberOfActivePreTags = 0; |
|
47
|
|
|
|
|
48
|
|
|
/** @var Node|null */ |
|
49
|
|
|
private $lastSibling; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Default values. |
|
53
|
|
|
*/ |
|
54
|
68 |
|
public function __construct() |
|
55
|
|
|
{ |
|
56
|
68 |
|
$this->bodyNode = new BodyNode(); |
|
57
|
68 |
|
$this->currentParent = $this->bodyNode; |
|
58
|
68 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return BodyNode |
|
62
|
|
|
*/ |
|
63
|
48 |
|
public function getBodyNode(): BodyNode |
|
64
|
|
|
{ |
|
65
|
48 |
|
return $this->bodyNode; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return TextNode[] |
|
70
|
|
|
*/ |
|
71
|
35 |
|
public function getTextNodes(): array |
|
72
|
|
|
{ |
|
73
|
35 |
|
return $this->textNodes; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function isDocumentStarted(): bool |
|
80
|
|
|
{ |
|
81
|
2 |
|
return $this->documentStarted; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function isDocumentEnded(): bool |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->documentEnded; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Starts the document, if one has not already been started. |
|
94
|
|
|
* |
|
95
|
|
|
* @throws \RuntimeException |
|
96
|
|
|
*/ |
|
97
|
47 |
|
public function startDocument(): void |
|
98
|
|
|
{ |
|
99
|
47 |
|
if ($this->documentStarted) { |
|
100
|
1 |
|
throw new \RuntimeException('This Handler only accepts one document.'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
47 |
|
$this->documentStarted = true; |
|
104
|
47 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Ends the document, if a document is started. |
|
108
|
|
|
* |
|
109
|
|
|
* @throws \RuntimeException |
|
110
|
|
|
*/ |
|
111
|
28 |
|
public function endDocument(): void |
|
112
|
|
|
{ |
|
113
|
28 |
|
if (!$this->documentStarted || $this->documentEnded) { |
|
114
|
2 |
|
throw new \RuntimeException(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
27 |
|
$this->endWord(); |
|
118
|
|
|
|
|
119
|
27 |
|
$this->documentEnded = true; |
|
120
|
27 |
|
$this->documentStarted = false; |
|
121
|
27 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param mixed $xmlParser |
|
125
|
|
|
* @param string $qName |
|
126
|
|
|
* @param array $attributes |
|
127
|
|
|
* |
|
128
|
|
|
* @throws \RuntimeException |
|
129
|
|
|
*/ |
|
130
|
37 |
|
public function startElement($xmlParser, string $qName, array $attributes = []): void |
|
131
|
|
|
{ |
|
132
|
|
|
// Required parameter, but not used. |
|
133
|
37 |
|
\assert($xmlParser); |
|
134
|
|
|
|
|
135
|
37 |
|
$qName = \mb_strtolower($qName); |
|
136
|
|
|
|
|
137
|
37 |
|
if (!$this->documentStarted || $this->documentEnded) { |
|
138
|
2 |
|
throw new \RuntimeException(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
35 |
|
if ($this->bodyStarted && !$this->bodyEnded) { |
|
142
|
26 |
|
$this->endWord(); |
|
143
|
|
|
|
|
144
|
26 |
|
$newTagNode = new TagNode($this->currentParent, $qName, $attributes); |
|
145
|
26 |
|
$this->currentParent = $newTagNode; |
|
146
|
26 |
|
$this->lastSibling = null; |
|
147
|
|
|
|
|
148
|
26 |
|
if ($this->whiteSpaceBeforeThis && $newTagNode->isInline()) { |
|
149
|
4 |
|
$this->currentParent->setWhiteBefore(true); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
26 |
|
$this->whiteSpaceBeforeThis = false; |
|
153
|
|
|
|
|
154
|
26 |
|
if ($newTagNode->isPre()) { |
|
155
|
3 |
|
$this->numberOfActivePreTags++; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
26 |
|
if ($this->isSeparatingTag($newTagNode)) { |
|
159
|
26 |
|
$this->addSeparatorNode(); |
|
160
|
|
|
} |
|
161
|
25 |
|
} elseif ($this->bodyStarted) { |
|
162
|
|
|
// Ignoring element after body tag closed. |
|
163
|
23 |
|
} elseif ('body' === $qName) { |
|
164
|
19 |
|
$this->bodyStarted = true; |
|
165
|
|
|
} |
|
166
|
35 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param mixed $xmlParser |
|
170
|
|
|
* @param string $qName |
|
171
|
|
|
* |
|
172
|
|
|
* @throws \RuntimeException |
|
173
|
|
|
*/ |
|
174
|
29 |
|
public function endElement($xmlParser, string $qName): void |
|
175
|
|
|
{ |
|
176
|
|
|
// Required parameter, but not used. |
|
177
|
29 |
|
\assert($xmlParser); |
|
178
|
|
|
|
|
179
|
29 |
|
$qName = \mb_strtolower($qName); |
|
180
|
|
|
|
|
181
|
29 |
|
if (!$this->documentStarted || $this->documentEnded) { |
|
182
|
2 |
|
throw new \RuntimeException(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
27 |
|
if ('body' === $qName) { |
|
186
|
18 |
|
$this->bodyEnded = true; |
|
187
|
25 |
|
} elseif ($this->bodyStarted && !$this->bodyEnded) { |
|
188
|
20 |
|
if ('img' === $qName) { |
|
189
|
|
|
// Insert a dummy leaf for the image. |
|
190
|
2 |
|
$img = new ImageNode($this->currentParent, $this->currentParent->getAttributes()); |
|
191
|
2 |
|
$img->setWhiteBefore($this->whiteSpaceBeforeThis); |
|
192
|
2 |
|
$this->lastSibling = $img; |
|
193
|
2 |
|
$this->textNodes[] = $img; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
20 |
|
$this->endWord(); |
|
197
|
|
|
|
|
198
|
20 |
|
if ($this->currentParent->isInline()) { |
|
199
|
9 |
|
$this->lastSibling = $this->currentParent; |
|
200
|
|
|
} else { |
|
201
|
17 |
|
$this->lastSibling = null; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
20 |
|
if ('pre' === $qName) { |
|
205
|
2 |
|
$this->numberOfActivePreTags--; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
20 |
|
if ($this->isSeparatingTag($this->currentParent)) { |
|
209
|
17 |
|
$this->addSeparatorNode(); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
20 |
|
$this->currentParent = $this->currentParent->getParent(); |
|
213
|
20 |
|
$this->whiteSpaceBeforeThis = false; |
|
214
|
|
|
} |
|
215
|
27 |
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param mixed $xmlParser |
|
219
|
|
|
* @param string $chars |
|
220
|
|
|
* |
|
221
|
|
|
* @throws \RuntimeException |
|
222
|
|
|
*/ |
|
223
|
23 |
|
public function characters($xmlParser, string $chars): void |
|
224
|
|
|
{ |
|
225
|
23 |
|
\assert($xmlParser); |
|
226
|
|
|
|
|
227
|
23 |
|
if (!$this->documentStarted || $this->documentEnded) { |
|
228
|
1 |
|
throw new \RuntimeException(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
22 |
|
for ($i = 0, $iMax = \mb_strlen($chars); $i < $iMax; $i++) { |
|
232
|
22 |
|
$c = \mb_substr($chars, $i, 1); |
|
233
|
|
|
|
|
234
|
22 |
|
if ($this->isDelimiter($c)) { |
|
235
|
17 |
|
$this->endWord(); |
|
236
|
|
|
|
|
237
|
17 |
|
if (WhiteSpaceNode::isWhiteSpace($c) && $this->numberOfActivePreTags === 0) { |
|
238
|
14 |
|
if (null !== $this->lastSibling) { |
|
239
|
14 |
|
$this->lastSibling->setWhiteAfter(true); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
14 |
|
$this->whiteSpaceBeforeThis = true; |
|
243
|
|
|
} else { |
|
244
|
4 |
|
$textNode = new TextNode($this->currentParent, $c); |
|
245
|
4 |
|
$textNode->setWhiteBefore($this->whiteSpaceBeforeThis); |
|
246
|
|
|
|
|
247
|
4 |
|
$this->whiteSpaceBeforeThis = false; |
|
248
|
4 |
|
$this->lastSibling = $textNode; |
|
249
|
17 |
|
$this->textNodes[] = $textNode; |
|
250
|
|
|
} |
|
251
|
|
|
} else { |
|
252
|
22 |
|
$this->newWord .= $c; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
22 |
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @return void |
|
259
|
|
|
*/ |
|
260
|
35 |
|
private function endWord(): void |
|
261
|
|
|
{ |
|
262
|
35 |
|
if (\mb_strlen($this->newWord) > 0) { |
|
263
|
22 |
|
$node = new TextNode($this->currentParent, $this->newWord); |
|
264
|
22 |
|
$node->setWhiteBefore($this->whiteSpaceBeforeThis); |
|
265
|
|
|
|
|
266
|
22 |
|
$this->whiteSpaceBeforeThis = false; |
|
267
|
22 |
|
$this->lastSibling = $node; |
|
268
|
22 |
|
$this->textNodes[] = $node; |
|
269
|
22 |
|
$this->newWord = ''; |
|
270
|
|
|
} |
|
271
|
35 |
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Returns true if the given tag separates text nodes from being successive. I.e. every block starts a new distinct |
|
275
|
|
|
* text flow. |
|
276
|
|
|
* |
|
277
|
|
|
* @param TagNode $tagNode |
|
278
|
|
|
* @return bool |
|
279
|
|
|
*/ |
|
280
|
27 |
|
private function isSeparatingTag(TagNode $tagNode): bool |
|
281
|
|
|
{ |
|
282
|
27 |
|
return $tagNode->isBlockLevel(); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Ensures that a separator is added after the last text node. |
|
287
|
|
|
*/ |
|
288
|
24 |
|
private function addSeparatorNode(): void |
|
289
|
|
|
{ |
|
290
|
24 |
|
if (empty($this->textNodes)) { |
|
291
|
24 |
|
return; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
// Don't add multiple separators. |
|
295
|
14 |
|
if ($this->textNodes[\count($this->textNodes) - 1] instanceof SeparatingNode) { |
|
296
|
3 |
|
return; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
14 |
|
$this->textNodes[] = new SeparatingNode($this->currentParent); |
|
300
|
14 |
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @param string $c |
|
304
|
|
|
* @return bool |
|
305
|
|
|
*/ |
|
306
|
22 |
|
public static function isDelimiter(string $c): bool |
|
307
|
|
|
{ |
|
308
|
22 |
|
if (WhiteSpaceNode::isWhiteSpace($c)) { |
|
309
|
15 |
|
return true; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
22 |
|
switch ($c) { |
|
313
|
|
|
// Basic Delimiters |
|
314
|
22 |
|
case '/': |
|
315
|
22 |
|
case '.': |
|
316
|
22 |
|
case '!': |
|
317
|
22 |
|
case ',': |
|
318
|
22 |
|
case ';': |
|
319
|
22 |
|
case '?': |
|
320
|
22 |
|
case '=': |
|
321
|
22 |
|
case "'": |
|
322
|
22 |
|
case '"': |
|
323
|
|
|
// Extra Delimiters |
|
324
|
22 |
|
case '[': |
|
325
|
22 |
|
case ']': |
|
326
|
22 |
|
case '{': |
|
327
|
22 |
|
case '}': |
|
328
|
22 |
|
case '(': |
|
329
|
22 |
|
case ')': |
|
330
|
22 |
|
case '&': |
|
331
|
22 |
|
case '|': |
|
332
|
22 |
|
case "\\": |
|
333
|
22 |
|
case '-': |
|
334
|
22 |
|
case '_': |
|
335
|
22 |
|
case '+': |
|
336
|
22 |
|
case '*': |
|
337
|
22 |
|
case ':': |
|
338
|
4 |
|
return true; |
|
339
|
|
|
default: |
|
340
|
22 |
|
break; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
22 |
|
return false; |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|