Skip to content

Commit 7dc38de

Browse files
committed
fix SFC parsing pug templates that contains "<" (fix #3973)
1 parent 3105661 commit 7dc38de

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/compiler/parser/html-parser.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,23 @@ let IS_REGEX_CAPTURING_BROKEN = false
4444
})
4545

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

4965
const reCache = {}
5066

@@ -74,7 +90,7 @@ export function parseHTML (html, options) {
7490
while (html) {
7591
last = html
7692
// Make sure we're not in a script or style element
77-
if (!lastTag || !isSpecialTag(lastTag)) {
93+
if (!lastTag || !isSpecialTag(lastTag, options.sfc, stack)) {
7894
const textEnd = html.indexOf('<')
7995
if (textEnd === 0) {
8096
// Comment:

src/sfc/parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export function parseComponent (
9292

9393
parseHTML(content, {
9494
start,
95-
end
95+
end,
96+
sfc: true
9697
})
9798

9899
return sfc

test/unit/modules/sfc/sfc-parser.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,14 @@ describe('Single File Component parser', () => {
6363
expect(res.script.content).toBe(Array(3 + 1).join('//\n') + '\nexport default {}\n')
6464
expect(res.styles[0].content).toBe(Array(6 + 1).join('\n') + '\nh1 { color: red }\n')
6565
})
66+
67+
it('should handle template blocks with lang as special text', () => {
68+
const res = parseComponent(`
69+
<template lang="pug">
70+
div
71+
h1(v-if='1 < 2') hello
72+
</template>
73+
`)
74+
expect(res.template.content.trim()).toBe(`div\n h1(v-if='1 < 2') hello`)
75+
})
6676
})

0 commit comments

Comments
 (0)