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(vue-template-compiler): allow comments on the root node in templates (fix #9407) #9408

Merged
merged 1 commit into from
Feb 4, 2019
Merged
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
22 changes: 13 additions & 9 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,20 @@ export function parse (
}
},
comment (text: string, start, end) {
const child: ASTText = {
type: 3,
text,
isComment: true
}
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
child.start = start
child.end = end
// adding anyting as a sibling to the root node is forbidden
// comments should still be allowed, but ignored
if (currentParent) {
const child: ASTText = {
type: 3,
text,
isComment: true
}
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
child.start = start
child.end = end
}
currentParent.children.push(child)
}
currentParent.children.push(child)
}
})
return root
Expand Down
12 changes: 11 additions & 1 deletion test/unit/modules/compiler/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,16 @@ describe('parser', () => {
expect(ast.children[1].text).toBe('comment here')
})

// #9407
it('should parse templates with comments anywhere', () => {
const options = extend({
comments: true
}, baseOptions)
const ast = parse(`<!--comment here--><div>123</div>`, options)
expect(ast.tag).toBe('div')
expect(ast.children.length).toBe(1)
})

// #8103
it('should allow CRLFs in string interpolations', () => {
const ast = parse(`<p>{{\r\nmsg\r\n}}</p>`, baseOptions)
Expand All @@ -797,7 +807,7 @@ describe('parser', () => {
preserveWhitespace: false
}, baseOptions)

const ast = parse('<p>\n Welcome to <b>Vue.js</b> <i>world</i> \n <span>.\n Have fun!\n</span></p>', options)
const ast = parse('<p>\n Welcome to <b>Vue.js</b> <i>world</i> \n <span>.\n Have fun!\n</span></p>', options)
expect(ast.tag).toBe('p')
expect(ast.children.length).toBe(4)
expect(ast.children[0].type).toBe(3)
Expand Down