|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the league/commonmark package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
|
11
|
|
|
* - (c) John MacFarlane |
|
12
|
|
|
* |
|
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
14
|
|
|
* file that was distributed with this source code. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace League\CommonMark\Node; |
|
18
|
|
|
|
|
19
|
|
|
use Dflydev\DotAccessData\Data; |
|
20
|
|
|
|
|
21
|
|
|
abstract class Node |
|
22
|
|
|
{ |
|
23
|
|
|
/** @psalm-readonly */ |
|
24
|
|
|
public Data $data; |
|
25
|
|
|
|
|
26
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
27
|
|
|
protected int $depth = 0; |
|
28
|
|
|
|
|
29
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
30
|
|
|
protected ?Node $parent = null; |
|
31
|
|
|
|
|
32
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
33
|
|
|
protected ?Node $previous = null; |
|
34
|
|
|
|
|
35
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
36
|
|
|
protected ?Node $next = null; |
|
37
|
|
|
|
|
38
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
39
|
|
|
protected ?Node $firstChild = null; |
|
40
|
|
|
|
|
41
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
42
|
|
|
protected ?Node $lastChild = null; |
|
43
|
|
|
|
|
44
|
2544 |
|
public function __construct() |
|
45
|
|
|
{ |
|
46
|
2544 |
|
$this->data = new Data([ |
|
47
|
2544 |
|
'attributes' => [], |
|
48
|
|
|
]); |
|
49
|
2544 |
|
} |
|
50
|
|
|
|
|
51
|
766 |
|
public function previous(): ?Node |
|
52
|
|
|
{ |
|
53
|
766 |
|
return $this->previous; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1402 |
|
public function next(): ?Node |
|
57
|
|
|
{ |
|
58
|
1402 |
|
return $this->next; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1806 |
|
public function parent(): ?Node |
|
62
|
|
|
{ |
|
63
|
1806 |
|
return $this->parent; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2244 |
|
protected function setParent(?Node $node = null): void |
|
67
|
|
|
{ |
|
68
|
2244 |
|
$this->parent = $node; |
|
69
|
2244 |
|
$this->depth = $node === null ? 0 : $node->depth + 1; |
|
70
|
2244 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Inserts the $sibling node after $this |
|
74
|
|
|
*/ |
|
75
|
1766 |
|
public function insertAfter(Node $sibling): void |
|
76
|
|
|
{ |
|
77
|
1766 |
|
$sibling->detach(); |
|
78
|
1766 |
|
$sibling->next = $this->next; |
|
79
|
|
|
|
|
80
|
1766 |
|
if ($sibling->next) { |
|
81
|
620 |
|
$sibling->next->previous = $sibling; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1766 |
|
$sibling->previous = $this; |
|
85
|
1766 |
|
$this->next = $sibling; |
|
86
|
1766 |
|
$sibling->setParent($this->parent); |
|
87
|
|
|
|
|
88
|
1766 |
|
if (! $sibling->next && $sibling->parent) { |
|
89
|
1760 |
|
$sibling->parent->lastChild = $sibling; |
|
90
|
|
|
} |
|
91
|
1766 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Inserts the $sibling node before $this |
|
95
|
|
|
*/ |
|
96
|
118 |
|
public function insertBefore(Node $sibling): void |
|
97
|
|
|
{ |
|
98
|
118 |
|
$sibling->detach(); |
|
99
|
118 |
|
$sibling->previous = $this->previous; |
|
100
|
|
|
|
|
101
|
118 |
|
if ($sibling->previous) { |
|
102
|
32 |
|
$sibling->previous->next = $sibling; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
118 |
|
$sibling->next = $this; |
|
106
|
118 |
|
$this->previous = $sibling; |
|
107
|
118 |
|
$sibling->setParent($this->parent); |
|
108
|
|
|
|
|
109
|
118 |
|
if (! $sibling->previous && $sibling->parent) { |
|
110
|
84 |
|
$sibling->parent->firstChild = $sibling; |
|
111
|
|
|
} |
|
112
|
118 |
|
} |
|
113
|
|
|
|
|
114
|
336 |
|
public function replaceWith(Node $replacement): void |
|
115
|
|
|
{ |
|
116
|
336 |
|
$replacement->detach(); |
|
117
|
336 |
|
$this->insertAfter($replacement); |
|
118
|
336 |
|
$this->detach(); |
|
119
|
336 |
|
} |
|
120
|
|
|
|
|
121
|
2242 |
|
public function detach(): void |
|
122
|
|
|
{ |
|
123
|
2242 |
|
if ($this->previous) { |
|
124
|
984 |
|
$this->previous->next = $this->next; |
|
125
|
2242 |
|
} elseif ($this->parent) { |
|
126
|
556 |
|
$this->parent->firstChild = $this->next; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
2242 |
|
if ($this->next) { |
|
130
|
888 |
|
$this->next->previous = $this->previous; |
|
131
|
2242 |
|
} elseif ($this->parent) { |
|
132
|
896 |
|
$this->parent->lastChild = $this->previous; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2242 |
|
$this->parent = null; |
|
136
|
2242 |
|
$this->next = null; |
|
137
|
2242 |
|
$this->previous = null; |
|
138
|
2242 |
|
$this->depth = 0; |
|
139
|
2242 |
|
} |
|
140
|
|
|
|
|
141
|
188 |
|
public function hasChildren(): bool |
|
142
|
|
|
{ |
|
143
|
188 |
|
return $this->firstChild !== null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
1962 |
|
public function firstChild(): ?Node |
|
147
|
|
|
{ |
|
148
|
1962 |
|
return $this->firstChild; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
1944 |
|
public function lastChild(): ?Node |
|
152
|
|
|
{ |
|
153
|
1944 |
|
return $this->lastChild; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return Node[] |
|
158
|
|
|
*/ |
|
159
|
2174 |
|
public function children(): iterable |
|
160
|
|
|
{ |
|
161
|
2174 |
|
$children = []; |
|
162
|
2174 |
|
for ($current = $this->firstChild; $current !== null; $current = $current->next) { |
|
163
|
2096 |
|
$children[] = $current; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
2174 |
|
return $children; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
2230 |
|
public function appendChild(Node $child): void |
|
170
|
|
|
{ |
|
171
|
2230 |
|
if ($this->lastChild) { |
|
172
|
1754 |
|
$this->lastChild->insertAfter($child); |
|
173
|
|
|
} else { |
|
174
|
2230 |
|
$child->detach(); |
|
175
|
2230 |
|
$child->setParent($this); |
|
176
|
2230 |
|
$this->lastChild = $this->firstChild = $child; |
|
177
|
|
|
} |
|
178
|
2230 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Adds $child as the very first child of $this |
|
182
|
|
|
*/ |
|
183
|
86 |
|
public function prependChild(Node $child): void |
|
184
|
|
|
{ |
|
185
|
86 |
|
if ($this->firstChild) { |
|
186
|
82 |
|
$this->firstChild->insertBefore($child); |
|
187
|
|
|
} else { |
|
188
|
6 |
|
$child->detach(); |
|
189
|
6 |
|
$child->setParent($this); |
|
190
|
6 |
|
$this->lastChild = $this->firstChild = $child; |
|
191
|
|
|
} |
|
192
|
86 |
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Detaches all child nodes of given node |
|
196
|
|
|
*/ |
|
197
|
24 |
|
public function detachChildren(): void |
|
198
|
|
|
{ |
|
199
|
24 |
|
foreach ($this->children() as $children) { |
|
200
|
14 |
|
$children->setParent(null); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
24 |
|
$this->firstChild = $this->lastChild = null; |
|
204
|
24 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Replace all children of given node with collection of another |
|
208
|
|
|
* |
|
209
|
|
|
* @param iterable<Node> $children |
|
210
|
|
|
*/ |
|
211
|
14 |
|
public function replaceChildren(iterable $children): void |
|
212
|
|
|
{ |
|
213
|
14 |
|
$this->detachChildren(); |
|
214
|
14 |
|
foreach ($children as $item) { |
|
215
|
14 |
|
$this->appendChild($item); |
|
216
|
|
|
} |
|
217
|
14 |
|
} |
|
218
|
|
|
|
|
219
|
1642 |
|
public function getDepth(): int |
|
220
|
|
|
{ |
|
221
|
1642 |
|
return $this->depth; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
98 |
|
public function walker(): NodeWalker |
|
225
|
|
|
{ |
|
226
|
98 |
|
return new NodeWalker($this); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
432 |
|
public function iterator(int $flags = 0): NodeIterator |
|
230
|
|
|
{ |
|
231
|
432 |
|
return new NodeIterator($this, $flags); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Clone the current node and its children |
|
236
|
|
|
* |
|
237
|
|
|
* WARNING: This is a recursive function and should not be called on deeply-nested node trees! |
|
238
|
|
|
*/ |
|
239
|
6 |
|
public function __clone() |
|
240
|
|
|
{ |
|
241
|
|
|
// Cloned nodes are detached from their parents, siblings, and children |
|
242
|
6 |
|
$this->parent = null; |
|
243
|
6 |
|
$this->previous = null; |
|
244
|
6 |
|
$this->next = null; |
|
245
|
|
|
// But save a copy of the children since we'll need that in a moment |
|
246
|
6 |
|
$children = $this->children(); |
|
247
|
6 |
|
$this->detachChildren(); |
|
248
|
|
|
|
|
249
|
|
|
// The original children get cloned and re-added |
|
250
|
6 |
|
foreach ($children as $child) { |
|
251
|
6 |
|
$this->appendChild(clone $child); |
|
252
|
|
|
} |
|
253
|
6 |
|
} |
|
254
|
|
|
|
|
255
|
2272 |
|
public static function assertInstanceOf(Node $node): void |
|
256
|
|
|
{ |
|
257
|
2272 |
|
if (! $node instanceof static) { |
|
|
|
|
|
|
258
|
54 |
|
throw new \InvalidArgumentException(\sprintf('Incompatible node type: expected %s, got %s', static::class, \get_class($node))); |
|
259
|
|
|
} |
|
260
|
2218 |
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|