Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd3768c0

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

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

‎src/compiler/parser/html-parser.js‎

Lines changed: 15 additions & 28 deletions
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
constisScriptOrStyle=makeMap('script,style',true)
50-
consthasLang=attr=>attr.name==='lang'&&attr.value!=='html'
51-
constisSpecialTag=(tag,isSFC,stack)=>{
52-
if(isScriptOrStyle(tag)){
53-
returntrue
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-
returnfalse
59-
}else{
60-
returntrue
61-
}
62-
}
63-
returnfalse
64-
}
65-
6650
constreCache={}
6751

6852
constltRE=/</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
lettextEnd=html.indexOf('<')
9680
if(textEnd===0){
9781
// Comment:
@@ -126,7 +110,7 @@ export function parseHTML (html, options) {
126110
if(endTagMatch){
127111
constcurIndex=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-
functionparseEndTag(tag,tagName,start,end){
273-
letpos
256+
functionparseEndTag(tagName,start,end){
257+
letpos,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-
constneedle=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-
}elseif(tagName.toLowerCase()==='br'){
288+
}elseif(lowerCasedTagName==='br'){
302289
if(options.start){
303290
options.start(tagName,[],true,start,end)
304291
}
305-
}elseif(tagName.toLowerCase()==='p'){
292+
}elseif(lowerCasedTagName==='p'){
306293
if(options.start){
307294
options.start(tagName,[],false,start,end)
308295
}

‎src/sfc/parser.js‎

Lines changed: 1 addition & 2 deletions
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
returnsfc

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp