Skip to content

fix(vnode): should skip null/undefined/boolean inside array children fix #1342 #1345

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

Closed
wants to merge 2 commits 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
5 changes: 5 additions & 0 deletions packages/runtime-core/__tests__/vnode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ describe('vnode', () => {
ShapeFlags.ELEMENT | ShapeFlags.ARRAY_CHILDREN
)
})

test('should skip null/undefined/boolean inside array children', () => {
const vnode = createVNode('div', null, [null, undefined, true])
expect(vnode.children!.length).toBe(0)
})
})

test('normalizeVNode', () => {
Expand Down
16 changes: 11 additions & 5 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,16 +488,13 @@ export function createCommentVNode(
}

export function normalizeVNode(child: VNodeChild): VNode {
if (child == null || typeof child === 'boolean') {
// empty placeholder
return createVNode(Comment)
} else if (isArray(child)) {
if (isArray(child)) {
// fragment
return createVNode(Fragment, null, child)
} else if (typeof child === 'object') {
// already vnode, this should be the most common since compiled templates
// always produce all-vnode children arrays
return child.el === null ? child : cloneVNode(child)
return child!.el === null ? child! : cloneVNode(child!)
} else {
// strings and numbers
return createVNode(Text, null, String(child))
Expand Down Expand Up @@ -545,10 +542,19 @@ export function normalizeChildren(vnode: VNode, children: unknown) {
type = ShapeFlags.TEXT_CHILDREN
}
}

if (isArray(children)) {
children = normalizeArrayChildren(children)
}

vnode.children = children as VNodeNormalizedChildren
vnode.shapeFlag |= type
}

function normalizeArrayChildren(children: VNodeArrayChildren) {
return children.filter(child => child != null && typeof child !== 'boolean')
}

const handlersRE = /^on|^vnode/

export function mergeProps(...args: (Data & VNodeProps)[]) {
Expand Down