From a584ff134703cb340c90fef608646209d585cfb5 Mon Sep 17 00:00:00 2001 From: Manuel <30698007+manuel3108@users.noreply.github.com> Date: Thu, 11 Dec 2025 01:31:58 +0100 Subject: [PATCH] fix: correctly print `!doctype` during `print` (#17341) --- .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 0000000000..4bf26ed9f1 --- /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 8b39bdd739..4aed743dba 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 0000000000..8f1b4accab --- /dev/null +++ b/packages/svelte/tests/print/samples/html-document/input.svelte @@ -0,0 +1,11 @@ + + +
+ + +