Skip to content

Commit 5c2a937

Browse files
committed
try to address long lists of content, #64
1 parent 6e10702 commit 5c2a937

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

diffDOM.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
})(this, function() {
1616
"use strict";
1717

18-
var diffcount;
18+
var diffcount, foundAll = false;
1919

2020
var Diff = function(options) {
2121
var diff = this;
@@ -150,7 +150,6 @@
150150
if (Boolean(e1.childNodes) !== Boolean(e2.childNodes)) {
151151
return false;
152152
}
153-
154153
if (e1.attributes) {
155154
e1Attributes = Object.keys(e1.attributes);
156155
e2Attributes = Object.keys(e2.attributes);
@@ -162,11 +161,11 @@
162161
if (e1.attributes[attribute] !== e2.attributes[attribute]) {
163162
return false;
164163
}
164+
return true;
165165
})) {
166166
return false;
167167
}
168168
}
169-
170169
if (e1.childNodes) {
171170
if (e1.childNodes.length !== e2.childNodes.length) {
172171
return false;
@@ -449,6 +448,8 @@
449448
debug: false,
450449
diffcap: 10, // Limit for how many diffs are accepting when debugging. Inactive when debug is false.
451450
maxDepth: false, // False or a numeral. If set to a numeral, limits the level of depth that the the diff mechanism looks for differences. If false, goes through the entire tree.
451+
maxChildCount: false, // False or a numeral. If set to a numeral, does not try to diff the contents of nodes with more children if there are more than maxChildCountDiffCount differences among child nodes.
452+
maxChildCountDiffCount: 3, // Numeral. See maxChildCount.
452453
valueDiffing: true, // Whether to take into consideration the values of forms that differ from auto assigned values (when a user fills out a form).
453454
// syntax: textDiff: function (node, currentValue, expectedValue, newValue)
454455
textDiff: function() {
@@ -555,17 +556,24 @@
555556
}
556557
}
557558
diffs = this.findNextDiff(t1, t2, []);
559+
558560
if (diffs.length === 0) {
559561
// Last check if the elements really are the same now.
560562
// If not, remove all info about being done and start over.
561-
// Somtimes a node can be marked as done, but the creation of subsequent diffs means that it has to be changed anyway.
563+
// Sometimes a node can be marked as done, but the creation of subsequent diffs means that it has to be changed again.
562564
if (!isEqual(t1, t2)) {
563-
removeDone(t1);
564-
diffs = this.findNextDiff(t1, t2, []);
565+
if (foundAll) {
566+
console.error('Could not find remaining diffs!');
567+
console.log({t1, t2});
568+
} else {
569+
foundAll = true;
570+
removeDone(t1);
571+
diffs = this.findNextDiff(t1, t2, []);
572+
}
565573
}
566574
}
567-
568575
if (diffs.length > 0) {
576+
foundAll = false
569577
this.tracker.add(diffs);
570578
this.applyVirtual(t1, diffs);
571579
}
@@ -632,6 +640,24 @@
632640
];
633641
}
634642

643+
if (this.maxChildCount && t1.childNodes && t2.childNodes && t1.childNodes.length > this.maxChildCount && t2.childNodes.length > this.maxChildCount) {
644+
var childNodesLength = t1.childNodes.length < t2.childNodes.length ? t1.childNodes.length : t2.childNodes.length, childDiffCount = 0, j = 0;
645+
while (childDiffCount < this.maxChildCountDiffCount && j < childNodesLength) {
646+
if (!isEqual(t1.childNodes[j], t2.childNodes[j])) {
647+
childDiffCount++;
648+
}
649+
j++;
650+
}
651+
if (childDiffCount === this.maxChildCountDiffCount) {
652+
return [new Diff()
653+
.setValue(t._const.action, t._const.replaceElement)
654+
.setValue(t._const.oldValue, cloneObj(t1))
655+
.setValue(t._const.newValue, cloneObj(t2))
656+
.setValue(t._const.route, route)
657+
];
658+
}
659+
}
660+
635661
if (t1.data !== t2.data) {
636662
// Comment or text node.
637663
if (t1.nodeName === '#text') {

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ <h2>Tests</h2>
1919

2020
<p>The <a href="tests/modify-value.html">input value modification test</a>, contributed by user matthewwolfe, tests diffing changes to input fields.</p>
2121

22+
<p>The <a href="tests/long-list.html">long list test</a>, contributed by user vatavale, tests diffing changes to long lists with IDs.</p>
23+
2224
<p>The <a href="tests/site-integration.html">site integration test</a>, contributed by user unbug, allows for testing for speed on a 170kb site real-world-like site where it is integrated with jQuery.</p>
2325

2426
<h2>Demo</h2>

0 commit comments

Comments
 (0)