diff --git a/packages/compiler-core/__tests__/transforms/vIf.spec.ts b/packages/compiler-core/__tests__/transforms/vIf.spec.ts index 315497a590c..a21ab1f019b 100644 --- a/packages/compiler-core/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vIf.spec.ts @@ -606,6 +606,27 @@ describe('compiler: v-if', () => { expect(branch1.props).toMatchObject(createObjectMatcher({ key: `[0]` })) }) + test('with spaces between branches', () => { + const { + node: { codegenNode } + } = parseWithIfTransform( + `
` + ) + 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(` diff --git a/packages/compiler-core/src/transforms/vIf.ts b/packages/compiler-core/src/transforms/vIf.ts index 00832a9acec..76a79e53f21 100644 --- a/packages/compiler-core/src/transforms/vIf.ts +++ b/packages/compiler-core/src/transforms/vIf.ts @@ -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()