fix: insert comment before text in an each block, to prevent glued nodes (#13073)

pull/13074/head
Rich Harris 1 year ago committed by GitHub
parent 588d636ad6
commit 0203eb319b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: insert comment before text in an each block, to prevent glued nodes

@ -287,10 +287,11 @@ export function clean_nodes(
!first.attributes.some(
(attribute) => attribute.type === 'Attribute' && attribute.name.startsWith('--')
))),
/** 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 */
/** 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 */
is_text_first:
(parent.type === 'Fragment' ||
parent.type === 'SnippetBlock' ||
parent.type === 'EachBlock' ||
parent.type === 'SvelteComponent' ||
parent.type === 'Component' ||
parent.type === 'SvelteSelf') &&

@ -0,0 +1,5 @@
<svelte:options preserveWhitespace />
{#each 'abc' as l}
<div>{l}</div>
{/each}

@ -6,6 +6,8 @@ export default function Each_string_template($$anchor) {
var node = $.first_child(fragment);
$.each(node, 0, () => ['foo', 'bar', 'baz'], $.index, ($$anchor, thing) => {
$.next();
var text = $.text();
$.template_effect(() => $.set_text(text, `${thing ?? ""}, `));

@ -8,7 +8,7 @@ export default function Each_string_template($$payload) {
for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
const thing = each_array[$$index];
$$payload.out += `${$.escape(thing)}, `;
$$payload.out += `<!---->${$.escape(thing)}, `;
}
$$payload.out += `<!--]-->`;

Loading…
Cancel
Save