Skip to content

Commit 8e3c99f

Browse files
committed
fix: more robust moving of each item nodes
1 parent af8e914 commit 8e3c99f

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

.changeset/brave-carrots-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: more robust moving of each item nodes

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
323323
if (matched.length < stashed.length) {
324324
// more efficient to move later items to the front
325325
var start = stashed[0];
326-
var local_anchor = start.o;
327326
var j;
328327

329328
prev = start.prev;
@@ -332,7 +331,7 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
332331
var b = matched[matched.length - 1];
333332

334333
for (j = 0; j < matched.length; j += 1) {
335-
move(matched[j], local_anchor);
334+
move(matched[j], start, anchor);
336335
}
337336

338337
for (j = 0; j < stashed.length; j += 1) {
@@ -352,7 +351,7 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
352351
} else {
353352
// more efficient to move earlier items to the back
354353
seen.delete(item);
355-
move(item, current ? current.o : anchor);
354+
move(item, current, anchor);
356355

357356
link(item.prev, item.next);
358357
link(item, prev.next);
@@ -492,21 +491,19 @@ function create_item(open, anchor, prev, next, value, key, index, render_fn, fla
492491

493492
/**
494493
* @param {import('#client').EachItem} item
494+
* @param {import('#client').EachItem | null} next
495495
* @param {Text | Element | Comment} anchor
496496
*/
497-
function move(item, anchor) {
498-
anchor.before(item.o);
497+
function move(item, next, anchor) {
498+
var end = item.next ? item.next.o : anchor;
499+
var dest = next ? next.o : anchor;
499500

500-
var dom = item.e.dom;
501+
var node = /** @type {import('#client').TemplateNode} */ (item.o);
501502

502-
if (dom !== null) {
503-
if (is_array(dom)) {
504-
for (var i = 0; i < dom.length; i++) {
505-
anchor.before(dom[i]);
506-
}
507-
} else {
508-
anchor.before(dom);
509-
}
503+
while (node !== end) {
504+
var next_node = /** @type {import('#client').TemplateNode} */ (node.nextSibling);
505+
dest.before(node);
506+
node = next_node;
510507
}
511508
}
512509

0 commit comments

Comments
 (0)