-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiffEngine.php
More file actions
348 lines (293 loc) · 10 KB
/
Copy pathDiffEngine.php
File metadata and controls
348 lines (293 loc) · 10 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
namespace Diffyne\VirtualDOM;
/**
* Diff engine that compares two Virtual DOM trees and generates patches.
*/
class DiffEngine
{
/**
* Patch type constants.
*/
public const PATCH_CREATE = 'create';
public const PATCH_REMOVE = 'remove';
public const PATCH_REPLACE = 'replace';
public const PATCH_UPDATE_TEXT = 'update_text';
public const PATCH_UPDATE_ATTRS = 'update_attrs';
public const PATCH_REORDER = 'reorder';
/**
* Generated patches.
*
* @var array<int, array<string, mixed>>
*/
protected array $patches = [];
/**
* Compare two VNode trees and generate patches.
*
* @param array<int, int> $path
* @return array<int, array<string, mixed>>
*/
public function diff(?VNode $oldNode, ?VNode $newNode, array $path = []): array
{
$this->patches = [];
$this->diffNodes($oldNode, $newNode, $path);
return $this->patches;
}
/**
* Diff two nodes recursively.
*
* @param array<int, int> $path
*/
protected function diffNodes(?VNode $oldNode, ?VNode $newNode, array $path): void
{
// Case 1: New node created
if ($oldNode === null && $newNode !== null) {
$this->addPatch(self::PATCH_CREATE, $path, [
'node' => $newNode->toMinimal(),
]);
return;
}
// Case 2: Node removed
if ($oldNode !== null && $newNode === null) {
$this->addPatch(self::PATCH_REMOVE, $path);
return;
}
// Case 3: Both nodes are null
if ($oldNode === null && $newNode === null) {
return;
}
// Case 4: Node type changed
if ($oldNode->type !== $newNode->type) {
$this->addPatch(self::PATCH_REPLACE, $path, [
'node' => $newNode->toMinimal(),
]);
return;
}
// Case 5: Element tag changed
if ($oldNode->isElement() && $newNode->isElement() && $oldNode->tag !== $newNode->tag) {
$this->addPatch(self::PATCH_REPLACE, $path, [
'node' => $newNode->toMinimal(),
]);
return;
}
// Case 6: Text content changed
if ($oldNode->isText() && $newNode->isText() && $oldNode->text !== $newNode->text) {
$this->addPatch(self::PATCH_UPDATE_TEXT, $path, [
'text' => $newNode->text,
]);
return;
}
// Case 7: Element attributes changed
if ($oldNode->isElement() && $newNode->isElement()) {
$attrChanges = $this->diffAttributes($oldNode->attributes, $newNode->attributes);
if (! empty($attrChanges)) {
$this->addPatch(self::PATCH_UPDATE_ATTRS, $path, $attrChanges);
}
// Diff children
$this->diffChildren($oldNode->children, $newNode->children, $path);
}
}
/**
* Diff attributes between two nodes.
*
* @param array<string, mixed> $oldAttrs
* @param array<string, mixed> $newAttrs
* @return array<string, mixed>
*/
protected function diffAttributes(array $oldAttrs, array $newAttrs): array
{
$changes = [
'set' => [],
'remove' => [],
];
// Find attributes to set or update
foreach ($newAttrs as $key => $value) {
if (! isset($oldAttrs[$key]) || $oldAttrs[$key] !== $value) {
$changes['set'][$key] = $value;
}
}
// Find attributes to remove
foreach ($oldAttrs as $key => $value) {
if (! isset($newAttrs[$key])) {
$changes['remove'][] = $key;
}
}
// Return null if no changes
if (empty($changes['set']) && empty($changes['remove'])) {
return [];
}
return $changes;
}
/**
* Diff children arrays.
*
* @param array<int, VNode> $oldChildren
* @param array<int, VNode> $newChildren
* @param array<int, int> $parentPath
*/
protected function diffChildren(array $oldChildren, array $newChildren, array $parentPath): void
{
$oldCount = count($oldChildren);
$newCount = count($newChildren);
// Check if we can use keyed diffing
$oldKeyed = $this->extractKeyedNodes($oldChildren);
$newKeyed = $this->extractKeyedNodes($newChildren);
// Use keyed diffing if at least one list has keys (even if the other is empty)
if (! empty($oldKeyed) || ! empty($newKeyed)) {
// If old list has keys but new doesn't (or vice versa), still use keyed diffing
// This handles cases where list goes from keyed to empty or vice versa
$this->diffKeyedChildren($oldChildren, $newChildren, $parentPath, $oldKeyed, $newKeyed);
return;
}
// Simple index-based diffing (no keys)
$maxCount = max($oldCount, $newCount);
// First, remove excess old children (in reverse order to avoid index shifting)
if ($oldCount > $newCount) {
for ($i = $oldCount - 1; $i >= $newCount; $i--) {
$this->diffNodes($oldChildren[$i], null, [...$parentPath, $i]);
}
}
// Then diff/create/update remaining children
for ($i = 0; $i < $newCount; $i++) {
$oldChild = $oldChildren[$i] ?? null;
$newChild = $newChildren[$i] ?? null;
$childPath = [...$parentPath, $i];
$this->diffNodes($oldChild, $newChild, $childPath);
}
}
/**
* Diff children using keys for efficient reordering.
*
* @param array<int, VNode> $oldChildren
* @param array<int, VNode> $newChildren
* @param array<int, int> $parentPath
* @param array<string, int> $oldKeyed
* @param array<string, int> $newKeyed
*/
protected function diffKeyedChildren(
array $oldChildren,
array $newChildren,
array $parentPath,
array $oldKeyed,
array $newKeyed
): void {
// First, identify nodes to remove
$toRemove = [];
foreach ($oldKeyed as $key => $position) {
if (! isset($newKeyed[$key])) {
$toRemove[] = $position;
}
}
// Remove in reverse order to avoid index shifting
rsort($toRemove);
foreach ($toRemove as $position) {
$this->diffNodes($oldChildren[$position], null, [...$parentPath, $position]);
}
// Process all new children in order
foreach ($newChildren as $newIndex => $newChild) {
$key = $newChild->key;
$oldChildAtIndex = $oldChildren[$newIndex] ?? null;
if ($key !== null && isset($oldKeyed[$key])) {
// Node exists in old list
$oldPosition = $oldKeyed[$key];
// Check if the key at this position changed (reorder)
if ($oldChildAtIndex && $oldChildAtIndex->key !== $key) {
// Position has different key - replace the node
$this->addPatch(self::PATCH_REPLACE, [...$parentPath, $newIndex], [
'node' => $newChild->toMinimal(),
]);
} else {
// Same position or no old child - just diff content
$this->diffNodes(
$oldChildren[$oldPosition],
$newChild,
[...$parentPath, $newIndex]
);
}
} else {
// New node, create it
$this->diffNodes(null, $newChild, [...$parentPath, $newIndex]);
}
}
}
/**
* Extract keyed nodes from children array.
*
* @param array<int, VNode> $children
* @return array<string, int>
*/
protected function extractKeyedNodes(array $children): array
{
$keyed = [];
foreach ($children as $index => $child) {
if ($child->key !== null) {
$keyed[$child->key] = $index;
}
}
return $keyed;
}
/**
* Add a patch to the collection.
*
* @param array<int, int> $path
* @param array<string, mixed> $data
*/
protected function addPatch(string $type, array $path, array $data = []): void
{
$this->patches[] = [
'type' => $type,
'path' => $path,
'data' => $data,
];
}
/**
* Get all generated patches.
*
* @return array<int, array<string, mixed>>
*/
public function getPatches(): array
{
return $this->patches;
}
/**
* Optimize patches by removing redundant operations.
*
* @param array<int, array<string, mixed>> $patches
* @return array<int, array<string, mixed>>
*/
public function optimizePatches(array $patches): array
{
// Remove patches for nodes that were replaced at a parent level
$replacedPaths = [];
foreach ($patches as $patch) {
if ($patch['type'] === self::PATCH_REPLACE || $patch['type'] === self::PATCH_REMOVE) {
$replacedPaths[] = $patch['path'];
}
}
return array_filter($patches, function ($patch) use ($replacedPaths) {
foreach ($replacedPaths as $replacedPath) {
if ($this->isDescendantPath($patch['path'], $replacedPath)) {
return false;
}
}
return true;
});
}
/**
* Check if a path is a descendant of another path.
*
* @param array<int, int> $childPath
* @param array<int, int> $parentPath
*/
protected function isDescendantPath(array $childPath, array $parentPath): bool
{
if (count($childPath) <= count($parentPath)) {
return false;
}
for ($i = 0; $i < count($parentPath); $i++) {
if ($childPath[$i] !== $parentPath[$i]) {
return false;
}
}
return true;
}
}