fix(compiler-core): consistently remove comment nodes for pre tags in production
close #2217
This commit is contained in:
parent
4d20ac8173
commit
f4119249f2
@ -384,6 +384,25 @@ describe('compiler: parse', () => {
|
|||||||
expect(astNoComment.children).toHaveLength(0)
|
expect(astNoComment.children).toHaveLength(0)
|
||||||
expect(astWithComments.children).toHaveLength(1)
|
expect(astWithComments.children).toHaveLength(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// #2217
|
||||||
|
test('comments in the <pre> tag should be removed in production mode', () => {
|
||||||
|
__DEV__ = false
|
||||||
|
const rawText = `<p/><!-- foo --><p/>`
|
||||||
|
const ast = baseParse(`<pre>${rawText}</pre>`)
|
||||||
|
__DEV__ = true
|
||||||
|
|
||||||
|
expect((ast.children[0] as ElementNode).children).toMatchObject([
|
||||||
|
{
|
||||||
|
type: NodeTypes.ELEMENT,
|
||||||
|
tag: 'p'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: NodeTypes.ELEMENT,
|
||||||
|
tag: 'p'
|
||||||
|
}
|
||||||
|
])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Element', () => {
|
describe('Element', () => {
|
||||||
|
@ -198,10 +198,9 @@ function parseChildren(
|
|||||||
// (same as v2 whitespace: 'condense')
|
// (same as v2 whitespace: 'condense')
|
||||||
let removedWhitespace = false
|
let removedWhitespace = false
|
||||||
if (mode !== TextModes.RAWTEXT) {
|
if (mode !== TextModes.RAWTEXT) {
|
||||||
if (!context.inPre) {
|
|
||||||
for (let i = 0; i < nodes.length; i++) {
|
for (let i = 0; i < nodes.length; i++) {
|
||||||
const node = nodes[i]
|
const node = nodes[i]
|
||||||
if (node.type === NodeTypes.TEXT) {
|
if (!context.inPre && node.type === NodeTypes.TEXT) {
|
||||||
if (!/[^\t\r\n\f ]/.test(node.content)) {
|
if (!/[^\t\r\n\f ]/.test(node.content)) {
|
||||||
const prev = nodes[i - 1]
|
const prev = nodes[i - 1]
|
||||||
const next = nodes[i + 1]
|
const next = nodes[i + 1]
|
||||||
@ -229,17 +228,18 @@ function parseChildren(
|
|||||||
} else {
|
} else {
|
||||||
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
|
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
|
||||||
}
|
}
|
||||||
} else if (
|
}
|
||||||
|
// also remove comment nodes in prod by default
|
||||||
|
if (
|
||||||
!__DEV__ &&
|
!__DEV__ &&
|
||||||
node.type === NodeTypes.COMMENT &&
|
node.type === NodeTypes.COMMENT &&
|
||||||
!context.options.comments
|
!context.options.comments
|
||||||
) {
|
) {
|
||||||
// remove comment nodes in prod by default
|
|
||||||
removedWhitespace = true
|
removedWhitespace = true
|
||||||
nodes[i] = null as any
|
nodes[i] = null as any
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (parent && context.options.isPreTag(parent.tag)) {
|
if (context.inPre && parent && context.options.isPreTag(parent.tag)) {
|
||||||
// remove leading newline per html spec
|
// remove leading newline per html spec
|
||||||
// https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
|
// https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
|
||||||
const first = nodes[0]
|
const first = nodes[0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user