Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/fifty-llamas-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: simplify `<pre>` cleaning
17 changes: 5 additions & 12 deletions packages/svelte/src/compiler/phases/3-transform/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,12 @@ export function clean_nodes(

var first = trimmed[0];

// initial newline inside a `<pre>` is disregarded, if not followed by another newline
// if first text node inside a <pre> is a single newline, discard it, because otherwise
// the browser will do it for us which could break hydration
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, '');
if (text === tmp) {
first.data = text;
first.raw = first.raw.replace(regex_starts_with_newline, '');
if (first.data === '') {
trimmed.shift();
first = trimmed[0];
}
}
if (first.data === '\n' || first.data === '\r\n') {
trimmed.shift();
first = trimmed[0];
}
}

Expand Down