Skip to content

Commit

Permalink
fix SFC parsing pug templates that contains "<" (fix vuejs#3973)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Oct 22, 2016
1 parent 3105661 commit 7dc38de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/compiler/parser/html-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,23 @@ let IS_REGEX_CAPTURING_BROKEN = false
})

// Special Elements (can contain anything)
const isSpecialTag = makeMap('script,style', true)
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
}
// top-level template that has a pre-processor
if (
isSFC &&
tag === 'template' &&
stack.length === 1 &&
stack[0].attrs.some(hasLang)
) {
return true
}
return false
}

const reCache = {}

Expand Down Expand Up @@ -74,7 +90,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)) {
if (!lastTag || !isSpecialTag(lastTag, options.sfc, stack)) {
const textEnd = html.indexOf('<')
if (textEnd === 0) {
// Comment:
Expand Down
3 changes: 2 additions & 1 deletion src/sfc/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export function parseComponent (

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

return sfc
Expand Down
10 changes: 10 additions & 0 deletions test/unit/modules/sfc/sfc-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ describe('Single File Component parser', () => {
expect(res.script.content).toBe(Array(3 + 1).join('//\n') + '\nexport default {}\n')
expect(res.styles[0].content).toBe(Array(6 + 1).join('\n') + '\nh1 { color: red }\n')
})

it('should handle template blocks with lang as special text', () => {
const res = parseComponent(`
<template lang="pug">
div
h1(v-if='1 < 2') hello
</template>
`)
expect(res.template.content.trim()).toBe(`div\n h1(v-if='1 < 2') hello`)
})
})

0 comments on commit 7dc38de

Please sign in to comment.