Skip to content

Commit 0203eb3

Browse files
authored
fix: insert comment before text in an each block, to prevent glued nodes (#13073)
1 parent 588d636 commit 0203eb3

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

.changeset/orange-sheep-exist.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: insert comment before text in an each block, to prevent glued nodes

packages/svelte/src/compiler/phases/3-transform/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,11 @@ export function clean_nodes(
287287
!first.attributes.some(
288288
(attribute) => attribute.type === 'Attribute' && attribute.name.startsWith('--')
289289
))),
290-
/** if a component or snippet starts with text, we need to add an anchor comment so that its text node doesn't get fused with its surroundings */
290+
/** if a component/snippet/each block starts with text, we need to add an anchor comment so that its text node doesn't get fused with its surroundings */
291291
is_text_first:
292292
(parent.type === 'Fragment' ||
293293
parent.type === 'SnippetBlock' ||
294+
parent.type === 'EachBlock' ||
294295
parent.type === 'SvelteComponent' ||
295296
parent.type === 'Component' ||
296297
parent.type === 'SvelteSelf') &&
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<svelte:options preserveWhitespace />
2+
3+
{#each 'abc' as l}
4+
<div>{l}</div>
5+
{/each}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{#each 'abc' as l}
2+
{l}
3+
{/each}

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export default function Each_string_template($$anchor) {
66
var node = $.first_child(fragment);
77

88
$.each(node, 0, () => ['foo', 'bar', 'baz'], $.index, ($$anchor, thing) => {
9+
$.next();
10+
911
var text = $.text();
1012

1113
$.template_effect(() => $.set_text(text, `${thing ?? ""}, `));

packages/svelte/tests/snapshot/samples/each-string-template/_expected/server/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function Each_string_template($$payload) {
88
for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
99
const thing = each_array[$$index];
1010

11-
$$payload.out += `${$.escape(thing)}, `;
11+
$$payload.out += `<!---->${$.escape(thing)}, `;
1212
}
1313

1414
$$payload.out += `<!--]-->`;

0 commit comments

Comments
 (0)