Skip to content

Commit

Permalink
proper fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Jan 7, 2025
1 parent 2ea4c02 commit af3d03e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 39 deletions.
27 changes: 14 additions & 13 deletions packages/svelte/src/compiler/phases/3-transform/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import {
regex_ends_with_whitespaces,
regex_not_whitespace,
regex_starts_with_newline,
regex_starts_with_whitespaces
} from '../patterns.js';
import * as b from '../../utils/builders.js';
Expand Down Expand Up @@ -270,19 +271,19 @@ 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];
// initial newline inside a `<pre>` is disregarded, if not followed by another newline
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];
}
}
}
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

// A note about _expected.html: It is different from body.html because we're
// testing against target.innerHTML which already removed the redundant first newline
export default test({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--[--><pre>static content no line</pre> <pre> static content ignored line
</pre> <pre>
static content relevant line
</pre> <pre><div><span></span></div>
</pre> <pre>
<div><span></span></div>
</pre><!--]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
let name = $state('');
</script>

<pre>static content no line</pre>

<pre>
static content ignored line
</pre>

<pre>

static content relevant line
</pre>

<pre>
<div><span>{name}</span></div>
</pre>

<pre>

<div><span>{name}</span></div>
</pre>
22 changes: 8 additions & 14 deletions packages/svelte/tests/runtime-legacy/samples/pre-tag/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ import { test } from '../../test';

export default test({
mode: ['client', 'server'], // output is correct, but test suite chokes on the extra ssr comment which is harmless
withoutNormalizeHtml: true,
html: get_html(false),
ssrHtml: get_html(true)
});

/** @param {boolean} ssr */
function get_html(ssr) {
// ssr rendered HTML has an extra newline prefixed within `<pre>` tag,
// if the <pre> tag starts with `\n`
// because when browser parses the SSR rendered HTML, it will ignore the 1st '\n' character
return `${ssr ? '<!--[-->' : ''}<pre id="pre"> A
withoutNormalizeHtml: 'only-strip-comments', // because whitespace inside pre tags is significant
// Note how we're testing against target.innerHTML which already removed the redundant first newline
html: `<pre id="pre"> A
B
<span>
C
Expand All @@ -31,7 +23,9 @@ 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 ? '<!--]-->' : ''}`;
}
multiple leading newlines</pre>`
});

0 comments on commit af3d03e

Please sign in to comment.