File tree 12 files changed +94
-4
lines changed
12 files changed +94
-4
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,9 @@ declare type CompilerOptions = {
21
21
22
22
// runtime user-configurable
23
23
delimiters ? : [ string , string ] ; // template delimiters
24
+
25
+ // allow user kept comments
26
+ comments ? : boolean
24
27
} ;
25
28
26
29
declare type CompiledResult = {
@@ -151,6 +154,7 @@ declare type ASTText = {
151
154
type : 3 ;
152
155
text: string ;
153
156
static ? : boolean ;
157
+ isComment ? : boolean ;
154
158
// 2.4 ssr optimization
155
159
ssrOptimizability ? : number ;
156
160
} ;
Original file line number Diff line number Diff line change @@ -68,6 +68,7 @@ declare type ComponentOptions = {
68
68
name ?: string ;
69
69
extends ?: Class < Component > | Object ;
70
70
delimiters ?: [ string , string ] ;
71
+ comments ?: boolean ;
71
72
72
73
// private
73
74
_isComponent ?: true ;
Original file line number Diff line number Diff line change @@ -423,6 +423,8 @@ function needsNormalization (el: ASTElement): boolean {
423
423
function genNode ( node : ASTNode , state : CodegenState ) : string {
424
424
if ( node . type === 1 ) {
425
425
return genElement ( node , state )
426
+ } if ( node . type === 3 && node . isComment ) {
427
+ return genComment ( node )
426
428
} else {
427
429
return genText ( node )
428
430
}
@@ -435,6 +437,10 @@ export function genText (text: ASTText | ASTExpression): string {
435
437
} ) `
436
438
}
437
439
440
+ export function genComment ( comment : ASTText ) : string {
441
+ return `_e ( '${comment . text } ') `
442
+ }
443
+
438
444
function genSlot ( el : ASTElement , state : CodegenState ) : string {
439
445
const slotName = el . slotName || '"default"'
440
446
const children = genChildren ( el , state )
Original file line number Diff line number Diff line change @@ -82,6 +82,9 @@ export function parseHTML (html, options) {
82
82
const commentEnd = html . indexOf ( '-->' )
83
83
84
84
if ( commentEnd >= 0 ) {
85
+ if ( options . shouldKeepComment ) {
86
+ options . comment ( html . substring ( 4 , commentEnd ) )
87
+ }
85
88
advance ( commentEnd + 3 )
86
89
continue
87
90
}
Original file line number Diff line number Diff line change @@ -90,6 +90,7 @@ export function parse (
90
90
isUnaryTag : options . isUnaryTag ,
91
91
canBeLeftOpenTag : options . canBeLeftOpenTag ,
92
92
shouldDecodeNewlines : options . shouldDecodeNewlines ,
93
+ shouldKeepComment : options . comments ,
93
94
start ( tag , attrs , unary ) {
94
95
// check namespace.
95
96
// inherit parent ns if there is one
@@ -274,6 +275,13 @@ export function parse (
274
275
} )
275
276
}
276
277
}
278
+ } ,
279
+ comment ( text : string ) {
280
+ currentParent . children . push ( {
281
+ type : 3 ,
282
+ text,
283
+ isComment : true
284
+ } )
277
285
}
278
286
} )
279
287
return root
Original file line number Diff line number Diff line change @@ -64,9 +64,9 @@ export default class VNode {
64
64
}
65
65
}
66
66
67
- export const createEmptyVNode = ( ) => {
67
+ export const createEmptyVNode = ( text : string = '' ) => {
68
68
const node = new VNode ( )
69
- node . text = ''
69
+ node . text = text
70
70
node . isComment = true
71
71
return node
72
72
}
Original file line number Diff line number Diff line change @@ -64,7 +64,8 @@ Vue.prototype.$mount = function (
64
64
65
65
const { render , staticRenderFns } = compileToFunctions ( template , {
66
66
shouldDecodeNewlines,
67
- delimiters : options . delimiters
67
+ delimiters : options . delimiters ,
68
+ comments : options . comments
68
69
} , this )
69
70
options . render = render
70
71
options . staticRenderFns = staticRenderFns
Original file line number Diff line number Diff line change
1
+ import Vue from 'vue'
2
+
3
+ describe ( 'Comments' , ( ) => {
4
+ it ( 'comments should be kept' , ( ) => {
5
+ const vm = new Vue ( {
6
+ comments : true ,
7
+ data ( ) {
8
+ return {
9
+ foo : 1
10
+ }
11
+ } ,
12
+ template : '<div><span>node1</span><!--comment1-->{{foo}}<!--comment2--></div>'
13
+ } ) . $mount ( )
14
+ expect ( vm . $el . innerHTML ) . toEqual ( '<span>node1</span><!--comment1-->1<!--comment2-->' )
15
+ } )
16
+ } )
Original file line number Diff line number Diff line change 1
1
import { parse } from 'compiler/parser/index'
2
2
import { optimize } from 'compiler/optimizer'
3
3
import { generate } from 'compiler/codegen'
4
- import { isObject } from 'shared/util'
4
+ import { isObject , extend } from 'shared/util'
5
5
import { isReservedTag } from 'web/util/index'
6
6
import { baseOptions } from 'web/compiler/options'
7
7
@@ -474,6 +474,19 @@ describe('codegen', () => {
474
474
)
475
475
} )
476
476
477
+ it ( 'generate component with comment' , ( ) => {
478
+ const options = extend ( {
479
+ comments : true
480
+ } , baseOptions )
481
+ const template = '<div><!--comment--></div>'
482
+ const generatedCode = `with(this){return _c('div',[_e('comment')])}`
483
+
484
+ const ast = parse ( template , options )
485
+ optimize ( ast , options )
486
+ const res = generate ( ast , options )
487
+ expect ( res . render ) . toBe ( generatedCode )
488
+ } )
489
+
477
490
it ( 'not specified ast type' , ( ) => {
478
491
const res = generate ( null , baseOptions )
479
492
expect ( res . render ) . toBe ( `with(this){return _c("div")}` )
Original file line number Diff line number Diff line change 1
1
import { parse } from 'compiler/parser/index'
2
+ import { extend } from 'shared/util'
2
3
import { optimize } from 'compiler/optimizer'
3
4
import { baseOptions } from 'web/compiler/options'
4
5
@@ -11,6 +12,19 @@ describe('optimizer', () => {
11
12
expect ( ast . children [ 0 ] . static ) . toBe ( true ) // span
12
13
} )
13
14
15
+ it ( 'simple with comment' , ( ) => {
16
+ const options = extend ( {
17
+ comments : true
18
+ } , baseOptions )
19
+ const ast = parse ( '<h1 id="section1"><span>hello world</span><!--comment--></h1>' , options )
20
+ optimize ( ast , options )
21
+ expect ( ast . static ) . toBe ( true ) // h1
22
+ expect ( ast . staticRoot ) . toBe ( true )
23
+ expect ( ast . children . length ) . toBe ( 2 )
24
+ expect ( ast . children [ 0 ] . static ) . toBe ( true ) // span
25
+ expect ( ast . children [ 1 ] . static ) . toBe ( true ) // comment
26
+ } )
27
+
14
28
it ( 'skip simple nodes' , ( ) => {
15
29
const ast = parse ( '<h1 id="section1">hello</h1>' , baseOptions )
16
30
optimize ( ast , baseOptions )
Original file line number Diff line number Diff line change @@ -548,4 +548,27 @@ describe('parser', () => {
548
548
const ast = parse ( `<script type="x/template">><foo><</script>` , options )
549
549
expect ( ast . children [ 0 ] . text ) . toBe ( `><foo><` )
550
550
} )
551
+
552
+ it ( 'should ignore comments' , ( ) => {
553
+ const options = extend ( { } , baseOptions )
554
+ const ast = parse ( `<div>123<!--comment here--></div>` , options )
555
+ expect ( ast . tag ) . toBe ( 'div' )
556
+ expect ( ast . children . length ) . toBe ( 1 )
557
+ expect ( ast . children [ 0 ] . type ) . toBe ( 3 )
558
+ expect ( ast . children [ 0 ] . text ) . toBe ( '123' )
559
+ } )
560
+
561
+ it ( 'should kept comments' , ( ) => {
562
+ const options = extend ( {
563
+ comments : true
564
+ } , baseOptions )
565
+ const ast = parse ( `<div>123<!--comment here--></div>` , options )
566
+ expect ( ast . tag ) . toBe ( 'div' )
567
+ expect ( ast . children . length ) . toBe ( 2 )
568
+ expect ( ast . children [ 0 ] . type ) . toBe ( 3 )
569
+ expect ( ast . children [ 0 ] . text ) . toBe ( '123' )
570
+ expect ( ast . children [ 1 ] . type ) . toBe ( 3 ) // parse comment with ASTText
571
+ expect ( ast . children [ 1 ] . isComment ) . toBe ( true ) // parse comment with ASTText
572
+ expect ( ast . children [ 1 ] . text ) . toBe ( 'comment here' )
573
+ } )
551
574
} )
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ export interface ComponentOptions<V extends Vue> {
54
54
name ?: string ;
55
55
extends ?: ComponentOptions < Vue > | typeof Vue ;
56
56
delimiters ?: [ string , string ] ;
57
+ comments ?: boolean ;
57
58
}
58
59
59
60
export interface FunctionalComponentOptions {
You can’t perform that action at this time.
0 commit comments