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

feat(compiler): take the comments option to fully control whether to keep comment nodes #3395

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
Next Next commit
feat(compiler): take the comments option to fully control whether to …
…keep comment nodes
  • Loading branch information
HcySunYang committed Mar 9, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit aa498e9138f6499bd76ce172e69662ef1ceea6fe
12 changes: 3 additions & 9 deletions packages/compiler-core/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
@@ -377,23 +377,17 @@ describe('compiler: parse', () => {
})

test('comments option', () => {
__DEV__ = false
const astDefaultComment = baseParse('<!--abc-->')
const astNoComment = baseParse('<!--abc-->', { comments: false })
const astWithComments = baseParse('<!--abc-->', { comments: true })
__DEV__ = true
const astNoComment = baseParse('<!--abc-->', { comments: false })

expect(astDefaultComment.children).toHaveLength(0)
expect(astNoComment.children).toHaveLength(0)
expect(astWithComments.children).toHaveLength(1)
expect(astNoComment.children).toHaveLength(0)
})

// #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
const ast = baseParse(`<pre>${rawText}</pre>`, { comments: false })

expect((ast.children[0] as ElementNode).children).toMatchObject([
{
14 changes: 7 additions & 7 deletions packages/compiler-core/src/parse.ts
Original file line number Diff line number Diff line change
@@ -59,7 +59,8 @@ export const defaultParserOptions: MergedParserOptions = {
decodeEntities: (rawText: string): string =>
rawText.replace(decodeRE, (_, p1) => decodeMap[p1]),
onError: defaultOnError,
comments: false
// only keep comment nodes in DEV by default
comments: __DEV__
}

export const enum TextModes {
@@ -101,7 +102,10 @@ function createParserContext(
const options = extend({}, defaultParserOptions)
for (const key in rawOptions) {
// @ts-ignore
options[key] = rawOptions[key] || defaultParserOptions[key]
options[key] =
rawOptions[key] === undefined
? defaultParserOptions[key]
: rawOptions[key]
}
return {
options,
@@ -238,11 +242,7 @@ function parseChildren(
}
}
// also remove comment nodes in prod by default
if (
!__DEV__ &&
node.type === NodeTypes.COMMENT &&
!context.options.comments
) {
if (node.type === NodeTypes.COMMENT && !context.options.comments) {
removedWhitespace = true
nodes[i] = null as any
}