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

Loading…
Cancel
Save