Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactor for template parser #4613

Merged
merged 3 commits into from
Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 15 additions & 28 deletions src/compiler/parser/html-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +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
}
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
}

const reCache = {}

const ltRE = /</g
Expand Down Expand Up @@ -91,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:
Expand Down Expand Up @@ -126,7 +110,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
}

Expand Down Expand Up @@ -183,7 +167,7 @@ export function parseHTML (html, options) {
})
index += html.length - rest.length
html = rest
parseEndTag('</' + stackedTag + '>', stackedTag, index - endTagLength, index)
parseEndTag(stackedTag, index - endTagLength, index)
}

if (html === last && options.chars) {
Expand Down Expand Up @@ -229,10 +213,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)
}
}

Expand All @@ -259,7 +243,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 = ''
}
Expand All @@ -269,16 +253,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) {
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
}
}
Expand All @@ -298,11 +285,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)
}
Expand Down
3 changes: 1 addition & 2 deletions src/sfc/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ export function parseComponent (

parseHTML(content, {
start,
end,
sfc: true
end
})

return sfc
Expand Down