Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove leading newline from <pre> contents #14922

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-radios-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: remove leading newline from `<pre>` contents
16 changes: 16 additions & 0 deletions packages/svelte/src/compiler/phases/3-transform/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ export function clean_nodes(

var first = trimmed[0];

// initial newline inside a `<pre>` is disregarded
if (
parent.type === 'RegularElement' &&
parent.name === 'pre' &&
first.type === 'Text' &&
first.data[0] === '\n'
) {
first.data = first.data.slice(1);
first.raw = first.raw.slice(1);

if (first.data === '') {
trimmed.shift();
first = trimmed[0];
}
}

// Special case: Add a comment if this is a lone script tag. This ensures that our run_scripts logic in template.js
// will always be able to call node.replaceWith() on the script tag in order to make it run. If we don't add this
// and would still call node.replaceWith() on the script tag, it would be a no-op because the script tag has no parent.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from '../../test';

export default test({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--[--><pre><div><span></span></div>
</pre><!--]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let name = $state('');
</script>

<pre>
<div><span>{name}</span></div>
</pre>
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ function get_html(ssr) {
</span>
E
F
</pre></div> <div id="pre-with-leading-newline"><pre>leading newline</pre> <pre> leading newline and spaces</pre> <pre>
leading newlines</pre></div> <div id="pre-without-leading-newline"><pre>without spaces</pre> <pre> with spaces </pre> <pre>${' '}
</pre></div> <div id="pre-with-leading-newline"><pre>leading newline</pre> <pre> leading newline and spaces</pre> <pre>leading newlines</pre></div> <div id="pre-without-leading-newline"><pre>without spaces</pre> <pre> with spaces </pre> <pre>${' '}
newline after leading space</pre></div> <pre id="pre-with-multiple-leading-newlines">

multiple leading newlines</pre>${ssr ? '<!--]-->' : ''}`;
}
Loading