Skip to content

Commit be666eb

Browse files
committed
fix(compiler): should only strip leading newline directly in pre tag
1 parent 8444078 commit be666eb

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

packages/compiler-core/src/parse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function parseChildren(
223223
}
224224
}
225225
}
226-
} else {
226+
} else if (parent && context.options.isPreTag(parent.tag)) {
227227
// remove leading newline per html spec
228228
// https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
229229
const first = nodes[0]

packages/compiler-dom/__tests__/parse.spec.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,24 @@ describe('DOM parser', () => {
141141

142142
// #908
143143
test('<pre> tag should remove leading newline', () => {
144-
const rawText = `\nhello`
144+
const rawText = `\nhello<div>\nbye</div>`
145145
const ast = parse(`<pre>${rawText}</pre>`, parserOptions)
146-
expect((ast.children[0] as ElementNode).children[0]).toMatchObject({
147-
type: NodeTypes.TEXT,
148-
content: rawText.slice(1)
149-
})
146+
expect((ast.children[0] as ElementNode).children).toMatchObject([
147+
{
148+
type: NodeTypes.TEXT,
149+
content: `hello`
150+
},
151+
{
152+
type: NodeTypes.ELEMENT,
153+
children: [
154+
{
155+
type: NodeTypes.TEXT,
156+
// should not remove the leading newline for nested elements
157+
content: `\nbye`
158+
}
159+
]
160+
}
161+
])
150162
})
151163
})
152164

0 commit comments

Comments
 (0)