fix: more robust moving of each item nodes

pull/11254/head
Rich Harris 2 years ago
parent af8e914e01
commit 8e3c99fb26

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: more robust moving of each item nodes

@ -323,7 +323,6 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
if (matched.length < stashed.length) { if (matched.length < stashed.length) {
// more efficient to move later items to the front // more efficient to move later items to the front
var start = stashed[0]; var start = stashed[0];
var local_anchor = start.o;
var j; var j;
prev = start.prev; prev = start.prev;
@ -332,7 +331,7 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
var b = matched[matched.length - 1]; var b = matched[matched.length - 1];
for (j = 0; j < matched.length; j += 1) { for (j = 0; j < matched.length; j += 1) {
move(matched[j], local_anchor); move(matched[j], start, anchor);
} }
for (j = 0; j < stashed.length; j += 1) { for (j = 0; j < stashed.length; j += 1) {
@ -352,7 +351,7 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
} else { } else {
// more efficient to move earlier items to the back // more efficient to move earlier items to the back
seen.delete(item); seen.delete(item);
move(item, current ? current.o : anchor); move(item, current, anchor);
link(item.prev, item.next); link(item.prev, item.next);
link(item, prev.next); link(item, prev.next);
@ -492,21 +491,19 @@ function create_item(open, anchor, prev, next, value, key, index, render_fn, fla
/** /**
* @param {import('#client').EachItem} item * @param {import('#client').EachItem} item
* @param {import('#client').EachItem | null} next
* @param {Text | Element | Comment} anchor * @param {Text | Element | Comment} anchor
*/ */
function move(item, anchor) { function move(item, next, anchor) {
anchor.before(item.o); var end = item.next ? item.next.o : anchor;
var dest = next ? next.o : anchor;
var dom = item.e.dom; var node = /** @type {import('#client').TemplateNode} */ (item.o);
if (dom !== null) { while (node !== end) {
if (is_array(dom)) { var next_node = /** @type {import('#client').TemplateNode} */ (node.nextSibling);
for (var i = 0; i < dom.length; i++) { dest.before(node);
anchor.before(dom[i]); node = next_node;
}
} else {
anchor.before(dom);
}
} }
} }

Loading…
Cancel
Save