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
|
|
|
use League\CommonMark\Exception\InvalidArgumentException; |
21
|
|
|
|
22
|
|
|
abstract class Node |
23
|
|
|
{ |
24
|
|
|
/** @psalm-readonly */ |
25
|
|
|
public Data $data; |
26
|
|
|
|
27
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
28
|
|
|
protected int $depth = 0; |
29
|
|
|
|
30
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
31
|
|
|
protected ?Node $parent = null; |
32
|
|
|
|
33
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
34
|
|
|
protected ?Node $previous = null; |
35
|
|
|
|
36
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
37
|
|
|
protected ?Node $next = null; |
38
|
|
|
|
39
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
40
|
|
|
protected ?Node $firstChild = null; |
41
|
|
|
|
42
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
43
|
|
|
protected ?Node $lastChild = null; |
44
|
|
|
|
45
|
2812 |
|
public function __construct() |
46
|
|
|
{ |
47
|
2812 |
|
$this->data = new Data([ |
48
|
2812 |
|
'attributes' => [], |
49
|
2812 |
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
918 |
|
public function previous(): ?Node |
53
|
|
|
{ |
54
|
918 |
|
return $this->previous; |
55
|
|
|
} |
56
|
|
|
|
57
|
2348 |
|
public function next(): ?Node |
58
|
|
|
{ |
59
|
2348 |
|
return $this->next; |
60
|
|
|
} |
61
|
|
|
|
62
|
2350 |
|
public function parent(): ?Node |
63
|
|
|
{ |
64
|
2350 |
|
return $this->parent; |
65
|
|
|
} |
66
|
|
|
|
67
|
2468 |
|
protected function setParent(?Node $node = null): void |
68
|
|
|
{ |
69
|
2468 |
|
$this->parent = $node; |
70
|
2468 |
|
$this->depth = $node === null ? 0 : $node->depth + 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Inserts the $sibling node after $this |
75
|
|
|
*/ |
76
|
1946 |
|
public function insertAfter(Node $sibling): void |
77
|
|
|
{ |
78
|
1946 |
|
$sibling->detach(); |
79
|
1946 |
|
$sibling->next = $this->next; |
80
|
|
|
|
81
|
1946 |
|
if ($sibling->next) { |
82
|
710 |
|
$sibling->next->previous = $sibling; |
83
|
|
|
} |
84
|
|
|
|
85
|
1946 |
|
$sibling->previous = $this; |
86
|
1946 |
|
$this->next = $sibling; |
87
|
1946 |
|
$sibling->setParent($this->parent); |
88
|
|
|
|
89
|
1946 |
|
if (! $sibling->next && $sibling->parent) { |
90
|
1940 |
|
$sibling->parent->lastChild = $sibling; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Inserts the $sibling node before $this |
96
|
|
|
*/ |
97
|
140 |
|
public function insertBefore(Node $sibling): void |
98
|
|
|
{ |
99
|
140 |
|
$sibling->detach(); |
100
|
140 |
|
$sibling->previous = $this->previous; |
101
|
|
|
|
102
|
140 |
|
if ($sibling->previous) { |
103
|
46 |
|
$sibling->previous->next = $sibling; |
104
|
|
|
} |
105
|
|
|
|
106
|
140 |
|
$sibling->next = $this; |
107
|
140 |
|
$this->previous = $sibling; |
108
|
140 |
|
$sibling->setParent($this->parent); |
109
|
|
|
|
110
|
140 |
|
if (! $sibling->previous && $sibling->parent) { |
111
|
92 |
|
$sibling->parent->firstChild = $sibling; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
380 |
|
public function replaceWith(Node $replacement): void |
116
|
|
|
{ |
117
|
380 |
|
$replacement->detach(); |
118
|
380 |
|
$this->insertAfter($replacement); |
119
|
380 |
|
$this->detach(); |
120
|
|
|
} |
121
|
|
|
|
122
|
2466 |
|
public function detach(): void |
123
|
|
|
{ |
124
|
2466 |
|
if ($this->previous) { |
125
|
1110 |
|
$this->previous->next = $this->next; |
126
|
2466 |
|
} elseif ($this->parent) { |
127
|
638 |
|
$this->parent->firstChild = $this->next; |
128
|
|
|
} |
129
|
|
|
|
130
|
2466 |
|
if ($this->next) { |
131
|
996 |
|
$this->next->previous = $this->previous; |
132
|
2466 |
|
} elseif ($this->parent) { |
133
|
1010 |
|
$this->parent->lastChild = $this->previous; |
134
|
|
|
} |
135
|
|
|
|
136
|
2466 |
|
$this->parent = null; |
137
|
2466 |
|
$this->next = null; |
138
|
2466 |
|
$this->previous = null; |
139
|
2466 |
|
$this->depth = 0; |
140
|
|
|
} |
141
|
|
|
|
142
|
206 |
|
public function hasChildren(): bool |
143
|
|
|
{ |
144
|
206 |
|
return $this->firstChild !== null; |
145
|
|
|
} |
146
|
|
|
|
147
|
2376 |
|
public function firstChild(): ?Node |
148
|
|
|
{ |
149
|
2376 |
|
return $this->firstChild; |
150
|
|
|
} |
151
|
|
|
|
152
|
2170 |
|
public function lastChild(): ?Node |
153
|
|
|
{ |
154
|
2170 |
|
return $this->lastChild; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return Node[] |
159
|
|
|
*/ |
160
|
2372 |
|
public function children(): iterable |
161
|
|
|
{ |
162
|
2372 |
|
$children = []; |
163
|
2372 |
|
for ($current = $this->firstChild; $current !== null; $current = $current->next) { |
164
|
2296 |
|
$children[] = $current; |
165
|
|
|
} |
166
|
|
|
|
167
|
2372 |
|
return $children; |
168
|
|
|
} |
169
|
|
|
|
170
|
2454 |
|
public function appendChild(Node $child): void |
171
|
|
|
{ |
172
|
2454 |
|
if ($this->lastChild) { |
173
|
1934 |
|
$this->lastChild->insertAfter($child); |
174
|
|
|
} else { |
175
|
2454 |
|
$child->detach(); |
176
|
2454 |
|
$child->setParent($this); |
177
|
2454 |
|
$this->lastChild = $this->firstChild = $child; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Adds $child as the very first child of $this |
183
|
|
|
*/ |
184
|
94 |
|
public function prependChild(Node $child): void |
185
|
|
|
{ |
186
|
94 |
|
if ($this->firstChild) { |
187
|
90 |
|
$this->firstChild->insertBefore($child); |
188
|
|
|
} else { |
189
|
6 |
|
$child->detach(); |
190
|
6 |
|
$child->setParent($this); |
191
|
6 |
|
$this->lastChild = $this->firstChild = $child; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Detaches all child nodes of given node |
197
|
|
|
*/ |
198
|
24 |
|
public function detachChildren(): void |
199
|
|
|
{ |
200
|
24 |
|
foreach ($this->children() as $children) { |
201
|
14 |
|
$children->setParent(null); |
202
|
|
|
} |
203
|
|
|
|
204
|
24 |
|
$this->firstChild = $this->lastChild = null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Replace all children of given node with collection of another |
209
|
|
|
* |
210
|
|
|
* @param iterable<Node> $children |
211
|
|
|
*/ |
212
|
14 |
|
public function replaceChildren(iterable $children): void |
213
|
|
|
{ |
214
|
14 |
|
$this->detachChildren(); |
215
|
14 |
|
foreach ($children as $item) { |
216
|
14 |
|
$this->appendChild($item); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
2304 |
|
public function getDepth(): int |
221
|
|
|
{ |
222
|
2304 |
|
return $this->depth; |
223
|
|
|
} |
224
|
|
|
|
225
|
2314 |
|
public function walker(): NodeWalker |
226
|
|
|
{ |
227
|
2314 |
|
return new NodeWalker($this); |
228
|
|
|
} |
229
|
|
|
|
230
|
470 |
|
public function iterator(int $flags = 0): NodeIterator |
231
|
|
|
{ |
232
|
470 |
|
return new NodeIterator($this, $flags); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Clone the current node and its children |
237
|
|
|
* |
238
|
|
|
* WARNING: This is a recursive function and should not be called on deeply-nested node trees! |
239
|
|
|
*/ |
240
|
6 |
|
public function __clone() |
241
|
|
|
{ |
242
|
|
|
// Cloned nodes are detached from their parents, siblings, and children |
243
|
6 |
|
$this->parent = null; |
244
|
6 |
|
$this->previous = null; |
245
|
6 |
|
$this->next = null; |
246
|
|
|
// But save a copy of the children since we'll need that in a moment |
247
|
6 |
|
$children = $this->children(); |
248
|
6 |
|
$this->detachChildren(); |
249
|
|
|
|
250
|
|
|
// The original children get cloned and re-added |
251
|
6 |
|
foreach ($children as $child) { |
252
|
6 |
|
$this->appendChild(clone $child); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
2484 |
|
public static function assertInstanceOf(Node $node): void |
257
|
|
|
{ |
258
|
2484 |
|
if (! $node instanceof static) { |
|
|
|
|
259
|
54 |
|
throw new InvalidArgumentException(\sprintf('Incompatible node type: expected %s, got %s', static::class, \get_class($node))); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|