Skip to content

Commit cab84fe

Browse files
committed
fix for gaps based removal than addition
1 parent 8e29a3f commit cab84fe

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

diffDOM.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
var diffcount;
55

6-
var Diff = function(options) {
6+
var Diff = function (options) {
77
var diff = this;
88
Object.keys(options).forEach(function(option) {
99
diff[option] = options[option];
@@ -67,7 +67,7 @@
6767

6868
var elementDescriptors = function(el) {
6969
var output = [];
70-
if (el.nodeName != '#text' && el.nodeName != '#comment') {
70+
if (el.nodeName !== '#text' && el.nodeName !== '#comment') {
7171
output.push(el.nodeName);
7272
if (el.attributes) {
7373
if (el.attributes.class) {
@@ -153,7 +153,7 @@
153153
e1Attributes = Object.keys(e1.attributes);
154154
e2Attributes = Object.keys(e2.attributes);
155155

156-
if (e1Attributes.length != e2Attributes.length) {
156+
if (e1Attributes.length !== e2Attributes.length) {
157157
return false;
158158
}
159159
if (!e1Attributes.every(function(attribute) {
@@ -821,20 +821,25 @@
821821
oldValue: node.data,
822822
newValue: t2.childNodes[i].data
823823
}));
824+
)
824825
}
825826
}
826827
diffs.push(new Diff({
827828
action: 'removeTextElement',
828829
route: route.concat(index),
829830
value: node.data
830831
}));
832+
gaps1.splice(index, 1);
833+
shortest = Math.min(gaps1.length, gaps2.length);
831834
index -= 1;
832835
} else {
833836
diffs.push(new Diff({
834837
action: 'removeElement',
835838
route: route.concat(index),
836839
element: cloneObj(node)
837840
}));
841+
gaps1.splice(index, 1);
842+
shortest = Math.min(gaps1.length, gaps2.length);
838843
index -= 1;
839844
}
840845

tests/basic.html

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

2525
<!-- Add all divs to be compared here two by two -->
26+
27+
2628
<div><img><p></p><i></i><img></div>
2729
<div><img><p>a</p><i></i><img><p></p></div>
2830

@@ -567,6 +569,12 @@ <h1>Test for diffDOM</h1>
567569
objToNode(JSON.parse('{"nn":"DIV","a":[["id","document-editable"]],"c":[{"nn":"DIV","a":[["id","document-title"],["class","editable user-contents"],["title","The title of the document"],["contenteditable","true"]],"c":[{"t":"Hellofglalak"}]},{"nn":"DIV","a":[["id","document-metadata"],["class","user-contents"]]},{"nn":"DIV","a":[["id","document-contents"],["class","editable user-contents"],["contenteditable","true"]],"c":[{"nn":"P","c":[{"t":"I want a new Finisky is a German writer. Finish laundromats. French steel plants kate -- spiegel overrated. Howe"},{"t":"ver, the essential parts are good Danish roses. And Swedish steel. Bitcoin Claustrophobic Visions (BCC). Aftenposten regner, bicycle. It\'s really easy to do. University finals are a discipline of their own. Bortsch ist something I would like to eat during winter. fellxDAGBBloneHONEZDzow. And so thdededeey went to M"},{"nn":"SPAN","a":[["class","comment"],["id","comment-0"],["data-id","0"],["data-user","1"],["data-user-name","johannes"],["data-user-avatar","/static/img/default_avatar.png"],["data-date","1388568971084"],["data-comment","test"]],"c":[{"t":"unichmel"}]},{"t":"lom "},{"nn":"SPAN","a":[["class","citation"],["data-bib-entry","1"],["data-bib-before",""],["data-bib-page","123"],["data-bib-format","autocite"]]},{"t":" jfllfrjc"},{"nn":"SPAN","a":[["class","equation"],["data-equation","\\frac{2}{3}"]]},{"t":" aftfellowen"},{"nn":"SPAN","a":[["class","equation"],["data-equation","x=2*y"]]},{"t":" deAftend"},{"nn":"SPAN","a":[["class","equation"],["data-equation","x=2*y"]]},{"t":" etigeren"}]},{"nn":"P","c":[{"t":"lalalalala"},{"nn":"SPAN","a":[["class","footnote"]],"c":[{"t":"a footnote "},{"nn":"BR"}]},{"t":"dsdfdsfllasdsBerchtesgardener Lichtspiele. Flensburger Nahverkehr.qwedw casdsdwqqw"}]}]}]}')),
568570
]);
569571

572+
divs = divs.concat([
573+
objToNode(JSON.parse('{"nn":"DIV","c":[{"nn":"B"},{"nn":"I"},{"nn":"I"},{"t":"1"}]}')),
574+
objToNode(JSON.parse('{"nn":"DIV","c":[{"nn":"B"},{"nn":"I"},{"t":"1"},{"t":"2"}]}')),
575+
]);
576+
577+
570578

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

tests/random-unlimited.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@
2828
tl = new TraceLogger(dd),
2929
divs, diffs, t1, i, j, availableElements, testElement, textContent, mainDiv = document.createElement('div');
3030

31+
32+
function nodeToObj(node) {
33+
var objNode = {}, i;
34+
35+
if (node.nodeType === 3) {
36+
objNode.t = node.data;
37+
} else if (node.nodeType === 8) {
38+
objNode.co = node.data;
39+
} else {
40+
objNode.nn = node.nodeName;
41+
if (node.attributes && node.attributes.length > 0) {
42+
objNode.a = [];
43+
for (i = 0; i < node.attributes.length; i++) {
44+
objNode.a.push([node.attributes[i].name, node.attributes[i].value]);
45+
}
46+
}
47+
if (node.childNodes && node.childNodes.length > 0) {
48+
objNode.c = [];
49+
for (i = 0; i < node.childNodes.length; i++) {
50+
objNode.c.push(nodeToObj(node.childNodes[i]));
51+
}
52+
}
53+
}
54+
return objNode;
55+
}
56+
3157
function reportDiv() {
3258
document.body.appendChild(document.createElement('div'));
3359
}
@@ -108,6 +134,8 @@
108134
print(diffs);
109135
print(t1.outerHTML);
110136
print(divs[i + 1].outerHTML);
137+
print(JSON.stringify(nodeToObj(divs[i])));
138+
print(JSON.stringify(nodeToObj(divs[i+1])));
111139
throw 'Outputs not matching';
112140
}
113141

@@ -122,6 +150,8 @@
122150
print(diffs);
123151
print(t1.outerHTML);
124152
print(divs[i].outerHTML);
153+
print(JSON.stringify(nodeToObj(divs[i])));
154+
print(JSON.stringify(nodeToObj(divs[i+1])));
125155
throw 'Outputs not matching';
126156
}
127157
//try{

0 commit comments

Comments
 (0)