-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVNode.php
More file actions
283 lines (242 loc) · 6.05 KB
/
Copy pathVNode.php
File metadata and controls
283 lines (242 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace Diffyne\VirtualDOM;
/**
* Virtual Node representation of a DOM element.
*/
class VNode
{
/**
* Node type constants.
*/
public const TYPE_ELEMENT = 'element';
public const TYPE_TEXT = 'text';
public const TYPE_COMMENT = 'comment';
/**
* The node type.
*/
public string $type;
/**
* The tag name (for element nodes).
*/
public ?string $tag = null;
/**
* The text content (for text nodes).
*/
public ?string $text = null;
/**
* Node attributes.
*
* @var array<string, mixed>
*/
public array $attributes = [];
/**
* Child nodes.
*
* @var array<int, VNode>
*/
public array $children = [];
/**
* Unique node identifier for tracking.
*/
public ?string $key = null;
/**
* Node path in the tree (for efficient updates).
*
* @var array<int, int>
*/
public array $path = [];
/**
* Create a new VNode instance.
*
* @param array<string, mixed> $attributes
* @param array<int, VNode> $children
*/
public function __construct(
string $type,
?string $tag = null,
?string $text = null,
array $attributes = [],
array $children = []
) {
$this->type = $type;
$this->tag = $tag;
$this->text = $text;
$this->attributes = $attributes;
$this->children = $children;
// Extract key from attributes if present
if (isset($attributes['key'])) {
$this->key = $attributes['key'];
}
}
/**
* Create an element node.
*
* @param array<string, mixed> $attributes
* @param array<int, VNode> $children
*/
public static function element(string $tag, array $attributes = [], array $children = []): self
{
return new self(self::TYPE_ELEMENT, $tag, null, $attributes, $children);
}
/**
* Create a text node.
*/
public static function text(string $text): self
{
return new self(self::TYPE_TEXT, null, $text);
}
/**
* Create a comment node.
*/
public static function comment(string $text): self
{
return new self(self::TYPE_COMMENT, null, $text);
}
/**
* Check if this is an element node.
*/
public function isElement(): bool
{
return $this->type === self::TYPE_ELEMENT;
}
/**
* Check if this is a text node.
*/
public function isText(): bool
{
return $this->type === self::TYPE_TEXT;
}
/**
* Check if this is a comment node.
*/
public function isComment(): bool
{
return $this->type === self::TYPE_COMMENT;
}
/**
* Set the node's path in the tree.
*
* @param array<int, int> $path
*/
public function setPath(array $path): void
{
$this->path = $path;
// Recursively set paths for children
foreach ($this->children as $index => $child) {
$child->setPath([...$path, $index]);
}
}
/**
* Get a child node by index.
*/
public function getChild(int $index): ?VNode
{
return $this->children[$index] ?? null;
}
/**
* Add a child node.
*/
public function appendChild(VNode $child): void
{
$this->children[] = $child;
$child->setPath([...$this->path, count($this->children) - 1]);
}
/**
* Get an attribute value.
*/
public function getAttribute(string $name): mixed
{
return $this->attributes[$name] ?? null;
}
/**
* Set an attribute value.
*/
public function setAttribute(string $name, mixed $value): void
{
$this->attributes[$name] = $value;
}
/**
* Remove an attribute.
*/
public function removeAttribute(string $name): void
{
unset($this->attributes[$name]);
}
/**
* Convert the node to an array representation.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
$data = [
'type' => $this->type,
'path' => $this->path,
];
if ($this->isElement()) {
$data['tag'] = $this->tag;
$data['attributes'] = $this->attributes;
$data['children'] = array_map(fn ($child) => $child->toArray(), $this->children);
} elseif ($this->isText()) {
$data['text'] = $this->text;
} elseif ($this->isComment()) {
$data['text'] = $this->text;
}
if ($this->key !== null) {
$data['key'] = $this->key;
}
return $data;
}
/**
* Convert node to minimal client representation (compact keys, no paths).
*
* @return array<string, mixed>
*/
public function toMinimal(): array
{
if ($this->isElement()) {
$data = ['t' => $this->tag];
if (! empty($this->attributes)) {
$data['a'] = $this->attributes;
}
if (! empty($this->children)) {
$data['c'] = array_map(fn ($child) => $child->toMinimal(), $this->children);
}
if ($this->key !== null) {
$data['k'] = $this->key;
}
return $data;
} elseif ($this->isText()) {
return ['x' => $this->text];
} elseif ($this->isComment()) {
return ['m' => $this->text];
}
return [];
}
/**
* Get a string representation of the node path.
*/
public function getPathString(): string
{
return implode('.', $this->path);
}
/**
* Clone the node deeply.
*/
public function clone(): self
{
$cloned = new self(
$this->type,
$this->tag,
$this->text,
$this->attributes,
[]
);
$cloned->key = $this->key;
$cloned->path = $this->path;
foreach ($this->children as $child) {
$cloned->children[] = $child->clone();
}
return $cloned;
}
}