Skip to content

Commit

Permalink
fix(compiler-sfc): template with alt lang should be parsed as raw text
Browse files Browse the repository at this point in the history
fix #1120
  • Loading branch information
yyx990803 committed May 7, 2020
1 parent e58beec commit d10835a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/compiler-sfc/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ h1 { color: red }
expect(descriptor.template!.content).toBe(content)
})

// #1120
test('alternative template lang should be treated as plain text', () => {
const content = `p(v-if="1 < 2") test`
const { descriptor, errors } = parse(
`<template lang="pug">` + content + `</template>`
)
expect(errors.length).toBe(0)
expect(descriptor.template!.content).toBe(content)
})

test('error tolerance', () => {
const { errors } = parse(`<template>`)
expect(errors.length).toBe(1)
Expand Down
14 changes: 12 additions & 2 deletions packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,20 @@ export function parse(
isNativeTag: () => true,
// preserve all whitespaces
isPreTag: () => true,
getTextMode: ({ tag }, parent) => {
getTextMode: ({ tag, props }, parent) => {
// all top level elements except <template> are parsed as raw text
// containers
if (!parent && tag !== 'template') {
if (
(!parent && tag !== 'template') ||
// <template lang="xxx"> should also be treated as raw text
props.some(
p =>
p.type === NodeTypes.ATTRIBUTE &&
p.name === 'lang' &&
p.value &&
p.value.content !== 'html'
)
) {
return TextModes.RAWTEXT
} else {
return TextModes.DATA
Expand Down

0 comments on commit d10835a

Please sign in to comment.