Skip to content

Commit

Permalink
fix(runtime-core/vnode): should not render boolean values in vnode ch…
Browse files Browse the repository at this point in the history
…ildren (close #574)
  • Loading branch information
yyx990803 committed Jan 6, 2020
1 parent 137893a commit 84dc5a6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/runtime-core/__tests__/vnode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ describe('vnode', () => {
expect(normalizeVNode(null)).toMatchObject({ type: Comment })
expect(normalizeVNode(undefined)).toMatchObject({ type: Comment })

// boolean -> Comment
// this is for usage like `someBoolean && h('div')` and behavior consistency
// with 2.x (#574)
expect(normalizeVNode(true)).toMatchObject({ type: Comment })
expect(normalizeVNode(false)).toMatchObject({ type: Comment })

// array -> Fragment
expect(normalizeVNode(['foo'])).toMatchObject({ type: Fragment })

Expand All @@ -137,7 +143,6 @@ describe('vnode', () => {
// primitive types
expect(normalizeVNode('foo')).toMatchObject({ type: Text, children: `foo` })
expect(normalizeVNode(1)).toMatchObject({ type: Text, children: `1` })
expect(normalizeVNode(true)).toMatchObject({ type: Text, children: `true` })
})

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 @@ -337,7 +337,7 @@ export function createCommentVNode(
}

export function normalizeVNode<T, U>(child: VNodeChild<T, U>): VNode<T, U> {
if (child == null) {
if (child == null || typeof child === 'boolean') {
// empty placeholder
return createVNode(Comment)
} else if (isArray(child)) {
Expand All @@ -348,7 +348,7 @@ export function normalizeVNode<T, U>(child: VNodeChild<T, U>): VNode<T, U> {
// always produce all-vnode children arrays
return child.el === null ? child : cloneVNode(child)
} else {
// primitive types
// strings and numbers
return createVNode(Text, null, String(child))
}
}
Expand Down

0 comments on commit 84dc5a6

Please sign in to comment.