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): before cloneIfMounted, should first check if the object is a VNode (#8778) #8784

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/runtime-core/__tests__/rendererComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,41 @@ describe('renderer: component', () => {
expect(serializeInner(root)).toBe(`<h1>1</h1>`)
expect(spy).toHaveBeenCalledTimes(2)
})

// #8778
describe('render return non-VNode', () => {
test('render return Object', async () => {
const root = nodeOps.createElement('div')
const App = {
render() {
return { info: 'hi' }
}
}
render(h(App), root)
expect(serializeInner(root)).toBe('[object Object]')
})

test('render return Proxy', async () => {
const root = nodeOps.createElement('div')
const App = {
render() {
return ref('hi')
}
}
render(h(App), root)
expect(serializeInner(root)).toBe('[object Object]')
})

test('render return Class', async () => {
const A = class {}
const root = nodeOps.createElement('div')
const App = {
render() {
return new A()
}
}
render(h(App), root)
expect(serializeInner(root)).toBe('[object Object]')
})
})
})
9 changes: 8 additions & 1 deletion packages/runtime-core/__tests__/vnode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
mergeProps,
normalizeVNode,
transformVNodeArgs,
isBlockTreeEnabled
isBlockTreeEnabled,
VNodeChild
} from '../src/vnode'
import { Data } from '../src/component'
import { ShapeFlags, PatchFlags } from '@vue/shared'
Expand Down Expand Up @@ -201,6 +202,12 @@ describe('vnode', () => {
// primitive types
expect(normalizeVNode('foo')).toMatchObject({ type: Text, children: `foo` })
expect(normalizeVNode(1)).toMatchObject({ type: Text, children: `1` })

// #8778 object from render function
expect(normalizeVNode({ key: '1' } as VNodeChild)).toMatchObject({
type: Text,
children: `[object Object]`
})
})

test('type shapeFlag inference', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,12 @@ export function normalizeVNode(child: VNodeChild): VNode {
// #3666, avoid reference pollution when reusing vnode
child.slice()
)
} else if (typeof child === 'object') {
} else if (typeof child === 'object' && isVNode(child)) {
// already vnode, this should be the most common since compiled templates
// always produce all-vnode children arrays
return cloneIfMounted(child)
} else {
// strings and numbers
// strings and numbers or object that doesn't a vnode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a warning or error would be more appropriate, rather than converting the object to a string.

return createVNode(Text, null, String(child))
}
}
Expand Down