Skip to content

Commit 2bf0dd3

Browse files
committed
bundling of diffs in attemptGroupRelocation
1 parent 4c3ffd3 commit 2bf0dd3

File tree

1 file changed

+57
-43
lines changed

1 file changed

+57
-43
lines changed

diffDOM.js

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

705706
if (subtrees.length > 1) {
706707
/* Two or more groups have been identified among the childnodes of t1
@@ -738,12 +739,12 @@
738739
}));
739740
index -= 1;
740741
} else {
741-
diffs.push(new Diff({
742-
action: 'removeElement',
743-
route: route.concat(index),
744-
element: cloneObj(e1)
745-
}));
746-
index -= 1;
742+
diffs.push(new Diff({
743+
action: 'removeElement',
744+
route: route.concat(index),
745+
element: cloneObj(e1)
746+
}));
747+
index -= 1;
747748
}
748749

749750
} else if (e2 && !e1) {
@@ -754,21 +755,21 @@
754755
value: e2.data
755756
}));
756757
} else {
757-
diffs.push(new Diff({
758-
action: 'addElement',
759-
route: route.concat(index),
760-
element: cloneObj(e2)
761-
}));
758+
diffs.push(new Diff({
759+
action: 'addElement',
760+
route: route.concat(index),
761+
element: cloneObj(e2)
762+
}));
762763
}
763764
}
764765
}
765766
/* We are now guaranteed that childNodes e1 and e2 exist,
766767
* and that they can be diffed.
767768
*/
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-
*/
769+
/* Diffs in child nodes should not affect the parent node,
770+
* so we let these diffs be submitted together with other
771+
* diffs.
772+
*/
772773

773774
if (e1 && e2) {
774775
diffs = diffs.concat(this.findNextDiff(e1, e2, route.concat(index)));
@@ -796,10 +797,11 @@
796797
gaps2 = gapInformation.gaps2,
797798
shortest = Math.min(gaps1.length, gaps2.length),
798799
destinationDifferent, toGroup,
799-
group, node, similarNode, testI,
800+
group, node, similarNode, testI, diffs = [],
801+
index = 0,
800802
i, j;
801803

802-
// group relocation
804+
803805
for (i = 0; i < shortest; i += 1) {
804806
if (gaps1[i] === true) {
805807
node = t1.childNodes[i];
@@ -814,42 +816,53 @@
814816
}
815817
}
816818
if (!similarNode) {
817-
return [new Diff({
819+
diffs.push(new Diff({
818820
action: 'modifyTextElement',
819-
route: route.concat(i),
821+
route: route.concat(index),
820822
oldValue: node.data,
821823
newValue: t2.childNodes[i].data
822-
})];
824+
}));
823825
}
824826
}
825-
return [new Diff({
827+
diffs.push(new Diff({
826828
action: 'removeTextElement',
827-
route: route.concat(i),
829+
route: route.concat(index),
828830
value: node.data
829-
})];
831+
}));
832+
index -= 1;
833+
return diffs;
834+
} else {
835+
diffs.push(new Diff({
836+
action: 'removeElement',
837+
route: route.concat(index),
838+
element: cloneObj(node)
839+
}));
840+
index -= 1;
830841
}
831-
return [new Diff({
832-
action: 'removeElement',
833-
route: route.concat(i),
834-
element: cloneObj(node)
835-
})];
836-
}
837-
if (gaps2[i] === true) {
842+
843+
} else if (gaps2[i] === true) {
838844
node = t2.childNodes[i];
839845
if (node.nodeName === '#text') {
840-
return [new Diff({
846+
diffs.push(new Diff({
841847
action: 'addTextElement',
842-
route: route.concat(i),
848+
route: route.concat(index),
843849
value: node.data
844-
})];
850+
}));
851+
index += 1;
852+
} else {
853+
diffs.push(new Diff({
854+
action: 'addElement',
855+
route: route.concat(index),
856+
element: cloneObj(node)
857+
}));
858+
index += 1;
845859
}
846-
return [new Diff({
847-
action: 'addElement',
848-
route: route.concat(i),
849-
element: cloneObj(node)
850-
})];
851-
}
852-
if (gaps1[i] !== gaps2[i]) {
860+
861+
} else if (gaps1[i] !== gaps2[i]) {
862+
if (diffs.length > 0) {
863+
return diffs;
864+
}
865+
// group relocation
853866
group = subtrees[gaps1[i]];
854867
toGroup = Math.min(group.new, (t1.childNodes.length - group.length));
855868
if (toGroup !== group.old) {
@@ -868,11 +881,12 @@
868881
to: toGroup,
869882
route: route
870883
})];
871-
}
884+
}
872885
}
873886
}
887+
index += 1;
874888
}
875-
return [];
889+
return diffs;
876890
},
877891

878892
findValueDiff: function(t1, t2, route) {

0 commit comments

Comments
 (0)