File tree Expand file tree Collapse file tree 3 files changed +30
-13
lines changed Expand file tree Collapse file tree 3 files changed +30
-13
lines changed Original file line number Diff line number Diff line change @@ -85,7 +85,10 @@ const entityRE = /&#?\w+?;/
8585
8686function stringToFragment ( templateString , raw ) {
8787 // 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 )
8992 if ( hit ) {
9093 return hit
9194 }
@@ -108,8 +111,7 @@ function stringToFragment (templateString, raw) {
108111 var suffix = wrap [ 2 ]
109112 var node = document . createElement ( 'div' )
110113
111- var templateStringToUse = raw ? templateString : templateString . trim ( )
112- node . innerHTML = prefix + templateStringToUse + suffix
114+ node . innerHTML = prefix + templateString + suffix
113115 while ( depth -- ) {
114116 node = node . lastChild
115117 }
@@ -121,8 +123,10 @@ function stringToFragment (templateString, raw) {
121123 frag . appendChild ( child )
122124 }
123125 }
124-
125- templateCache . put ( templateString , frag )
126+ if ( ! raw ) {
127+ trimNode ( frag )
128+ }
129+ templateCache . put ( cacheKey , frag )
126130 return frag
127131}
128132
Original file line number Diff line number Diff line change @@ -272,20 +272,29 @@ export function extractContent (el, asFragment) {
272272}
273273
274274/**
275- * Trim possible empty head/tail textNodes inside a parent.
275+ * Trim possible empty head/tail text and comment
276+ * nodes inside a parent.
276277 *
277278 * @param {Node } node
278279 */
279280
280281export 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 */
283291}
284292
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+ )
289298}
290299
291300/**
Original file line number Diff line number Diff line change @@ -158,7 +158,7 @@ describe('Template Parser', function () {
158158 expect ( c . value ) . toBe ( '' )
159159 } )
160160
161- it ( 'should trim empty text nodes' , function ( ) {
161+ it ( 'should trim empty text nodes and comments ' , function ( ) {
162162 // string
163163 var res = parse ( ' <p>test</p> ' )
164164 expect ( res . childNodes . length ) . toBe ( 1 )
@@ -169,6 +169,10 @@ describe('Template Parser', function () {
169169 res = parse ( el . children [ 0 ] )
170170 expect ( res . childNodes . length ) . toBe ( 1 )
171171 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' )
172176 } )
173177
174178 it ( 'should reuse fragment from cache for the same string template' , function ( ) {
You can’t perform that action at this time.
0 commit comments