Skip to content

Commit 0382019

Browse files
committed
fix(ssr): fix pre tag windows newline hydration mismatch
fix #6410
1 parent 121eb32 commit 0382019

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

packages/compiler-core/src/parse.ts

+34-27
Original file line numberDiff line numberDiff line change
@@ -257,34 +257,41 @@ function parseChildren(
257257
const shouldCondense = context.options.whitespace !== 'preserve'
258258
for (let i = 0; i < nodes.length; i++) {
259259
const node = nodes[i]
260-
if (!context.inPre && node.type === NodeTypes.TEXT) {
261-
if (!/[^\t\r\n\f ]/.test(node.content)) {
262-
const prev = nodes[i - 1]
263-
const next = nodes[i + 1]
264-
// Remove if:
265-
// - the whitespace is the first or last node, or:
266-
// - (condense mode) the whitespace is adjacent to a comment, or:
267-
// - (condense mode) the whitespace is between two elements AND contains newline
268-
if (
269-
!prev ||
270-
!next ||
271-
(shouldCondense &&
272-
(prev.type === NodeTypes.COMMENT ||
273-
next.type === NodeTypes.COMMENT ||
274-
(prev.type === NodeTypes.ELEMENT &&
275-
next.type === NodeTypes.ELEMENT &&
276-
/[\r\n]/.test(node.content))))
277-
) {
278-
removedWhitespace = true
279-
nodes[i] = null as any
280-
} else {
281-
// Otherwise, the whitespace is condensed into a single space
282-
node.content = ' '
260+
if (node.type === NodeTypes.TEXT) {
261+
if (!context.inPre) {
262+
if (!/[^\t\r\n\f ]/.test(node.content)) {
263+
const prev = nodes[i - 1]
264+
const next = nodes[i + 1]
265+
// Remove if:
266+
// - the whitespace is the first or last node, or:
267+
// - (condense mode) the whitespace is adjacent to a comment, or:
268+
// - (condense mode) the whitespace is between two elements AND contains newline
269+
if (
270+
!prev ||
271+
!next ||
272+
(shouldCondense &&
273+
(prev.type === NodeTypes.COMMENT ||
274+
next.type === NodeTypes.COMMENT ||
275+
(prev.type === NodeTypes.ELEMENT &&
276+
next.type === NodeTypes.ELEMENT &&
277+
/[\r\n]/.test(node.content))))
278+
) {
279+
removedWhitespace = true
280+
nodes[i] = null as any
281+
} else {
282+
// Otherwise, the whitespace is condensed into a single space
283+
node.content = ' '
284+
}
285+
} else if (shouldCondense) {
286+
// in condense mode, consecutive whitespaces in text are condensed
287+
// down to a single space.
288+
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
283289
}
284-
} else if (shouldCondense) {
285-
// in condense mode, consecutive whitespaces in text are condensed
286-
// down to a single space.
287-
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
290+
} else {
291+
// #6410 normalize windows newlines in <pre>:
292+
// in SSR, browsers normalize server-rendered \r\n into a single \n
293+
// in the DOM
294+
node.content = node.content.replace(/\r\n/g, '\n')
288295
}
289296
}
290297
// Remove comment nodes if desired by configuration.

0 commit comments

Comments
 (0)