Skip to content

Commit 7c74feb

Browse files
authored
fix(compiler-dom): comments in the v-if branchs should be ignored when used in Transition (#3622)
fix #3619
1 parent 3ef1fcc commit 7c74feb

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
OPEN_BLOCK,
3535
CREATE_VNODE
3636
} from '../runtimeHelpers'
37-
import { injectProp, findDir, findProp } from '../utils'
37+
import { injectProp, findDir, findProp, isBuiltInType } from '../utils'
3838
import { PatchFlags, PatchFlagNames } from '@vue/shared'
3939

4040
export const transformIf = createStructuralDirectiveTransform(
@@ -146,7 +146,16 @@ export function processIf(
146146
// move the node to the if node's branches
147147
context.removeNode()
148148
const branch = createIfBranch(node, dir)
149-
if (__DEV__ && comments.length) {
149+
if (
150+
__DEV__ &&
151+
comments.length &&
152+
// #3619 ignore comments if the v-if is direct child of <transition>
153+
!(
154+
context.parent &&
155+
context.parent.type === NodeTypes.ELEMENT &&
156+
isBuiltInType(context.parent.tag, 'transition')
157+
)
158+
) {
150159
branch.children = [...comments, ...branch.children]
151160
}
152161

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`the v-if/else-if/else branchs in Transition should ignore comments 1`] = `
4+
"const _Vue = Vue
5+
6+
return function render(_ctx, _cache) {
7+
with (_ctx) {
8+
const { openBlock: _openBlock, createBlock: _createBlock, createCommentVNode: _createCommentVNode, createVNode: _createVNode, Fragment: _Fragment, Transition: _Transition, withCtx: _withCtx } = _Vue
9+
10+
return (_openBlock(), _createBlock(_Transition, null, {
11+
default: _withCtx(() => [
12+
a
13+
? (_openBlock(), _createBlock(\\"div\\", { key: 0 }, \\"hey\\"))
14+
: b
15+
? (_openBlock(), _createBlock(\\"div\\", { key: 1 }, \\"hey\\"))
16+
: (_openBlock(), _createBlock(\\"div\\", { key: 2 }, [
17+
c
18+
? (_openBlock(), _createBlock(\\"p\\", { key: 0 }))
19+
: (_openBlock(), _createBlock(_Fragment, { key: 1 }, [
20+
_createCommentVNode(\\" this should not be ignored \\"),
21+
_createVNode(\\"p\\")
22+
], 2112 /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */))
23+
]))
24+
]),
25+
_: 1 /* STABLE */
26+
}))
27+
}
28+
}"
29+
`;

packages/compiler-dom/__tests__/transforms/warnTransitionChildren.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,21 @@ describe('compiler warnings', () => {
138138
})
139139
})
140140
})
141+
142+
test('the v-if/else-if/else branchs in Transition should ignore comments', () => {
143+
expect(
144+
compile(`
145+
<transition>
146+
<div v-if="a">hey</div>
147+
<!-- this should be ignored -->
148+
<div v-else-if="b">hey</div>
149+
<!-- this should be ignored -->
150+
<div v-else>
151+
<p v-if="c"/>
152+
<!-- this should not be ignored -->
153+
<p v-else/>
154+
</div>
155+
</transition>
156+
`).code
157+
).toMatchSnapshot()
158+
})

0 commit comments

Comments
 (0)