From f5b9ea6533f8811c5e1bfb3b714255f1faa265ee Mon Sep 17 00:00:00 2001 From: defcc Date: Sat, 31 Dec 2016 02:51:38 +0800 Subject: [PATCH 1/3] refactor isSpecialTag and remove useless argument of parseEndTag --- src/compiler/parser/html-parser.js | 39 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/compiler/parser/html-parser.js b/src/compiler/parser/html-parser.js index b374ee32dd5..e3ef2baa5e6 100644 --- a/src/compiler/parser/html-parser.js +++ b/src/compiler/parser/html-parser.js @@ -52,15 +52,11 @@ const isSpecialTag = (tag, isSFC, stack) => { if (isScriptOrStyle(tag)) { return true } - if (isSFC && stack.length === 1) { - // top-level template that has no pre-processor - if (tag === 'template' && !stack[0].attrs.some(hasLang)) { - return false - } else { - return true - } - } - return false + return (isSFC && + stack.length === 1 && + tag === 'template' && + stack[0].attrs.some(hasLang) + ) } const reCache = {} @@ -126,7 +122,7 @@ export function parseHTML (html, options) { if (endTagMatch) { const curIndex = index advance(endTagMatch[0].length) - parseEndTag(endTagMatch[0], endTagMatch[1], curIndex, index) + parseEndTag(endTagMatch[1], curIndex, index) continue } @@ -183,7 +179,7 @@ export function parseHTML (html, options) { }) index += html.length - rest.length html = rest - parseEndTag('', stackedTag, index - endTagLength, index) + parseEndTag(stackedTag, index - endTagLength, index) } if (html === last && options.chars) { @@ -229,10 +225,10 @@ export function parseHTML (html, options) { if (expectHTML) { if (lastTag === 'p' && isNonPhrasingTag(tagName)) { - parseEndTag('', lastTag) + parseEndTag(lastTag) } if (canBeLeftOpenTag(tagName) && lastTag === tagName) { - parseEndTag('', tagName) + parseEndTag(tagName) } } @@ -259,7 +255,7 @@ export function parseHTML (html, options) { } if (!unary) { - stack.push({ tag: tagName, attrs: attrs }) + stack.push({ tag: tagName, lowerCasedTag: tagName.toLowerCase(), attrs: attrs }) lastTag = tagName unarySlash = '' } @@ -269,16 +265,19 @@ export function parseHTML (html, options) { } } - function parseEndTag (tag, tagName, start, end) { - let pos + function parseEndTag (tagName, start, end) { + let pos, lowerCasedTagName if (start == null) start = index if (end == null) end = index + if (tagName) { + const lowerCasedTagName = tagName.toLowerCase() + } + // Find the closest opened tag of the same type if (tagName) { - const needle = tagName.toLowerCase() for (pos = stack.length - 1; pos >= 0; pos--) { - if (stack[pos].tag.toLowerCase() === needle) { + if (stack[pos].lowerCasedTag === lowerCasedTagName) { break } } @@ -298,11 +297,11 @@ export function parseHTML (html, options) { // Remove the open elements from the stack stack.length = pos lastTag = pos && stack[pos - 1].tag - } else if (tagName.toLowerCase() === 'br') { + } else if (lowerCasedTagName === 'br') { if (options.start) { options.start(tagName, [], true, start, end) } - } else if (tagName.toLowerCase() === 'p') { + } else if (lowerCasedTagName === 'p') { if (options.start) { options.start(tagName, [], false, start, end) } From 6400ba33861075f37c89bf21b712ef9662328695 Mon Sep 17 00:00:00 2001 From: defcc Date: Sat, 31 Dec 2016 02:56:17 +0800 Subject: [PATCH 2/3] remove const --- src/compiler/parser/html-parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/parser/html-parser.js b/src/compiler/parser/html-parser.js index e3ef2baa5e6..2aee920b48b 100644 --- a/src/compiler/parser/html-parser.js +++ b/src/compiler/parser/html-parser.js @@ -271,7 +271,7 @@ export function parseHTML (html, options) { if (end == null) end = index if (tagName) { - const lowerCasedTagName = tagName.toLowerCase() + lowerCasedTagName = tagName.toLowerCase() } // Find the closest opened tag of the same type From 98be4d96b5c8f13ef0dec8d686607308c3641983 Mon Sep 17 00:00:00 2001 From: defcc Date: Sat, 31 Dec 2016 03:12:17 +0800 Subject: [PATCH 3/3] remove useless isSpecialTag because template node is guarded by sfc parser. --- src/compiler/parser/html-parser.js | 14 +------------- src/sfc/parser.js | 3 +-- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/compiler/parser/html-parser.js b/src/compiler/parser/html-parser.js index 2aee920b48b..fabe264e5f6 100644 --- a/src/compiler/parser/html-parser.js +++ b/src/compiler/parser/html-parser.js @@ -47,18 +47,6 @@ let IS_REGEX_CAPTURING_BROKEN = false // Special Elements (can contain anything) const isScriptOrStyle = makeMap('script,style', true) -const hasLang = attr => attr.name === 'lang' && attr.value !== 'html' -const isSpecialTag = (tag, isSFC, stack) => { - if (isScriptOrStyle(tag)) { - return true - } - return (isSFC && - stack.length === 1 && - tag === 'template' && - stack[0].attrs.some(hasLang) - ) -} - const reCache = {} const ltRE = /</g @@ -87,7 +75,7 @@ export function parseHTML (html, options) { while (html) { last = html // Make sure we're not in a script or style element - if (!lastTag || !isSpecialTag(lastTag, options.sfc, stack)) { + if (!lastTag || !isScriptOrStyle(lastTag)) { let textEnd = html.indexOf('<') if (textEnd === 0) { // Comment: diff --git a/src/sfc/parser.js b/src/sfc/parser.js index 8f5af6b0b7e..533c8b558eb 100644 --- a/src/sfc/parser.js +++ b/src/sfc/parser.js @@ -109,8 +109,7 @@ export function parseComponent ( parseHTML(content, { start, - end, - sfc: true + end }) return sfc