Browse Source

fix sfc parser special tag check for nested templates (fix #4289)

Evan You 9 years ago
parent
commit
9d0bc9768b
2 changed files with 20 additions and 2 deletions
  1. 2 2
      src/compiler/parser/html-parser.js
  2. 18 0
      test/unit/modules/sfc/sfc-parser.spec.js

+ 2 - 2
src/compiler/parser/html-parser.js

@@ -52,9 +52,9 @@ const isSpecialTag = (tag, isSFC, stack) => {
   if (isScriptOrStyle(tag)) {
     return true
   }
-  if (isSFC) {
+  if (isSFC && stack.length === 1) {
     // top-level template that has no pre-processor
-    if (tag === 'template' && stack.length === 1 && !stack[0].attrs.some(hasLang)) {
+    if (tag === 'template' && !stack[0].attrs.some(hasLang)) {
       return false
     } else {
       return true

+ 18 - 0
test/unit/modules/sfc/sfc-parser.spec.js

@@ -123,4 +123,22 @@ describe('Single File Component parser', () => {
     expect(simpleTest.attrs.name).toBe('simple')
     expect(simpleTest.attrs.foo).toBe('bar')
   })
+
+  // Regression #4289
+  it('accepts nested template tag', () => {
+    const raw = `<div>
+      <template v-if="true === true">
+        <section class="section">
+          <div class="container">
+            Should be shown
+          </div>
+        </section>
+      </template>
+      <template v-else>
+        <p>Shoud not be shown</p>
+      </template>
+    </div>`
+    const res = parseComponent(`<template>${raw}</template>`)
+    expect(res.template.content.trim()).toBe(raw)
+  })
 })