Skip to content

Commit f411924

Browse files
committed
fix(compiler-core): consistently remove comment nodes for pre tags in production
close #2217
1 parent 4d20ac8 commit f411924

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,25 @@ describe('compiler: parse', () => {
384384
expect(astNoComment.children).toHaveLength(0)
385385
expect(astWithComments.children).toHaveLength(1)
386386
})
387+
388+
// #2217
389+
test('comments in the <pre> tag should be removed in production mode', () => {
390+
__DEV__ = false
391+
const rawText = `<p/><!-- foo --><p/>`
392+
const ast = baseParse(`<pre>${rawText}</pre>`)
393+
__DEV__ = true
394+
395+
expect((ast.children[0] as ElementNode).children).toMatchObject([
396+
{
397+
type: NodeTypes.ELEMENT,
398+
tag: 'p'
399+
},
400+
{
401+
type: NodeTypes.ELEMENT,
402+
tag: 'p'
403+
}
404+
])
405+
})
387406
})
388407

389408
describe('Element', () => {

packages/compiler-core/src/parse.ts

+38-38
Original file line numberDiff line numberDiff line change
@@ -198,48 +198,48 @@ function parseChildren(
198198
// (same as v2 whitespace: 'condense')
199199
let removedWhitespace = false
200200
if (mode !== TextModes.RAWTEXT) {
201-
if (!context.inPre) {
202-
for (let i = 0; i < nodes.length; i++) {
203-
const node = nodes[i]
204-
if (node.type === NodeTypes.TEXT) {
205-
if (!/[^\t\r\n\f ]/.test(node.content)) {
206-
const prev = nodes[i - 1]
207-
const next = nodes[i + 1]
208-
// If:
209-
// - the whitespace is the first or last node, or:
210-
// - the whitespace is adjacent to a comment, or:
211-
// - the whitespace is between two elements AND contains newline
212-
// Then the whitespace is ignored.
213-
if (
214-
!prev ||
215-
!next ||
216-
prev.type === NodeTypes.COMMENT ||
217-
next.type === NodeTypes.COMMENT ||
218-
(prev.type === NodeTypes.ELEMENT &&
219-
next.type === NodeTypes.ELEMENT &&
220-
/[\r\n]/.test(node.content))
221-
) {
222-
removedWhitespace = true
223-
nodes[i] = null as any
224-
} else {
225-
// Otherwise, condensed consecutive whitespace inside the text
226-
// down to a single space
227-
node.content = ' '
228-
}
201+
for (let i = 0; i < nodes.length; i++) {
202+
const node = nodes[i]
203+
if (!context.inPre && node.type === NodeTypes.TEXT) {
204+
if (!/[^\t\r\n\f ]/.test(node.content)) {
205+
const prev = nodes[i - 1]
206+
const next = nodes[i + 1]
207+
// If:
208+
// - the whitespace is the first or last node, or:
209+
// - the whitespace is adjacent to a comment, or:
210+
// - the whitespace is between two elements AND contains newline
211+
// Then the whitespace is ignored.
212+
if (
213+
!prev ||
214+
!next ||
215+
prev.type === NodeTypes.COMMENT ||
216+
next.type === NodeTypes.COMMENT ||
217+
(prev.type === NodeTypes.ELEMENT &&
218+
next.type === NodeTypes.ELEMENT &&
219+
/[\r\n]/.test(node.content))
220+
) {
221+
removedWhitespace = true
222+
nodes[i] = null as any
229223
} else {
230-
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
224+
// Otherwise, condensed consecutive whitespace inside the text
225+
// down to a single space
226+
node.content = ' '
231227
}
232-
} else if (
233-
!__DEV__ &&
234-
node.type === NodeTypes.COMMENT &&
235-
!context.options.comments
236-
) {
237-
// remove comment nodes in prod by default
238-
removedWhitespace = true
239-
nodes[i] = null as any
228+
} else {
229+
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
240230
}
241231
}
242-
} else if (parent && context.options.isPreTag(parent.tag)) {
232+
// also remove comment nodes in prod by default
233+
if (
234+
!__DEV__ &&
235+
node.type === NodeTypes.COMMENT &&
236+
!context.options.comments
237+
) {
238+
removedWhitespace = true
239+
nodes[i] = null as any
240+
}
241+
}
242+
if (context.inPre && parent && context.options.isPreTag(parent.tag)) {
243243
// remove leading newline per html spec
244244
// https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
245245
const first = nodes[0]

0 commit comments

Comments
 (0)