Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TransitionGroup): avoid set transition hooks for comment nodes and text nodes #9421

Merged
merged 14 commits into from
Apr 14, 2024
2 changes: 1 addition & 1 deletion packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let hasMismatch = false
const isSVGContainer = (container: Element) =>
/svg/.test(container.namespaceURI!) && container.tagName !== 'foreignObject'

const isComment = (node: Node): node is Comment =>
export const isComment = (node: Node): node is Comment =>
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
node.nodeType === DOMNodeTypes.COMMENT

// Note: hydration is DOM-specific
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export type {
RootRenderFunction
} from './renderer'
export type { RootHydrateFunction } from './hydration'
export { isComment } from './hydration'
export type { Slot, Slots, SlotsType } from './componentSlots'
export type {
Prop,
Expand Down
6 changes: 4 additions & 2 deletions packages/runtime-dom/src/components/TransitionGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
toRaw,
compatUtils,
DeprecationTypes,
ComponentOptions
ComponentOptions,
isComment
} from '@vue/runtime-core'
import { extend } from '@vue/shared'

Expand Down Expand Up @@ -112,7 +113,8 @@ const TransitionGroupImpl: ComponentOptions = {
tag = 'span'
}

prevChildren = children
prevChildren =
children && children.filter(child => !isComment(child.el as Element))
children = slots.default ? getTransitionRawChildren(slots.default()) : []

for (let i = 0; i < children.length; i++) {
Expand Down
70 changes: 70 additions & 0 deletions packages/vue/__tests__/e2e/TransitionGroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,74 @@ describe('e2e: TransitionGroup', () => {

expect(`<TransitionGroup> children must be keyed`).toHaveBeenWarned()
})

// #5168
test(
'avoid set transition hooks for comment node',
async () => {
await page().evaluate(duration => {
const { createApp, ref, h, createCommentVNode } = (window as any).Vue

const show = ref(false)
createApp({
template: `
<div id="container">
<transition-group name="test">
<div v-for="item in items" :key="item" class="test">{{item}}</div>
<Child key="child"/>
</transition-group>
</div>
<button id="toggleBtn" @click="click">button</button>
`,
components: {
Child: {
setup() {
return () =>
show.value
? h('div', { class: 'test' }, 'child')
: createCommentVNode('v-if', true)
}
}
},
setup: () => {
const items = ref([])
const click = () => {
items.value = ['a', 'b', 'c']
setTimeout(() => {
show.value = true
}, duration)
}
return { click, items }
}
}).mount('#app')
}, duration)

expect(await html('#container')).toBe(`<!--v-if-->`)

expect(await htmlWhenTransitionStart()).toBe(
`<div class="test test-enter-from test-enter-active">a</div>` +
`<div class="test test-enter-from test-enter-active">b</div>` +
`<div class="test test-enter-from test-enter-active">c</div>` +
`<!--v-if-->`
)

await transitionFinish(duration)
await nextFrame()
expect(await html('#container')).toBe(
`<div class="test">a</div>` +
`<div class="test">b</div>` +
`<div class="test">c</div>` +
`<div class="test test-enter-active test-enter-to">child</div>`
)

await transitionFinish(duration)
expect(await html('#container')).toBe(
`<div class="test">a</div>` +
`<div class="test">b</div>` +
`<div class="test">c</div>` +
`<div class="test">child</div>`
)
},
E2E_TIMEOUT
)
})