From 7396af3454fd94a416fe9913eb68dd734c7d94ec Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Wed, 10 Dec 2025 20:07:20 +0100 Subject: [PATCH] fix: correctly print `!doctype` during `print` --- .changeset/smooth-comics-shine.md | 5 +++++ packages/svelte/src/compiler/print/index.js | 5 +++-- .../tests/print/samples/html-document/input.svelte | 11 +++++++++++ .../tests/print/samples/html-document/output.svelte | 12 ++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/smooth-comics-shine.md create mode 100644 packages/svelte/tests/print/samples/html-document/input.svelte create mode 100644 packages/svelte/tests/print/samples/html-document/output.svelte diff --git a/.changeset/smooth-comics-shine.md b/.changeset/smooth-comics-shine.md new file mode 100644 index 000000000000..4bf26ed9f1b7 --- /dev/null +++ b/.changeset/smooth-comics-shine.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: correctly print `!doctype` during `print` diff --git a/packages/svelte/src/compiler/print/index.js b/packages/svelte/src/compiler/print/index.js index 8b39bdd739cc..4aed743dbad3 100644 --- a/packages/svelte/src/compiler/print/index.js +++ b/packages/svelte/src/compiler/print/index.js @@ -112,12 +112,13 @@ function base_element(node, context) { } const multiline_attributes = attributes(node.attributes, child_context); - + const is_doctype_node = node.name.toLowerCase() === '!doctype'; const is_self_closing = is_void(node.name) || (node.type === 'Component' && node.fragment.nodes.length === 0); let multiline_content = false; - if (is_self_closing) { + if (is_doctype_node) child_context.write(`>`); + else if (is_self_closing) { child_context.write(`${multiline_attributes ? '' : ' '}/>`); } else { child_context.write('>'); diff --git a/packages/svelte/tests/print/samples/html-document/input.svelte b/packages/svelte/tests/print/samples/html-document/input.svelte new file mode 100644 index 000000000000..8f1b4accabb9 --- /dev/null +++ b/packages/svelte/tests/print/samples/html-document/input.svelte @@ -0,0 +1,11 @@ + + + + + + Svelte App + + +
Hello World
+ + diff --git a/packages/svelte/tests/print/samples/html-document/output.svelte b/packages/svelte/tests/print/samples/html-document/output.svelte new file mode 100644 index 000000000000..765f9ca84b62 --- /dev/null +++ b/packages/svelte/tests/print/samples/html-document/output.svelte @@ -0,0 +1,12 @@ + + + + + + Svelte App + +
Hello World
+