Skip to content

Commit 4c3ffd3

Browse files
committed
more bundling of diffs in findInnerDiff
1 parent 29cfccc commit 4c3ffd3

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

diffDOM.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@
700700
t1_child_nodes = t1.childNodes ? t1.childNodes : [],
701701
t2_child_nodes = t2.childNodes ? t2.childNodes : [],
702702
childNodesLengthDifference, diffs = [],
703-
i, last, e1, e2;
703+
index = 0, last, e1, e2, i;
704704

705705
if (subtrees.length > 1) {
706706
/* Two or more groups have been identified among the childnodes of t1
@@ -733,44 +733,48 @@
733733
if (e1.nodeName === '#text') {
734734
diffs.push(new Diff({
735735
action: 'removeTextElement',
736-
route: route.concat(i),
736+
route: route.concat(index),
737737
value: e1.data
738738
}));
739-
return diffs;
739+
index -= 1;
740+
} else {
741+
diffs.push(new Diff({
742+
action: 'removeElement',
743+
route: route.concat(index),
744+
element: cloneObj(e1)
745+
}));
746+
index -= 1;
740747
}
741-
diffs.push(new Diff({
742-
action: 'removeElement',
743-
route: route.concat(i),
744-
element: cloneObj(e1)
745-
}));
746-
return diffs;
747-
}
748-
if (e2 && !e1) {
748+
749+
} else if (e2 && !e1) {
749750
if (e2.nodeName === '#text') {
750751
diffs.push(new Diff({
751752
action: 'addTextElement',
752-
route: route.concat(i),
753+
route: route.concat(index),
753754
value: e2.data
754755
}));
755-
return diffs;
756+
} else {
757+
diffs.push(new Diff({
758+
action: 'addElement',
759+
route: route.concat(index),
760+
element: cloneObj(e2)
761+
}));
756762
}
757-
diffs.push(new Diff({
758-
action: 'addElement',
759-
route: route.concat(i),
760-
element: cloneObj(e2)
761-
}));
762-
return diffs;
763763
}
764764
}
765765
/* We are now guaranteed that childNodes e1 and e2 exist,
766766
* and that they can be diffed.
767767
*/
768-
diffs = diffs.concat(this.findNextDiff(e1, e2, route.concat(i)));
768+
/* Diffs in child nodes should not affect the parent node,
769+
* so we let these diffs be submitted together with other
770+
* diffs.
771+
*/
769772

770-
/* Diffs in child nodes should not affect the parent node,
771-
* so we let these diffs be submitted together with other
772-
* diffs.
773-
*/
773+
if (e1 && e2) {
774+
diffs = diffs.concat(this.findNextDiff(e1, e2, route.concat(index)));
775+
}
776+
777+
index += 1;
774778

775779
}
776780

tests/basic.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
<h1>Test for diffDOM</h1>
2424

2525
<!-- Add all divs to be compared here two by two -->
26+
<!--div><img><p></p><i></i><img></div>
27+
<div><img><p>a</p><i></i><img><p></p></div>
28+
29+
<div><p></p><i></i><img></div>
30+
<div><img><p>a</p><i></i><img><p></p></div-->
31+
2632
<div><img><img><p></p><i></i><canvas></div>
2733
<div><p></p><img><img><canvas></div>
2834

0 commit comments

Comments
 (0)