fix(template-compiler): allow comments on the root node in templates (#9408)

In SFC templates, we are allowed to add comments to the root node. If parsing with comments flag
true, we are not anymore. This ignores the root comments in case they cannot be added.

fix #9407
This commit is contained in:
Barthélémy Ledoux 2019-02-04 15:34:32 -06:00 committed by Evan You
parent b6b42ca8c4
commit 1922e7d4d9
2 changed files with 24 additions and 10 deletions

View File

@ -377,16 +377,20 @@ export function parse (
}
},
comment (text: string, start, end) {
const child: ASTText = {
type: 3,
text,
isComment: true
// 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)
}
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
child.start = start
child.end = end
}
currentParent.children.push(child)
}
})
return root

View File

@ -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)
@ -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)