]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): template with alt lang should be parsed as raw text
authorEvan You <yyx990803@gmail.com>
Thu, 7 May 2020 15:08:17 +0000 (11:08 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 7 May 2020 15:08:17 +0000 (11:08 -0400)
fix #1120

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

index ddd6570de9a5ca30e8f518e6bee9593c741b3738..d7d8ca6b2e817f429cc8fbb3e9850b5a21ae99d1 100644 (file)
@@ -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)
index ce466ba672896c90d129bb1539e005ab6984df7d..2e8bd719eb6a28335d4c1f19bb3f91e57fb359a8 100644 (file)
@@ -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