Skip to content

Commit d10835a

Browse files
committed
fix(compiler-sfc): template with alt lang should be parsed as raw text
fix #1120
1 parent e58beec commit d10835a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/compiler-sfc/__tests__/parse.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ h1 { color: red }
106106
expect(descriptor.template!.content).toBe(content)
107107
})
108108

109+
// #1120
110+
test('alternative template lang should be treated as plain text', () => {
111+
const content = `p(v-if="1 < 2") test`
112+
const { descriptor, errors } = parse(
113+
`<template lang="pug">` + content + `</template>`
114+
)
115+
expect(errors.length).toBe(0)
116+
expect(descriptor.template!.content).toBe(content)
117+
})
118+
109119
test('error tolerance', () => {
110120
const { errors } = parse(`<template>`)
111121
expect(errors.length).toBe(1)

packages/compiler-sfc/src/parse.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,20 @@ export function parse(
9696
isNativeTag: () => true,
9797
// preserve all whitespaces
9898
isPreTag: () => true,
99-
getTextMode: ({ tag }, parent) => {
99+
getTextMode: ({ tag, props }, parent) => {
100100
// all top level elements except <template> are parsed as raw text
101101
// containers
102-
if (!parent && tag !== 'template') {
102+
if (
103+
(!parent && tag !== 'template') ||
104+
// <template lang="xxx"> should also be treated as raw text
105+
props.some(
106+
p =>
107+
p.type === NodeTypes.ATTRIBUTE &&
108+
p.name === 'lang' &&
109+
p.value &&
110+
p.value.content !== 'html'
111+
)
112+
) {
103113
return TextModes.RAWTEXT
104114
} else {
105115
return TextModes.DATA

0 commit comments

Comments
 (0)