8000 trim comments when parsing template (close #2253) · codeclever/vue@a2ab1f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit a2ab1f5

Browse files
committed
trim comments when parsing template (close vuejs#2253)
1 parent ad7585f commit a2ab1f5

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
li 10000 nes changed

src/parsers/template.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ const entityRE = /&#?\w+?;/
8585

8686
function 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

src/util/dom.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff 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

280281
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 */
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
/**

test/unit/specs/parsers/template_spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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 () {

0 commit comments

Comments
 (0)
0