File tree 3 files changed +30
-13
lines changed 3 files changed +30
-13
li
10000
nes changed Original file line number Diff line number Diff line change @@ -85,7 +85,10 @@ const entityRE = /&#?\w+?;/
85
85
86
86
function stringToFragment ( templateString , raw ) {
87
87
// try a cache hit first
88
- var hit = templateCache . get ( templateString )
88
+ var cacheKey = raw
89
+ ? templateString
90
+ : templateString . trim ( )
91
+ var hit = templateCache . get ( cacheKey )
89
92
if ( hit ) {
90
93
return hit
91
94
}
@@ -108,8 +111,7 @@ function stringToFragment (templateString, raw) {
108
111
var suffix = wrap [ 2 ]
109
112
var node = document . createElement ( 'div' )
110
113
111
- var templateStringToUse = raw ? templateString : templateString . trim ( )
112
- node . innerHTML = prefix + templateStringToUse + suffix
114
+ node . innerHTML = prefix + templateString + suffix
113
115
while ( depth -- ) {
114
116
node = node . lastChild
115
117
}
@@ -121,8 +123,10 @@ function stringToFragment (templateString, raw) {
121
123
frag . appendChild ( child )
122
124
}
123
125
}
124
-
125
- templateCache . put ( templateString , frag )
126
+ if ( ! raw ) {
127
+ trimNode ( frag )
128
+ }
129
+ templateCache . put ( cacheKey , frag )
126
130
return frag
127
131
}
128
132
Original file line number Diff line number Diff line change @@ -272,20 +272,29 @@ export function extractContent (el, asFragment) {
272
272
}
273
273
274
274
/**
275
- * Trim possible empty head/tail textNodes inside a parent.
275
+ * Trim possible empty head/tail text and comment
276
+ * nodes inside a parent.
276
277
*
277
278
* @param {Node } node
278
279
*/
279
280
280
281
export function trimNode ( node ) {
281
- trim ( node , node . firstChild )
282
- trim ( node , node . lastChild )
282
+ var child
283
+ /* eslint-disable no-sequences */
284
+ while ( child = node . firstChild , isTrimmable ( child ) ) {
285
+ node . removeChild ( child )
286
+ }
287
+ while ( child = node . lastChild , isTrimmable ( child ) ) {
288
+ node . removeChild ( child )
289
+ }
290
+ /* eslint-enable no-sequences */
283
291
}
284
292
285
- function trim ( parent , node ) {
286
- if ( node && node . nodeType === 3 && ! node . data . trim ( ) ) {
287
- parent . removeChild ( node )
288
- }
293
+ function isTrimmable ( node ) {
294
+ return node && (
295
+ ( node . nodeType === 3 && ! node . data . trim ( ) ) ||
296
+ node . nodeType === 8
297
+ )
289
298
}
290
299
291
300
/**
Original file line number Diff line number Diff line change @@ -158,7 +158,7 @@ describe('Template Parser', function () {
158
158
expect ( c . value ) . toBe ( '' )
159
159
} )
160
160
161
- it ( 'should trim empty text nodes' , function ( ) {
161
+ it ( 'should trim empty text nodes and comments ' , function ( ) {
162
162
// string
163
163
var res = parse ( ' <p>test</p> ' )
164
164
expect ( res . childNodes . length ) . toBe ( 1 )
@@ -169,6 +169,10 @@ describe('Template Parser', function () {
169
169
res = parse ( el . children [ 0 ] )
170
170
expect ( res . childNodes . length ) . toBe ( 1 )
171
171
expect ( res . firstChild . tagName ) . toBe ( 'P' )
172
+ // comments
173
+ res = parse ( ' <!-- yo --> <p>test</p> <!-- yo --> ' )
174
+ expect ( res . childNodes . length ) . toBe ( 1 )
175
+ expect ( res . firstChild . tagName ) . toBe ( 'P' )
172
176
} )
173
177
174
178
it ( 'should reuse fragment from cache for the same string template' , function ( ) {
You can’t perform that action at this time.
0 commit comments