This commit is contained in:
yangxiuxiu 2025-05-05 20:38:32 +00:00 committed by GitHub
commit 19372d4b74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View File

@ -50,6 +50,12 @@ describe('vnode', () => {
expect(vnode.type).toBe(Comment) expect(vnode.type).toBe(Comment)
}) })
test('show warn when create with void element', () => {
const vnode = createVNode('img', null, createVNode('p'))
expect("don't render child nodes in a void element").toHaveBeenWarned()
expect(vnode.type).toBe('img')
})
test('create from an existing vnode', () => { test('create from an existing vnode', () => {
const vnode1 = createVNode('p', { id: 'foo' }) const vnode1 = createVNode('p', { id: 'foo' })
const vnode2 = createVNode(vnode1, { class: 'bar' }, 'baz') const vnode2 = createVNode(vnode1, { class: 'bar' }, 'baz')

View File

@ -9,6 +9,7 @@ import {
isObject, isObject,
isOn, isOn,
isString, isString,
isVoidTag,
normalizeClass, normalizeClass,
normalizeStyle, normalizeStyle,
} from '@vue/shared' } from '@vue/shared'
@ -618,6 +619,17 @@ function _createVNode(
? ShapeFlags.FUNCTIONAL_COMPONENT ? ShapeFlags.FUNCTIONAL_COMPONENT
: 0 : 0
if (
__DEV__ &&
shapeFlag & ShapeFlags.ELEMENT &&
isVoidTag(type as string) &&
children != null
) {
warn(
`don't render child nodes in a void element <${type as string} />, it may cause unexpected behavior`,
)
}
if (__DEV__ && shapeFlag & ShapeFlags.STATEFUL_COMPONENT && isProxy(type)) { if (__DEV__ && shapeFlag & ShapeFlags.STATEFUL_COMPONENT && isProxy(type)) {
type = toRaw(type) type = toRaw(type)
warn( warn(