]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): allow spaces between if-else branches (#2305)
authorᴜɴвʏтᴇ <i@shangyes.net>
Thu, 8 Oct 2020 00:57:17 +0000 (08:57 +0800)
committerGitHub <noreply@github.com>
Thu, 8 Oct 2020 00:57:17 +0000 (20:57 -0400)
fix #2299

packages/compiler-core/__tests__/transforms/vIf.spec.ts
packages/compiler-core/src/transforms/vIf.ts

index 315497a590c4d7b0c93efe35412eb6b43d1d5384..a21ab1f019bf5456636b2d1c950eccfbcf45f6fb 100644 (file)
@@ -606,6 +606,27 @@ describe('compiler: v-if', () => {
       expect(branch1.props).toMatchObject(createObjectMatcher({ key: `[0]` }))
     })
 
+    test('with spaces between branches', () => {
+      const {
+        node: { codegenNode }
+      } = parseWithIfTransform(
+        `<div v-if="ok"/> <div v-else-if="no"/> <div v-else/>`
+      )
+      expect(codegenNode.consequent).toMatchObject({
+        tag: `"div"`,
+        props: createObjectMatcher({ key: `[0]` })
+      })
+      const branch = codegenNode.alternate as ConditionalExpression
+      expect(branch.consequent).toMatchObject({
+        tag: `"div"`,
+        props: createObjectMatcher({ key: `[1]` })
+      })
+      expect(branch.alternate).toMatchObject({
+        tag: `"div"`,
+        props: createObjectMatcher({ key: `[2]` })
+      })
+    })
+
     test('with comments', () => {
       const { node } = parseWithIfTransform(`
           <template v-if="ok">
index 00832a9acecf686594a3adfe2a90bf14119c692f..76a79e53f21625771aa4966820e6bc4c4d7d1019 100644 (file)
@@ -130,6 +130,16 @@ export function processIf(
         comments.unshift(sibling)
         continue
       }
+
+      if (
+        sibling &&
+        sibling.type === NodeTypes.TEXT &&
+        !sibling.content.trim().length
+      ) {
+        context.removeNode(sibling)
+        continue
+      }
+
       if (sibling && sibling.type === NodeTypes.IF) {
         // move the node to the if node's branches
         context.removeNode()