Skip to content

Commit 36c01ca

Browse files
committed
fix
2 parents a9f2d6f + c03ea47 commit 36c01ca

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
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/client/transform-template/to-functions.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @import { TemplateOperation } from '../types.js' */
22
/** @import { ObjectExpression, Identifier, ArrayExpression, Property, Expression, Literal } from 'estree' */
33
import * as b from '../../../../utils/builders.js';
4-
import { regex_is_valid_identifier } from '../../../patterns.js';
4+
import { regex_is_valid_identifier, regex_starts_with_newline } from '../../../patterns.js';
55
import fix_attribute_casing from './fix-attribute-casing.js';
66

77
/**
@@ -127,6 +127,18 @@ function create_anchor(element, data = '') {
127127
*/
128128
function create_text(element, value) {
129129
if (!element) return b.literal(value);
130+
131+
// TODO this is temporary, but i want the tests to keep passing in the meantime
132+
// @ts-expect-error
133+
const name = element?.properties[0].value.value;
134+
135+
if ((name === 'pre' || name === 'textarea') && regex_starts_with_newline.test(value)) {
136+
// @ts-expect-error
137+
if (!element.properties.find((prop) => prop.key.name === 'c')) {
138+
value = value.replace(regex_starts_with_newline, '');
139+
}
140+
}
141+
130142
const c = get_or_create_prop(element, 'c', b.array([]));
131143
/** @type {ArrayExpression} */ (c.value).elements.push(b.literal(value));
132144
}

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -273,27 +273,12 @@ export function clean_nodes(
273273

274274
var first = trimmed[0];
275275

276-
// initial newline inside a `<pre>` is disregarded, if not followed by another newline
277-
if (
278-
parent.type === 'RegularElement' &&
279-
// we also want to do the replacement on the textarea if we are in functional template mode because createTextNode behave differently
280-
// then template.innerHTML
281-
(parent.name === 'pre' || (is_functional_template_mode && parent.name === 'textarea')) &&
282-
first?.type === 'Text'
283-
) {
284-
const text = first.data.replace(regex_starts_with_newline, '');
285-
if (text !== first.data) {
286-
const tmp = text.replace(regex_starts_with_newline, '');
287-
// do an extra replacement if we are in functional template mode because createTextNode behave differently
288-
// then template.innerHTML
289-
if (text === tmp || is_functional_template_mode) {
290-
first.data = text;
291-
first.raw = first.raw.replace(regex_starts_with_newline, '');
292-
if (first.data === '') {
293-
trimmed.shift();
294-
first = trimmed[0];
295-
}
296-
}
276+
// if first text node inside a <pre> is a single newline, discard it, because otherwise
277+
// the browser will do it for us which could break hydration
278+
if (parent.type === 'RegularElement' && parent.name === 'pre' && first?.type === 'Text') {
279+
if (first.data === '\n' || first.data === '\r\n') {
280+
trimmed.shift();
281+
first = trimmed[0];
297282
}
298283
}
299284

0 commit comments

Comments
 (0)