Skip to content

Commit 1922e7d

Browse files
elevatebartyyx990803
authored andcommitted
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
1 parent b6b42ca commit 1922e7d

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/compiler/parser/index.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,20 @@ export function parse (
377377
}
378378
},
379379
comment (text: string, start, end) {
380-
const child: ASTText = {
381-
type: 3,
382-
text,
383-
isComment: true
384-
}
385-
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
386-
child.start = start
387-
child.end = end
380+
// adding anyting as a sibling to the root node is forbidden
381+
// comments should still be allowed, but ignored
382+
if (currentParent) {
383+
const child: ASTText = {
384+
type: 3,
385+
text,
386+
isComment: true
387+
}
388+
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
389+
child.start = start
390+
child.end = end
391+
}
392+
currentParent.children.push(child)
388393
}
389-
currentParent.children.push(child)
390394
}
391395
})
392396
return root

test/unit/modules/compiler/parser.spec.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,16 @@ describe('parser', () => {
786786
expect(ast.children[1].text).toBe('comment here')
787787
})
788788

789+
// #9407
790+
it('should parse templates with comments anywhere', () => {
791+
const options = extend({
792+
comments: true
793+
}, baseOptions)
794+
const ast = parse(`<!--comment here--><div>123</div>`, options)
795+
expect(ast.tag).toBe('div')
796+
expect(ast.children.length).toBe(1)
797+
})
798+
789799
// #8103
790800
it('should allow CRLFs in string interpolations', () => {
791801
const ast = parse(`<p>{{\r\nmsg\r\n}}</p>`, baseOptions)
@@ -797,7 +807,7 @@ describe('parser', () => {
797807
preserveWhitespace: false
798808
}, baseOptions)
799809

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

0 commit comments

Comments
 (0)