Skip to content

Commit b1b0667

Browse files
ryansolidbrenelzclaude
authored
fix(web): a markerless hole retags a node moved in from another slot, swap loop included (#4326)
* fix(web): a markerless hole retags a node moved in from another slot * refactor(web): tag in reconcile's insert, one swap loop for both kinds of hole The tag write moves into `insert`, so every place that inserts a node tags it the same way, and the swap loop no longer splits on `marker`. The markerless swap loop never retagged, so a node another slot had claimed and dropped kept that slot's tag after a markerless reverse, and a later removal left it in the DOM. Smaller, and covered by a new test. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1 * reconcile: drop the stale 05_swap1k note from the swap-loop comment Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1 --------- Co-authored-by: Brenley Dueck <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent 2c9fdd9 commit b1b0667

4 files changed

Lines changed: 165 additions & 39 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
A node that moves into a hole with no marker (a sole child, or the last child) no longer keeps the `$$SLOT` tag of the hole it came from. The markerless hole now retags it with its parent, so a later removal from that hole takes the node out of the DOM instead of skipping it as another slot's. Untagged nodes in markerless holes stay untagged, so lists like a `<For>` under `<tbody>` take the same path as before.

‎packages/web/src/client.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,7 +3136,7 @@ function insertExpression(parent, value, current, marker) {
31363136
} else {
31373137
parent.appendChild(value);
31383138
}
3139-
if (marker) value[$$SLOT] = marker;
3139+
if (marker || value[$$SLOT]) value[$$SLOT] = marker || parent;
31403140
} else if (Array.isArray(value)) {
31413141
const currentArray = Array.isArray(current);
31423142
// Commit-time text materialization (normalize left primitives raw): a
@@ -3324,14 +3324,14 @@ function cleanChildren(parent, current, marker, replacement) {
33243324
const el = current[i];
33253325
if (replacement !== el) {
33263326
const tag = el[$$SLOT];
3327-
const owns = el.parentNode === parent && (!tag || tag === marker);
3327+
const owns = el.parentNode === parent && (!tag || tag === (marker || parent));
33283328
if (replacement && !inserted && !i)
33293329
owns ? parent.replaceChild(replacement, el) : parent.insertBefore(replacement, marker);
33303330
else if (owns) el.remove();
33313331
} else inserted = true;
33323332
}
33333333
} else if (replacement) parent.insertBefore(replacement, marker);
3334-
if (replacement && marker) replacement[$$SLOT] = marker;
3334+
if (replacement && (marker || replacement[$$SLOT])) replacement[$$SLOT] = marker || parent;
33353335
}
33363336

33373337
function gatherHydratable(element, root) {

‎packages/web/src/reconcile.ts‎

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ export default function reconcileArrays(parentNode: any, a: any[], b: any[], mar
1818
// detached parent, hence the `isConnected` check on the parent.
1919
let active = parentNode.moveBefore && parentNode.isConnected && document.activeElement;
2020
if (active === document.body) active = null;
21-
const insert = (n, ref) =>
21+
// A markerless hole retags with its parent, so a tag from another slot never sticks.
22+
const slot = marker || parentNode;
23+
const insert = (n, ref) => {
2224
active && n.isConnected && n.contains(active)
2325
? parentNode.moveBefore(n, ref)
2426
: parentNode.insertBefore(n, ref);
27+
if (marker || n[$$SLOT]) n[$$SLOT] = slot;
28+
};
2529
// First mount: skip the diff's setup, which cost spa-navigation mounts ~8%.
2630
if (!a.length) {
27-
for (const n of b) {
28-
insert(n, marker);
29-
if (marker) n[$$SLOT] = marker;
30-
}
31+
for (const n of b) insert(n, marker);
3132
return;
3233
}
3334
// Ownership: an unclaimed node (no `$$SLOT`) is fair game; a tagged node
@@ -37,7 +38,7 @@ export default function reconcileArrays(parentNode: any, a: any[], b: any[], mar
3738
const isLive = n => {
3839
if (!n) return false;
3940
const tag = n[$$SLOT];
40-
return n.parentNode === parentNode && (!tag || tag === marker);
41+
return n.parentNode === parentNode && (!tag || tag === slot);
4142
};
4243
let bLength = b.length,
4344
aEnd = a.length,
@@ -80,11 +81,7 @@ export default function reconcileArrays(parentNode: any, a: any[], b: any[], mar
8081
while (k < bLength && !isLive(b[k])) k++;
8182
const node = k < bLength ? b[k] : after;
8283

83-
while (bStart < bEnd) {
84-
const n = b[bStart++];
85-
insert(n, node);
86-
if (marker) n[$$SLOT] = marker;
87-
}
84+
while (bStart < bEnd) insert(b[bStart++], node);
8885
// remove
8986
} else if (bEnd === bStart) {
9087
while (aStart < aEnd) {
@@ -104,27 +101,16 @@ export default function reconcileArrays(parentNode: any, a: any[], b: any[], mar
104101
(anchor = a[aStart]) === b[bEnd - 1] &&
105102
b[bStart] === a[aEnd - 1] &&
106103
anchor.parentNode === parentNode &&
107-
(!(anchorTag = anchor[$$SLOT]) || anchorTag === marker)
104+
(!(anchorTag = anchor[$$SLOT]) || anchorTag === slot)
108105
) {
109106
// Tightest inner loop in the file; one `insertBefore` per iter plus an
110-
// end-condition probe. Splitting on `marker` avoids a per-iter branch in
111-
// the hot path — js-framework-benchmark `05_swap1k` regresses ~6.5% when
112-
// this is collapsed (validated 2026-05-16 on Chrome headless).
113-
if (marker) {
114-
do {
115-
const n = a[--aEnd];
116-
insert(n, anchor);
117-
n[$$SLOT] = marker;
118-
bStart++;
119-
if (aStart >= aEnd - 1 || bStart >= bEnd) break;
120-
} while (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]);
121-
} else {
122-
do {
123-
insert(a[--aEnd], anchor);
124-
bStart++;
125-
if (aStart >= aEnd - 1 || bStart >= bEnd) break;
126-
} while (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]);
127-
}
107+
// end-condition probe. One loop for both kinds of hole: `insert` tags, and
108+
// a markerless hole must retag here too.
109+
do {
110+
insert(a[--aEnd], anchor);
111+
bStart++;
112+
if (aStart >= aEnd - 1 || bStart >= bEnd) break;
113+
} while (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]);
128114
// fallback to map
129115
} else {
130116
// An old node that is no longer ours holds no position in this region:
@@ -156,11 +142,7 @@ export default function reconcileArrays(parentNode: any, a: any[], b: any[], mar
156142

157143
if (sequence > index - bStart) {
158144
const node = a[aStart];
159-
while (bStart < index) {
160-
const n = b[bStart++];
161-
insert(n, node);
162-
if (marker) n[$$SLOT] = marker;
163-
}
145+
while (bStart < index) insert(b[bStart++], node);
164146
} else {
165147
// `replaceChild` detaches the old node, which is placed again
166148
// later in this pass. While focus is in the list, insert instead
@@ -170,7 +152,7 @@ export default function reconcileArrays(parentNode: any, a: any[], b: any[], mar
170152
active && parentNode.contains(active)
171153
? insert(newNode, node)
172154
: parentNode.replaceChild(newNode, node);
173-
if (marker) newNode[$$SLOT] = marker;
155+
if (marker || newNode[$$SLOT]) newNode[$$SLOT] = slot;
174156
}
175157
} else aStart++;
176158
} else a[aStart++].remove();
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
import { describe, expect, test } from "vitest";
6+
import { createSignal, flush, Show, For, children } from "solid-js";
7+
import { render, insert } from "@solidjs/web";
8+
9+
describe("slot ownership in a markerless hole", () => {
10+
test("resolved children moved from a sibling hole to a sole-child hole are still removed", () => {
11+
const [items, setItems] = createSignal(["a", "b", "c"]);
12+
const [wide, setWide] = createSignal(true);
13+
const Layout = (props: { children: any }) => {
14+
const c = children(() => props.children);
15+
return (
16+
<Show when={wide()} fallback={<div class="stack">{c()}</div>}>
17+
<div class="row">
18+
{c()}
19+
<span>end</span>
20+
</div>
21+
</Show>
22+
);
23+
};
24+
const el = document.createElement("div");
25+
const dispose = render(
26+
() => (
27+
<Layout>
28+
<For each={items()}>{x => <i>{x}</i>}</For>
29+
</Layout>
30+
),
31+
el
32+
);
33+
expect(el.innerHTML).toBe('<div class="row"><i>a</i><i>b</i><i>c</i><span>end</span></div>');
34+
setWide(false);
35+
flush();
36+
expect(el.innerHTML).toBe('<div class="stack"><i>a</i><i>b</i><i>c</i></div>');
37+
setItems(["a", "c"]);
38+
flush();
39+
expect(el.innerHTML).toBe('<div class="stack"><i>a</i><i>c</i></div>');
40+
dispose();
41+
});
42+
43+
test("minimal: node from a marker hole reused in a sole-child hole", () => {
44+
const n1 = document.createElement("i");
45+
n1.textContent = "1";
46+
const n2 = document.createElement("i");
47+
n2.textContent = "2";
48+
const [a, setA] = createSignal<any>([n1, n2]);
49+
const [b, setB] = createSignal<any>([]);
50+
const el = document.createElement("div");
51+
const dispose = render(
52+
() => (
53+
<>
54+
<div>
55+
{a()}
56+
<hr />
57+
</div>
58+
<p>{b()}</p>
59+
</>
60+
),
61+
el
62+
);
63+
setA([]);
64+
setB([n1, n2]);
65+
flush();
66+
expect(el.innerHTML).toBe("<div><hr></div><p><i>1</i><i>2</i></p>");
67+
setB([n2]);
68+
flush();
69+
expect(el.innerHTML).toBe("<div><hr></div><p><i>2</i></p>");
70+
dispose();
71+
});
72+
73+
test("a slot that clears does not remove a node a markerless sibling slot claimed", () => {
74+
const node = document.createElement("div");
75+
const parent = document.createElement("div");
76+
const markerA = parent.appendChild(document.createTextNode(""));
77+
const [aVal, setAVal] = createSignal<any>(node);
78+
const [bVal, setBVal] = createSignal<any>(null);
79+
const dispose = render(() => {
80+
insert(parent, aVal, markerA);
81+
insert(parent, bVal, null);
82+
return parent;
83+
}, document.createElement("div"));
84+
setBVal(node);
85+
setAVal(null);
86+
flush();
87+
expect(node.parentNode).toBe(parent);
88+
dispose();
89+
});
90+
91+
test("a slot that reconciles does not remove nodes a markerless sibling slot claimed", () => {
92+
const [n1, n2, n3] = ["1", "2", "3"].map(t =>
93+
Object.assign(document.createElement("i"), { textContent: t })
94+
);
95+
const parent = document.createElement("div");
96+
const markerA = parent.appendChild(document.createTextNode(""));
97+
const [aVal, setAVal] = createSignal<any>([n1, n2]);
98+
const [bVal, setBVal] = createSignal<any>([]);
99+
const dispose = render(() => {
100+
insert(parent, aVal, markerA);
101+
insert(parent, bVal, null);
102+
return parent;
103+
}, document.createElement("div"));
104+
setBVal([n2]);
105+
setAVal([n3]);
106+
flush();
107+
expect([...parent.children].map(n => n.textContent)).toEqual(["3", "2"]);
108+
expect(n1.parentNode).toBe(null);
109+
dispose();
110+
});
111+
112+
test("a markerless swap retags a node a sibling slot claimed and dropped", () => {
113+
const [n1, n2, n3] = ["1", "2", "3"].map(t =>
114+
Object.assign(document.createElement("i"), { textContent: t })
115+
);
116+
const parent = document.createElement("div");
117+
const markerA = parent.appendChild(document.createTextNode(""));
118+
const [aVal, setAVal] = createSignal<any>([]);
119+
const [bVal, setBVal] = createSignal<any>([n1, n2, n3]);
120+
const dispose = render(() => {
121+
insert(parent, aVal, markerA);
122+
insert(parent, bVal, null);
123+
return parent;
124+
}, document.createElement("div"));
125+
setAVal([n3]);
126+
flush();
127+
// The marker slot drops n3 while the markerless slot reverses, which moves
128+
// n3 back in through the swap loop.
129+
setAVal([]);
130+
setBVal([n3, n2, n1]);
131+
flush();
132+
expect([...parent.children].map(n => n.textContent)).toEqual(["3", "2", "1"]);
133+
setBVal([n2, n1]);
134+
flush();
135+
expect([...parent.children].map(n => n.textContent)).toEqual(["2", "1"]);
136+
expect(n3.parentNode).toBe(null);
137+
dispose();
138+
});
139+
});

0 commit comments

Comments
 (0)