Skip to content

Commit 92f65df

Browse files
committed
Improvement to @windyGex's awesome work from preactjs#717. This entirely removes the cases where preact would erroneously invoke appendChild() when it didn't need to. The diff no longer depends on DOM length values 🎉 Big thanks to @e1ectronic for pointing this out and providing a great repro (http://jsfiddle.net/o402afwp/).
1 parent 2ef872d commit 92f65df

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/vdom/diff.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function innerDiffNode(dom, vchildren, context, mountAll, isHydrating) {
170170
len = originalChildren.length,
171171
childrenLen = 0,
172172
vlen = vchildren ? vchildren.length : 0,
173-
j, c, vchild, child;
173+
j, c, f, vchild, child;
174174

175175
// Build up a map of keyed children and an Array of unkeyed children:
176176
if (len!==0) {
@@ -218,17 +218,16 @@ function innerDiffNode(dom, vchildren, context, mountAll, isHydrating) {
218218
// morph the matched/found/created DOM child to match vchild (deep)
219219
child = idiff(child, vchild, context, mountAll);
220220

221-
if (child && child!==dom) {
222-
if (i>len) {
221+
f = originalChildren[i];
222+
if (child && child!==dom && child!==f) {
223+
if (f==null) {
223224
dom.appendChild(child);
224225
}
225-
else if (child!==originalChildren[i]) {
226-
if (child===originalChildren[i+1]) {
227-
removeNode(originalChildren[i]);
228-
}
229-
else {
230-
dom.insertBefore(child, originalChildren[i] || null);
231-
}
226+
else if (child===f.nextSibling) {
227+
removeNode(f);
228+
}
229+
else {
230+
dom.insertBefore(child, f);
232231
}
233232
}
234233
}

0 commit comments

Comments
 (0)