Skip to content

Commit bceabb8

Browse files
committed
fix in gap diffing + use terms index1 and index2 for readability
1 parent 2cd9eca commit bceabb8

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

diffDOM.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -797,75 +797,74 @@
797797
shortest = Math.min(gaps1.length, gaps2.length),
798798
destinationDifferent, toGroup,
799799
group, node, similarNode, testI, diffs = [],
800-
index = 0,
801-
i, j;
800+
index1, index2, j;
802801

803802

804-
for (i = 0; i < shortest; i += 1) {
805-
if (gaps1[i] === true) {
806-
node = t1.childNodes[i];
803+
for (index2 = 0, index1 = 0; index2 < shortest; index1 += 1, index2 += 1) {
804+
if (gaps1[index2] === true) {
805+
node = t1.childNodes[index1];
807806
if (node.nodeName === '#text') {
808-
if (t2.childNodes[i].nodeName === '#text' && node.data !== t2.childNodes[i].data) {
809-
testI = i;
807+
if (t2.childNodes[index2].nodeName === '#text' && node.data !== t2.childNodes[index2].data) {
808+
testI = index1;
810809
while (t1.childNodes.length > testI + 1 && t1.childNodes[testI + 1].nodeName === '#text') {
811810
testI += 1;
812-
if (t2.childNodes[i].data === t1.childNodes[testI].data) {
811+
if (t2.childNodes[index2].data === t1.childNodes[testI].data) {
813812
similarNode = true;
814813
break;
815814
}
816815
}
817816
if (!similarNode) {
818817
diffs.push(new Diff({
819818
action: 'modifyTextElement',
820-
route: route.concat(index),
819+
route: route.concat(index2),
821820
oldValue: node.data,
822-
newValue: t2.childNodes[i].data
821+
newValue: t2.childNodes[index2].data
823822
}));
824823
}
825824
}
826825
diffs.push(new Diff({
827826
action: 'removeTextElement',
828-
route: route.concat(index),
827+
route: route.concat(index2),
829828
value: node.data
830829
}));
831-
gaps1.splice(index, 1);
830+
gaps1.splice(index2, 1);
832831
shortest = Math.min(gaps1.length, gaps2.length);
833-
index -= 1;
832+
index2 -= 1;
834833
} else {
835834
diffs.push(new Diff({
836835
action: 'removeElement',
837-
route: route.concat(index),
836+
route: route.concat(index2),
838837
element: cloneObj(node)
839838
}));
840-
gaps1.splice(index, 1);
839+
gaps1.splice(index2, 1);
841840
shortest = Math.min(gaps1.length, gaps2.length);
842-
index -= 1;
841+
index2 -= 1;
843842
}
844843

845-
} else if (gaps2[i] === true) {
846-
node = t2.childNodes[i];
844+
} else if (gaps2[index2] === true) {
845+
node = t2.childNodes[index2];
847846
if (node.nodeName === '#text') {
848847
diffs.push(new Diff({
849848
action: 'addTextElement',
850-
route: route.concat(index),
849+
route: route.concat(index2),
851850
value: node.data
852851
}));
853-
index += 1;
852+
gaps1.splice(index2, 0, true);
854853
} else {
855854
diffs.push(new Diff({
856855
action: 'addElement',
857-
route: route.concat(index),
856+
route: route.concat(index2),
858857
element: cloneObj(node)
859858
}));
860-
index += 1;
859+
gaps1.splice(index2, 0, true);
861860
}
862861

863-
} else if (gaps1[i] !== gaps2[i]) {
862+
} else if (gaps1[index2] !== gaps2[index2]) {
864863
if (diffs.length > 0) {
865864
return diffs;
866865
}
867866
// group relocation
868-
group = subtrees[gaps1[i]];
867+
group = subtrees[gaps1[index2]];
869868
toGroup = Math.min(group.new, (t1.childNodes.length - group.length));
870869
if (toGroup !== group.old) {
871870
// Check whether destination nodes are different than originating ones.
@@ -886,7 +885,6 @@
886885
}
887886
}
888887
}
889-
index += 1;
890888
}
891889
return diffs;
892890
},

tests/basic.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,11 @@ <h1>Test for diffDOM</h1>
574574
objToNode(JSON.parse('{"nn":"DIV","c":[{"nn":"B"},{"nn":"I"},{"t":"1"},{"t":"2"}]}')),
575575
]);
576576

577+
/* divs = [
578+
objToNode(JSON.parse('{"nn":"DIV","a":[["data-id","gAt"]],"c":[{"nn":"P","a":[["data-test","3ttAmk"]],"c":[{"nn":"P","a":[["data-fisher","63"],["class","VmApr"]],"c":[{"nn":"SPAN","a":[["data-fisher","ZnGp7vp"],["class","BbS"],["data-id","ON3d"]]}]},{"nn":"IMG"},{"nn":"B"}]},{"nn":"B","a":[["data-fisher","c91"]],"c":[{"nn":"I","a":[["data-test","1"]]},{"nn":"B","a":[["data-fisher","L7IMPg6"]],"c":[{"nn":"I","a":[["data-fisher","57"],["data-test","4QjWEV"]],"c":[{"nn":"B","c":[{"nn":"IMG"},{"nn":"IMG"}]}]},{"nn":"P","a":[["data-id","SHGK"],["data-test","UyJ"]]},{"t":"f3YsY"}]}]},{"nn":"IMG"},{"nn":"SPAN","a":[["class","jD9"]]},{"nn":"SPAN","a":[["class","HpZ2KsY"],["data-id","WY"]]},{"t":""}]}')),
579+
objToNode(JSON.parse('{"nn":"DIV","a":[["class","Pe0"],["data-fisher","LQ"]],"c":[{"nn":"P","a":[["data-fisher","KEkVN"]],"c":[{"nn":"I","a":[["data-id","0eS"],["data-fisher","YV"],["class","1y"]],"c":[{"nn":"B","a":[["data-fisher","lZR0Zbs"],["data-test","E"]],"c":[{"nn":"I","a":[["data-test","tS"],["data-id","HEY"]]}]}]},{"nn":"P","a":[["data-test","RhB"],["data-fisher","sALzSq0bZ"]],"c":[{"nn":"SPAN","c":[{"nn":"P","c":[{"nn":"SPAN","a":[["class","WbJLr1"],["data-id","xACUb"],["data-fisher","MlfVRw"]]}]},{"nn":"IMG"}]},{"nn":"IMG"}]},{"t":""}]},{"t":""},{"t":"j"}]}')),
580+
].concat(divs);
581+
*/
577582

578583

579584
for (i = 0; i < divs.length; i = i + 2) {

0 commit comments

Comments
 (0)