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(runtime-core): TypeError when using html comment in teleport(fix #10747) #10748

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 72 additions & 1 deletion packages/runtime-core/__tests__/components/Teleport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ import {
nodeOps,
ref,
render,
serialize,
serializeInner,
withDirectives,
} from '@vue/runtime-test'
import { Fragment, createVNode } from '../../src/vnode'
import {
Fragment,
createBlock,
createCommentVNode,
createTextVNode,
createVNode,
openBlock,
} from '../../src/vnode'
import { compile, render as domRender } from 'vue'
import { toDisplayString } from '@vue/shared'

describe('renderer: teleport', () => {
test('should work', () => {
Expand Down Expand Up @@ -129,6 +138,41 @@ describe('renderer: teleport', () => {
expect(serializeInner(target)).toMatchInlineSnapshot(`"teleported"`)
})

test('should traverse comment node after updating in optimize mode', async () => {
const target = nodeOps.createElement('div')
const root = nodeOps.createElement('div')
const count = ref(0)
let teleport

__DEV__ = false
render(
h(() => {
teleport =
(openBlock(),
createBlock(Teleport, { to: target }, [
createCommentVNode('comment in teleport'),
]))
return h('div', null, [
createTextVNode(toDisplayString(count.value)),
teleport,
])
}),
root,
)
const commentNode = teleport!.children[0].el
expect(serializeInner(root)).toMatchInlineSnapshot(`"<div>0</div>"`)
expect(serializeInner(target)).toMatchInlineSnapshot(
`"<!--comment in teleport-->"`,
)
expect(serialize(commentNode)).toBe(`<!--comment in teleport-->`)

count.value = 1
await nextTick()
__DEV__ = true
expect(serializeInner(root)).toMatchInlineSnapshot(`"<div>1</div>"`)
expect(teleport!.children[0].el).toBe(commentNode)
})

test('should remove children when unmounted', () => {
const target = nodeOps.createElement('div')
const root = nodeOps.createElement('div')
Expand All @@ -151,6 +195,33 @@ describe('renderer: teleport', () => {
testUnmount({ to: target, disabled: true })
testUnmount({ to: null, disabled: true })
})
// #10747
test('should unmount correctly when using top level comment in teleport', async () => {
const target = nodeOps.createElement('div')
const root = nodeOps.createElement('div')
const count = ref(0)

__DEV__ = false
render(
h(() => {
return h('div', null, [
createTextVNode(toDisplayString(count.value)),
(openBlock(),
createBlock(Teleport, { to: target }, [
createCommentVNode('comment in teleport'),
])),
])
}),
root,
)

count.value = 1

await nextTick()
__DEV__ = true
render(null, root)
expect(root.children.length).toBe(0)
})

test('component with multi roots should be removed when unmounted', () => {
const target = nodeOps.createElement('div')
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,7 @@ export function traverseStaticChildren(n1: VNode, n2: VNode, shallow = false) {
}
// also inherit for comment nodes, but not placeholders (e.g. v-if which
// would have received .el during block patch)
if (__DEV__ && c2.type === Comment && !c2.el) {
if (c2.type === Comment && !c2.el) {
c2.el = c1.el
}
}
Expand Down