Skip to content

Commit 84dc5a6

Browse files
committed
fix(runtime-core/vnode): should not render boolean values in vnode children (close #574)
1 parent 137893a commit 84dc5a6

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/runtime-core/__tests__/vnode.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ describe('vnode', () => {
120120
expect(normalizeVNode(null)).toMatchObject({ type: Comment })
121121
expect(normalizeVNode(undefined)).toMatchObject({ type: Comment })
122122

123+
// boolean -> Comment
124+
// this is for usage like `someBoolean && h('div')` and behavior consistency
125+
// with 2.x (#574)
126+
expect(normalizeVNode(true)).toMatchObject({ type: Comment })
127+
expect(normalizeVNode(false)).toMatchObject({ type: Comment })
128+
123129
// array -> Fragment
124130
expect(normalizeVNode(['foo'])).toMatchObject({ type: Fragment })
125131

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

143148
test('type shapeFlag inference', () => {

packages/runtime-core/src/vnode.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export function createCommentVNode(
337337
}
338338

339339
export function normalizeVNode<T, U>(child: VNodeChild<T, U>): VNode<T, U> {
340-
if (child == null) {
340+
if (child == null || typeof child === 'boolean') {
341341
// empty placeholder
342342
return createVNode(Comment)
343343
} else if (isArray(child)) {
@@ -348,7 +348,7 @@ export function normalizeVNode<T, U>(child: VNodeChild<T, U>): VNode<T, U> {
348348
// always produce all-vnode children arrays
349349
return child.el === null ? child : cloneVNode(child)
350350
} else {
351-
// primitive types
351+
// strings and numbers
352352
return createVNode(Text, null, String(child))
353353
}
354354
}

0 commit comments

Comments
 (0)