Skip to content

Commit 6f09904

Browse files
committed
added options to skip innerhtml changes of sibling text nodes and changes optiosn argument
1 parent 20ddc23 commit 6f09904

File tree

5 files changed

+68
-37
lines changed

5 files changed

+68
-37
lines changed

README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,37 @@ if (result) {
8282

8383
diffDOM does not include merging for changes to text nodes. However, it includes hooks so that you can add more advanced handling. Simple overwrite the textDiff function of the diffDOM instance. The functions TEXTDIFF and TEXTPATCH need to be defined in the code:
8484
```
85-
dd = new diffDOM();
86-
87-
dd.textDiff = function (node, currentValue, expectedValue, newValue) {
88-
if (currentValue===expectedValue) {
89-
// The text node contains the text we expect it to contain, so we simple change the text of it to the new value.
90-
node.data = newValue;
91-
} else {
92-
// The text node currently does not contain what we expected it to contain, so we need to merge.
93-
difference = TEXTDIFF(expectedValue, currentValue);
94-
node.data = TEXTPATCH(newValue, difference);
85+
dd = new diffDOM({
86+
textDiff: function (node, currentValue, expectedValue, newValue) {
87+
if (currentValue===expectedValue) {
88+
// The text node contains the text we expect it to contain, so we simple change the text of it to the new value.
89+
node.data = newValue;
90+
} else {
91+
// The text node currently does not contain what we expected it to contain, so we need to merge.
92+
difference = TEXTDIFF(expectedValue, currentValue);
93+
node.data = TEXTPATCH(newValue, difference);
94+
}
95+
return true;
9596
}
96-
return true;
97-
};
97+
});
9898
```
9999

100100
#### Debugging
101101

102102
For debugging you might want to set a max number of diff changes between two elements before diffDOM gives up. To allow for a maximum of 500 differences between elements when diffing, initialize diffDOM like this:
103103
```
104-
dd = new diffDOM(true, 500);
104+
dd = new diffDOM({
105+
debug: true,
106+
diffcap: 500
107+
});
105108
```
106109

107110
#### Disable value diff detection
108111

109112
For forms that have been filled out by a user in ways that have changed which value is associated with an input field or which options are checked/selected without
110113
the DOM having been updated, the values are diffed. For use cases in which no changes have been made to any of the form values, one may choose to skip diffing the values. To do this, hand `false` as a third configuration option to diffDOM:
111114
```
112-
dd = new diffDOM(false, 0, false);
115+
dd = new diffDOM({
116+
valueDiffing: false
117+
});
113118
```

diffDOM.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,34 @@
455455
}
456456
};
457457

458-
var diffDOM = function(debug, diffcap, valueDiffing) {
459-
if (typeof valueDiffing === 'undefined') {
460-
valueDiffing = true;
458+
var diffDOM = function(options){
459+
460+
var defaults = {
461+
debug: false,
462+
diffcap: 10,
463+
valueDiffing: true, // Whether to take into consideration the values of forms that differ from auto assigned values (when a user fills out a form).
464+
siblingTextNodes: true, // Whether to take into consideration sibling text nodes.
465+
// diffing text elements can be overwritten for use with diff_match_patch and alike
466+
// syntax: textDiff: function (node, currentValue, expectedValue, newValue)
467+
textDiff: function() {
468+
arguments[0].data = arguments[3];
469+
return;
470+
}
471+
}, i;
472+
473+
if (typeof options == "undefined") {
474+
options = {};
461475
}
462-
if (typeof debug === 'undefined') {
463-
debug = false;
464-
} else {
476+
477+
for (i in defaults) {
478+
if (typeof options[i] == "undefined") {
479+
this[i] = defaults[i];
480+
} else {
481+
this[i] = options[i];
482+
}
483+
}
484+
485+
if (this.debug) {
465486
ADD_ATTRIBUTE = "add attribute";
466487
MODIFY_ATTRIBUTE = "modify attribute";
467488
REMOVE_ATTRIBUTE = "remove attribute";
@@ -494,13 +515,6 @@
494515
SELECTED = "selected";
495516
}
496517

497-
498-
if (typeof diffcap === 'undefined') {
499-
diffcap = 10;
500-
}
501-
this.debug = debug;
502-
this.diffcap = diffcap;
503-
this.valueDiffing = valueDiffing;
504518
};
505519
diffDOM.prototype = {
506520

@@ -563,6 +577,7 @@
563577
return false;
564578
},
565579
findOuterDiff: function(t1, t2, route) {
580+
566581
var k, diffs = [];
567582

568583
if (t1.nodeName !== t2.nodeName) {
@@ -639,6 +654,13 @@
639654
return diffs;
640655
},
641656
findInnerDiff: function(t1, t2, route) {
657+
658+
if (!this.siblingTextNodes && !this.valueDiffing) {
659+
if (t1.innerHTML === t2.innerHTML) {
660+
return [];
661+
}
662+
}
663+
642664
var subtrees = markSubTrees(t1, t2),
643665
mappings = subtrees.length,
644666
diff, diffs, i, last, e1, e2,
@@ -885,12 +907,6 @@
885907
}
886908
return node;
887909
},
888-
// diffing text elements can be overwritten for use with diff_match_patch and alike
889-
// syntax: textDiff: function (node, currentValue, expectedValue, newValue)
890-
textDiff: function() {
891-
arguments[0].data = arguments[3];
892-
return;
893-
},
894910
applyDiff: function(tree, diff) {
895911
var node = this.getFromRoute(tree, diff[ROUTE]),
896912
newNode, reference, route, group, from, to, child, c, i;

tests/basic.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ <h1>Test for diffDOM</h1>
428428
</div>
429429
<div><span></span>hallo<span></span>
430430
</div>
431-
431+
432432
<div><select><option></option><option>A</option></select></div>
433433
<div><select><option>A</option><option></option></select></div>
434434

@@ -506,7 +506,10 @@ <h1>Test for diffDOM</h1>
506506
document.body.lastChild.appendChild(par);
507507
}
508508

509-
var dd = new diffDOM(true, 500),
509+
var dd = new diffDOM({
510+
debug: true,
511+
diffcap: 500
512+
}),
510513
tl = new TraceLogger(dd),
511514
divNodeList, divs = [],
512515
diffs, t1, i;

tests/extent.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ <h2>Different</h2>
121121
return cost;
122122
}
123123

124-
var dd = new diffDOM(true, 500),
124+
var dd = new diffDOM({
125+
debug: true,
126+
diffcap: 500
127+
}),
125128
tl = new TraceLogger(dd),
126129
divNodeList, divs = [],
127130
diffs, t1, i;

tests/form.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ <h1>Form element test for diffDOM</h1>
111111
print("diff operations for " + elTexts[0] + " → " + elTexts[1]);
112112
}
113113

114-
var dd = new diffDOM(true, 700, true),
114+
var dd = new diffDOM({
115+
debug: true,
116+
diffcap: 700,
117+
valueDiffing: true
118+
}),
115119
tl = new TraceLogger(dd), elements, first, second, third, fourth, theDiff;
116120

117121
// Textarea

0 commit comments

Comments
 (0)