diff --git a/.changeset/dull-mangos-wave.md b/.changeset/dull-mangos-wave.md new file mode 100644 index 0000000000..18ba4e3c8c --- /dev/null +++ b/.changeset/dull-mangos-wave.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: improve whitespace handling diff --git a/packages/svelte/src/compiler/phases/3-transform/utils.js b/packages/svelte/src/compiler/phases/3-transform/utils.js index 5b3a5e7c85..9c7a559394 100644 --- a/packages/svelte/src/compiler/phases/3-transform/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/utils.js @@ -177,8 +177,9 @@ export function clean_nodes( node.data = node.data.replace(regex_whitespaces_strict, ' '); node.raw = node.raw.replace(regex_whitespaces_strict, ' '); if ( - (last_text === null || !regex_ends_with_whitespaces.test(last_text.data)) && - (!can_remove_entirely || node.data !== ' ') + (last_text === null && !can_remove_entirely) || + node.data !== ' ' || + node.data.charCodeAt(0) === 160 // non-breaking space ) { trimmed.push(node); } diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-whitespace/_config.js b/packages/svelte/tests/runtime-runes/samples/snippet-whitespace/_config.js new file mode 100644 index 0000000000..6c130a0bfb --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-whitespace/_config.js @@ -0,0 +1,8 @@ +import { test } from '../../test'; + +export default test({ + compileOptions: { + dev: true // Render in dev mode to check that the validation error is not thrown + }, + html: `A\nB\nC\nD` +}); diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-whitespace/main.svelte b/packages/svelte/tests/runtime-runes/samples/snippet-whitespace/main.svelte new file mode 100644 index 0000000000..a85a932602 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-whitespace/main.svelte @@ -0,0 +1,5 @@ +A +{#snippet snip()}C{/snippet} +B +{@render snip()} +D