Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-core): comments in the <pre> tag should be removed in production mode #2221

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/compiler-core/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ function parseChildren(
if (first && first.type === NodeTypes.TEXT) {
first.content = first.content.replace(/^\r?\n/, '')
}
if (!__DEV__ && !context.options.comments) {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
if (node.type === NodeTypes.COMMENT) {
removedWhitespace = true
nodes[i] = null as any
}
}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-core/src/transforms/transformText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { isText } from '../utils'
import { CREATE_TEXT } from '../runtimeHelpers'
import { PatchFlags, PatchFlagNames } from '@vue/shared'
import { getStaticType } from './hoistStatic'

// Merge adjacent text nodes and expressions into a single expression
// e.g. <div>abc {{ d }} {{ e }}</div> should have a single expression node as child.
Expand Down Expand Up @@ -78,7 +79,7 @@ export const transformText: NodeTransform = (node, context) => {
callArgs.push(child)
}
// mark dynamic text with flag so it gets patched inside a block
if (!context.ssr && child.type !== NodeTypes.TEXT) {
if (!context.ssr && !getStaticType(child)) {
callArgs.push(
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
)
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler-dom/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ describe('DOM parser', () => {
])
})

// #2217
test('comments in the <pre> tag should be removed in production mode', () => {
__DEV__ = false
const rawText = `<p/><!-- foo --><p/>`
const ast = parse(`<pre>${rawText}</pre>`, parserOptions)
__DEV__ = true

expect((ast.children[0] as ElementNode).children).toMatchObject([
{
type: NodeTypes.ELEMENT,
tag: 'p'
},
{
type: NodeTypes.ELEMENT,
tag: 'p'
}
])
})

// #945
test('&nbsp; should not be condensed', () => {
const nbsp = String.fromCharCode(160)
Expand Down