Skip to content

Commit

Permalink
fix: insert comment before text in an each block, to prevent glued nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 29, 2024
1 parent a24ea0a commit 45f1df3
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-sheep-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: insert comment before text in an each block, to prevent glued nodes
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/phases/3-transform/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<svelte:options preserveWhitespace />

{#each 'abc' as l}
<div>{l}</div>
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#each 'abc' as l}
{l}
{/each}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? ""}, `));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function Each_string_template($$payload) {
for (let $$index = 0; $$index < each_array.length; $$index++) {
const thing = each_array[$$index];

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

$$payload.out += `<!--]-->`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ export default function Inline_module_vars($$anchor) {
$.set_attribute(img, "src", __ENHANCED_IMG_5__);
$.reset(picture);
$.append($$anchor, picture);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const __ENHANCED_IMG_6__ = "__VITE_ASSET__2AM7_y_f__";

export default function Inline_module_vars($$payload) {
$$payload.out += `<picture><source${$.attr("srcset", __ENHANCED_IMG_1__ + " 1440w, " + __ENHANCED_IMG_2__ + " 960w")} type="image/avif"> <source${$.attr("srcset", __ENHANCED_IMG_3__ + " 1440w, " + __ENHANCED_IMG_4__ + " 960w")} type="image/webp"> <source${$.attr("srcset", __ENHANCED_IMG_5__ + " 1440w, " + __ENHANCED_IMG_6__ + " 960w")} type="image/png"> <img${$.attr("src", __ENHANCED_IMG_5__)} alt="production test" width="1440" height="1440"></picture>`;
}
}

0 comments on commit 45f1df3

Please sign in to comment.