|
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
|
|
|
use SN\DaisyDiff\Html\Dom\Helper\LastCommonParentResult; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Represents any element in the DOM tree of a HTML file. |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class Node |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var TagNode|null */ |
|
21
|
|
|
protected $parent; |
|
22
|
|
|
|
|
23
|
|
|
/** @var TagNode */ |
|
24
|
|
|
private $root; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool */ |
|
27
|
|
|
private $whiteBefore = false; |
|
28
|
|
|
|
|
29
|
|
|
/** @var bool */ |
|
30
|
|
|
private $whiteAfter = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* This constructor not only sets the parameter as the parent for the created node, but also appends the created |
|
34
|
|
|
* node to the collection of parent's children. |
|
35
|
|
|
* |
|
36
|
|
|
* @param TagNode|null $parent |
|
37
|
|
|
*/ |
|
38
|
184 |
|
public function __construct(?TagNode $parent) |
|
39
|
|
|
{ |
|
40
|
184 |
|
$this->parent = $parent; |
|
41
|
|
|
|
|
42
|
184 |
|
if (null !== $parent) { |
|
43
|
139 |
|
$parent->addChild($this); |
|
44
|
139 |
|
$this->root = $parent->getRoot(); |
|
45
|
184 |
|
} elseif ($this instanceof TagNode) { |
|
46
|
184 |
|
$this->root = $this; |
|
47
|
|
|
} |
|
48
|
184 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the parent node instance. |
|
52
|
|
|
* |
|
53
|
|
|
* @return TagNode|null |
|
54
|
|
|
*/ |
|
55
|
140 |
|
public function getParent(): ?TagNode |
|
56
|
|
|
{ |
|
57
|
140 |
|
return $this->parent; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns a list of the ancestors that is ordered starting from the root by the depth. Index of an element in that |
|
62
|
|
|
* list corresponds its depth (if depth of the root is 0). |
|
63
|
|
|
* |
|
64
|
|
|
* @return TagNode[] |
|
65
|
|
|
*/ |
|
66
|
49 |
|
public function getParentTree(): array |
|
67
|
|
|
{ |
|
68
|
|
|
/** @var TagNode[] $ancestors */ |
|
69
|
49 |
|
$ancestors = []; |
|
70
|
|
|
|
|
71
|
49 |
|
for ($ancestor = $this->getParent(); null !== $ancestor; $ancestor = $ancestor->getParent()) { |
|
72
|
49 |
|
$ancestors[] = $ancestor; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
49 |
|
return \array_reverse($ancestors); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* "equals" method should work differently for the case where the compared nodes are from the same tree, and in that |
|
80
|
|
|
* case return true only if it's the same object. |
|
81
|
|
|
* |
|
82
|
|
|
* This method returns the root of the tree (which should be common ancestor for every node in the tree). If the |
|
83
|
|
|
* roots are the same object, then the nodes are in the same tree. |
|
84
|
|
|
* |
|
85
|
|
|
* Returns the "top" ancestor if this node has a parent, or the node itself if there is no parent, and this is a |
|
86
|
|
|
* TagNode or null if there is no parents and this node isn't a TagNode. |
|
87
|
|
|
* |
|
88
|
|
|
* @return TagNode |
|
89
|
|
|
*/ |
|
90
|
142 |
|
public function getRoot(): TagNode |
|
91
|
|
|
{ |
|
92
|
142 |
|
return $this->root; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param int $id |
|
97
|
|
|
* @return Node[] |
|
98
|
|
|
*/ |
|
99
|
|
|
abstract public function getMinimalDeletedSet(int $id): array; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Descent the ancestors list for both nodes stopping either at the first no-match case or when either of the lists |
|
103
|
|
|
* is exhausted. |
|
104
|
|
|
* |
|
105
|
|
|
* @param Node $other |
|
106
|
|
|
* @return LastCommonParentResult |
|
107
|
|
|
*/ |
|
108
|
20 |
|
public function getLastCommonParent(Node $other): LastCommonParentResult |
|
109
|
|
|
{ |
|
110
|
20 |
|
$result = new LastCommonParentResult(); |
|
111
|
|
|
|
|
112
|
|
|
// Note: these lists are never null, but sometimes are empty. |
|
113
|
20 |
|
$myParents = $this->getParentTree(); |
|
114
|
20 |
|
$otherParents = $other->getParentTree(); |
|
115
|
|
|
|
|
116
|
20 |
|
$myParentsCount = \count($myParents); |
|
117
|
20 |
|
$otherParentsCount = \count($otherParents); |
|
118
|
|
|
|
|
119
|
20 |
|
$i = 1; |
|
120
|
20 |
|
$isSame = true; |
|
121
|
|
|
|
|
122
|
20 |
|
while ($isSame && $i < $myParentsCount && $i < $otherParentsCount) { |
|
123
|
18 |
|
if (!$myParents[$i]->isSameTag($otherParents[$i])) { |
|
124
|
1 |
|
$isSame = false; |
|
125
|
|
|
} else { |
|
126
|
|
|
// After the while, the index $i-1 must be the last common parent. |
|
127
|
18 |
|
$i++; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
20 |
|
$result->setLastCommonParentDepth($i - 1); |
|
132
|
20 |
|
$result->setLastCommonParent($myParents[$i - 1]); |
|
133
|
|
|
|
|
134
|
20 |
|
if (!$isSame) { |
|
135
|
|
|
// Found different parent. |
|
136
|
1 |
|
$result->setIndexInLastCommonParent($myParents[$i - 1]->getIndexOf($myParents[$i])); |
|
137
|
1 |
|
$result->setSplittingNeeded(); |
|
138
|
20 |
|
} elseif ($myParentsCount < $otherParentsCount) { |
|
139
|
|
|
// Current node is not so deeply nested. |
|
140
|
4 |
|
$result->setIndexInLastCommonParent($myParents[$i - 1]->getIndexOf($this)); |
|
141
|
19 |
|
} elseif ($myParentsCount > $otherParentsCount) { |
|
142
|
|
|
// All tags matched but there are tags left in this tree - other node is not so deeply nested. |
|
143
|
5 |
|
$result->setIndexInLastCommonParent($myParents[$i - 1]->getIndexOf($myParents[$i])); |
|
144
|
5 |
|
$result->setSplittingNeeded(); |
|
145
|
|
|
} else { |
|
146
|
|
|
// All tags matched until the very last one in both trees or there were not tags besides the BODY. |
|
147
|
17 |
|
$result->setIndexInLastCommonParent($myParents[$i - 1]->getIndexOf($this)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
20 |
|
return $result; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Changes the $parent field of this node. Does NOT append/remove itself from the previous or the new parent |
|
155
|
|
|
* children collection. |
|
156
|
|
|
* |
|
157
|
|
|
* @param TagNode|null $parent |
|
158
|
|
|
*/ |
|
159
|
53 |
|
public function setParent(?TagNode $parent): void |
|
160
|
|
|
{ |
|
161
|
53 |
|
$this->parent = $parent; |
|
162
|
|
|
|
|
163
|
53 |
|
if (null !== $parent) { |
|
164
|
52 |
|
$this->setRoot($parent->getRoot()); |
|
165
|
|
|
} |
|
166
|
53 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param TagNode $root |
|
170
|
|
|
*/ |
|
171
|
53 |
|
protected function setRoot(TagNode $root): void |
|
172
|
|
|
{ |
|
173
|
53 |
|
$this->root = $root; |
|
174
|
53 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Return a deep copy of this node tree. |
|
178
|
|
|
* |
|
179
|
|
|
* @return Node |
|
180
|
|
|
*/ |
|
181
|
|
|
abstract public function copyTree(): Node; |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Return true only if one of the ancestors is a <pre> tag. False otherwise, including the case where this node is |
|
185
|
|
|
* a <pre> tag. |
|
186
|
|
|
* |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
1 |
|
public function inPre(): bool |
|
190
|
|
|
{ |
|
191
|
1 |
|
foreach ($this->getParentTree() as $ancestor) { |
|
192
|
1 |
|
if ($ancestor instanceof TagNode && $ancestor->isPre()) { |
|
193
|
1 |
|
return true; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
1 |
|
return false; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return bool |
|
202
|
|
|
*/ |
|
203
|
58 |
|
public function isWhiteBefore(): bool |
|
204
|
|
|
{ |
|
205
|
58 |
|
return $this->whiteBefore; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param bool $value |
|
210
|
|
|
*/ |
|
211
|
62 |
|
public function setWhiteBefore(bool $value): void |
|
212
|
|
|
{ |
|
213
|
62 |
|
$this->whiteBefore = $value; |
|
214
|
62 |
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return bool |
|
218
|
|
|
*/ |
|
219
|
61 |
|
public function isWhiteAfter(): bool |
|
220
|
|
|
{ |
|
221
|
61 |
|
return $this->whiteAfter; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param bool $value |
|
226
|
|
|
*/ |
|
227
|
53 |
|
public function setWhiteAfter(bool $value): void |
|
228
|
|
|
{ |
|
229
|
53 |
|
$this->whiteAfter = $value; |
|
230
|
53 |
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @return Node |
|
234
|
|
|
*/ |
|
235
|
|
|
abstract public function getLeftMostChild(): Node; |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return Node |
|
239
|
|
|
*/ |
|
240
|
|
|
abstract public function getRightMostChild(): Node; |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
|
|
abstract public function __toString(): string; |
|
246
|
|
|
} |
|
247
|
|
|
|