From 9881823a671d9c189eaf6089a6b4b8d293b2e23d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9l=C3=A9my=20Ledoux?= Date: Mon, 4 Feb 2019 15:34:32 -0600 Subject: [PATCH] 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 --- src/compiler/parser/index.js | 22 +++++++++++++--------- test/unit/modules/compiler/parser.spec.js | 12 +++++++++++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index ec0e302c68f..2b726e5d2f1 100644 --- a/src/compiler/parser/index.js +++ b/src/compiler/parser/index.js @@ -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 diff --git a/test/unit/modules/compiler/parser.spec.js b/test/unit/modules/compiler/parser.spec.js index e81f30ca479..13f1c0d389d 100644 --- a/test/unit/modules/compiler/parser.spec.js +++ b/test/unit/modules/compiler/parser.spec.js @@ -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(`
123
`, options) + expect(ast.tag).toBe('div') + expect(ast.children.length).toBe(1) + }) + // #8103 it('should allow CRLFs in string interpolations', () => { const ast = parse(`

{{\r\nmsg\r\n}}

`, baseOptions) @@ -797,7 +807,7 @@ describe('parser', () => { preserveWhitespace: false }, baseOptions) - const ast = parse('

\n Welcome to Vue.js world \n .\n Have fun!\n

', options) + const ast = parse('

\n Welcome to Vue.js world \n .\n Have fun!\n

', options) expect(ast.tag).toBe('p') expect(ast.children.length).toBe(4) expect(ast.children[0].type).toBe(3)