Skip to content

Commit af3d03e

Browse files
committed
proper fix
1 parent 2ea4c02 commit af3d03e

File tree

8 files changed

+57
-39
lines changed

8 files changed

+57
-39
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import {
66
regex_ends_with_whitespaces,
77
regex_not_whitespace,
8+
regex_starts_with_newline,
89
regex_starts_with_whitespaces
910
} from '../patterns.js';
1011
import * as b from '../../utils/builders.js';
@@ -270,19 +271,19 @@ export function clean_nodes(
270271

271272
var first = trimmed[0];
272273

273-
// initial newline inside a `<pre>` is disregarded
274-
if (
275-
parent.type === 'RegularElement' &&
276-
parent.name === 'pre' &&
277-
first.type === 'Text' &&
278-
first.data[0] === '\n'
279-
) {
280-
first.data = first.data.slice(1);
281-
first.raw = first.raw.slice(1);
282-
283-
if (first.data === '') {
284-
trimmed.shift();
285-
first = trimmed[0];
274+
// initial newline inside a `<pre>` is disregarded, if not followed by another newline
275+
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+
}
286287
}
287288
}
288289

packages/svelte/tests/hydration/samples/pre-first-node-backslash-n/_config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/svelte/tests/hydration/samples/pre-first-node-backslash-n/_expected.html

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/svelte/tests/hydration/samples/pre-first-node-backslash-n/main.svelte

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
// A note about _expected.html: It is different from body.html because we're
4+
// testing against target.innerHTML which already removed the redundant first newline
5+
export default test({});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--[--><pre>static content no line</pre> <pre> static content ignored line
2+
</pre> <pre>
3+
static content relevant line
4+
</pre> <pre><div><span></span></div>
5+
</pre> <pre>
6+
<div><span></span></div>
7+
</pre><!--]-->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
let name = $state('');
3+
</script>
4+
5+
<pre>static content no line</pre>
6+
7+
<pre>
8+
static content ignored line
9+
</pre>
10+
11+
<pre>
12+
13+
static content relevant line
14+
</pre>
15+
16+
<pre>
17+
<div><span>{name}</span></div>
18+
</pre>
19+
20+
<pre>
21+
22+
<div><span>{name}</span></div>
23+
</pre>

packages/svelte/tests/runtime-legacy/samples/pre-tag/_config.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@ import { test } from '../../test';
22

33
export default test({
44
mode: ['client', 'server'], // output is correct, but test suite chokes on the extra ssr comment which is harmless
5-
withoutNormalizeHtml: true,
6-
html: get_html(false),
7-
ssrHtml: get_html(true)
8-
});
9-
10-
/** @param {boolean} ssr */
11-
function get_html(ssr) {
12-
// ssr rendered HTML has an extra newline prefixed within `<pre>` tag,
13-
// if the <pre> tag starts with `\n`
14-
// because when browser parses the SSR rendered HTML, it will ignore the 1st '\n' character
15-
return `${ssr ? '<!--[-->' : ''}<pre id="pre"> A
5+
withoutNormalizeHtml: 'only-strip-comments', // because whitespace inside pre tags is significant
6+
// Note how we're testing against target.innerHTML which already removed the redundant first newline
7+
html: `<pre id="pre"> A
168
B
179
<span>
1810
C
@@ -31,7 +23,9 @@ function get_html(ssr) {
3123
</span>
3224
E
3325
F
34-
</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>${' '}
26+
</pre></div> <div id="pre-with-leading-newline"><pre>leading newline</pre> <pre> leading newline and spaces</pre> <pre>
27+
leading newlines</pre></div> <div id="pre-without-leading-newline"><pre>without spaces</pre> <pre> with spaces </pre> <pre>${' '}
3528
newline after leading space</pre></div> <pre id="pre-with-multiple-leading-newlines">
36-
multiple leading newlines</pre>${ssr ? '<!--]-->' : ''}`;
37-
}
29+
30+
multiple leading newlines</pre>`
31+
});

0 commit comments

Comments
 (0)