Skip to content

Commit d3768c0

Browse files
defccyyx990803
authored andcommitted
Small refactor for template parser (#4613)
* refactor isSpecialTag and remove useless argument of parseEndTag * remove const * remove useless isSpecialTag because template node is guarded by sfc parser.
1 parent aeb2efa commit d3768c0

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

src/compiler/parser/html-parser.js

+15-28
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,6 @@ let IS_REGEX_CAPTURING_BROKEN = false
4747

4848
// Special Elements (can contain anything)
4949
const isScriptOrStyle = makeMap('script,style', true)
50-
const hasLang = attr => attr.name === 'lang' && attr.value !== 'html'
51-
const isSpecialTag = (tag, isSFC, stack) => {
52-
if (isScriptOrStyle(tag)) {
53-
return true
54-
}
55-
if (isSFC && stack.length === 1) {
56-
// top-level template that has no pre-processor
57-
if (tag === 'template' && !stack[0].attrs.some(hasLang)) {
58-
return false
59-
} else {
60-
return true
61-
}
62-
}
63-
return false
64-
}
65-
6650
const reCache = {}
6751

6852
const ltRE = /</g
@@ -91,7 +75,7 @@ export function parseHTML (html, options) {
9175
while (html) {
9276
last = html
9377
// Make sure we're not in a script or style element
94-
if (!lastTag || !isSpecialTag(lastTag, options.sfc, stack)) {
78+
if (!lastTag || !isScriptOrStyle(lastTag)) {
9579
let textEnd = html.indexOf('<')
9680
if (textEnd === 0) {
9781
// Comment:
@@ -126,7 +110,7 @@ export function parseHTML (html, options) {
126110
if (endTagMatch) {
127111
const curIndex = index
128112
advance(endTagMatch[0].length)
129-
parseEndTag(endTagMatch[0], endTagMatch[1], curIndex, index)
113+
parseEndTag(endTagMatch[1], curIndex, index)
130114
continue
131115
}
132116

@@ -183,7 +167,7 @@ export function parseHTML (html, options) {
183167
})
184168
index += html.length - rest.length
185169
html = rest
186-
parseEndTag('</' + stackedTag + '>', stackedTag, index - endTagLength, index)
170+
parseEndTag(stackedTag, index - endTagLength, index)
187171
}
188172

189173
if (html === last && options.chars) {
@@ -229,10 +213,10 @@ export function parseHTML (html, options) {
229213

230214
if (expectHTML) {
231215
if (lastTag === 'p' && isNonPhrasingTag(tagName)) {
232-
parseEndTag('', lastTag)
216+
parseEndTag(lastTag)
233217
}
234218
if (canBeLeftOpenTag(tagName) && lastTag === tagName) {
235-
parseEndTag('', tagName)
219+
parseEndTag(tagName)
236220
}
237221
}
238222

@@ -259,7 +243,7 @@ export function parseHTML (html, options) {
259243
}
260244

261245
if (!unary) {
262-
stack.push({ tag: tagName, attrs: attrs })
246+
stack.push({ tag: tagName, lowerCasedTag: tagName.toLowerCase(), attrs: attrs })
263247
lastTag = tagName
264248
unarySlash = ''
265249
}
@@ -269,16 +253,19 @@ export function parseHTML (html, options) {
269253
}
270254
}
271255

272-
function parseEndTag (tag, tagName, start, end) {
273-
let pos
256+
function parseEndTag (tagName, start, end) {
257+
let pos, lowerCasedTagName
274258
if (start == null) start = index
275259
if (end == null) end = index
276260

261+
if (tagName) {
262+
lowerCasedTagName = tagName.toLowerCase()
263+
}
264+
277265
// Find the closest opened tag of the same type
278266
if (tagName) {
279-
const needle = tagName.toLowerCase()
280267
for (pos = stack.length - 1; pos >= 0; pos--) {
281-
if (stack[pos].tag.toLowerCase() === needle) {
268+
if (stack[pos].lowerCasedTag === lowerCasedTagName) {
282269
break
283270
}
284271
}
@@ -298,11 +285,11 @@ export function parseHTML (html, options) {
298285
// Remove the open elements from the stack
299286
stack.length = pos
300287
lastTag = pos && stack[pos - 1].tag
301-
} else if (tagName.toLowerCase() === 'br') {
288+
} else if (lowerCasedTagName === 'br') {
302289
if (options.start) {
303290
options.start(tagName, [], true, start, end)
304291
}
305-
} else if (tagName.toLowerCase() === 'p') {
292+
} else if (lowerCasedTagName === 'p') {
306293
if (options.start) {
307294
options.start(tagName, [], false, start, end)
308295
}

src/sfc/parser.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ export function parseComponent (
109109

110110
parseHTML(content, {
111111
start,
112-
end,
113-
sfc: true
112+
end
114113
})
115114

116115
return sfc

0 commit comments

Comments
 (0)