File tree Expand file tree Collapse file tree 2 files changed +24
-10
lines changed
test/unit/modules/compiler Expand file tree Collapse file tree 2 files changed +24
-10
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments