Skip to content

Commit c03ea47

Browse files
authored
chore: simplify <pre> cleaning (#15980)
1 parent 4e72b7d commit c03ea47

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

.changeset/fifty-llamas-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: simplify `<pre>` cleaning

packages/svelte/src/compiler/phases/3-transform/utils.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,12 @@ export function clean_nodes(
271271

272272
var first = trimmed[0];
273273

274-
// initial newline inside a `<pre>` is disregarded, if not followed by another newline
274+
// if first text node inside a <pre> is a single newline, discard it, because otherwise
275+
// the browser will do it for us which could break hydration
275276
if (parent.type === 'RegularElement' && parent.name === 'pre' && first?.type === 'Text') {
276-
const text = first.data.replace(regex_starts_with_newline, '');
277-
if (text !== first.data) {
278-
const tmp = text.replace(regex_starts_with_newline, '');
279-
if (text === tmp) {
280-
first.data = text;
281-
first.raw = first.raw.replace(regex_starts_with_newline, '');
282-
if (first.data === '') {
283-
trimmed.shift();
284-
first = trimmed[0];
285-
}
286-
}
277+
if (first.data === '\n' || first.data === '\r\n') {
278+
trimmed.shift();
279+
first = trimmed[0];
287280
}
288281
}
289282

0 commit comments

Comments
 (0)