From 9b6e65fbeb61297978a16e8f711ebe2dc97f2b10 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Fri, 10 Jan 2025 14:36:26 +0100 Subject: [PATCH] fix: correctly transform `pre` with no content (#14973) Closes #14971 --- .changeset/spicy-insects-check.md | 5 +++++ packages/svelte/src/compiler/phases/3-transform/utils.js | 2 +- .../tests/runtime-runes/samples/pre-no-content/_config.js | 5 +++++ .../tests/runtime-runes/samples/pre-no-content/main.svelte | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changeset/spicy-insects-check.md create mode 100644 packages/svelte/tests/runtime-runes/samples/pre-no-content/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/pre-no-content/main.svelte diff --git a/.changeset/spicy-insects-check.md b/.changeset/spicy-insects-check.md new file mode 100644 index 0000000000..b998d36400 --- /dev/null +++ b/.changeset/spicy-insects-check.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: correctly transform `pre` with no content diff --git a/packages/svelte/src/compiler/phases/3-transform/utils.js b/packages/svelte/src/compiler/phases/3-transform/utils.js index 62a635de35..46872fbfcf 100644 --- a/packages/svelte/src/compiler/phases/3-transform/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/utils.js @@ -272,7 +272,7 @@ export function clean_nodes( var first = trimmed[0]; // initial newline inside a `
` is disregarded, if not followed by another newline
-	if (parent.type === 'RegularElement' && parent.name === 'pre' && first.type === 'Text') {
+	if (parent.type === 'RegularElement' && parent.name === 'pre' && first?.type === 'Text') {
 		const text = first.data.replace(regex_starts_with_newline, '');
 		if (text !== first.data) {
 			const tmp = text.replace(regex_starts_with_newline, '');
diff --git a/packages/svelte/tests/runtime-runes/samples/pre-no-content/_config.js b/packages/svelte/tests/runtime-runes/samples/pre-no-content/_config.js
new file mode 100644
index 0000000000..cb9d31a69f
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/pre-no-content/_config.js
@@ -0,0 +1,5 @@
+import { test } from '../../test';
+
+export default test({
+	html: `
`
+});
diff --git a/packages/svelte/tests/runtime-runes/samples/pre-no-content/main.svelte b/packages/svelte/tests/runtime-runes/samples/pre-no-content/main.svelte
new file mode 100644
index 0000000000..a4357066a5
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/pre-no-content/main.svelte
@@ -0,0 +1 @@
+

\ No newline at end of file