From 88278a1fb47f93edb40e974ab4eaf6923169f4a4 Mon Sep 17 00:00:00 2001 From: David Burles Date: Mon, 18 Mar 2019 18:13:15 +1100 Subject: [PATCH 1/2] fix edge case with rendering children as number 0 --- src/vhtml.js | 2 +- test/vhtml.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/vhtml.js b/src/vhtml.js index d42d9c9..3fb7b95 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -36,7 +36,7 @@ export default function h(name, attrs) { while (stack.length) { let child = stack.pop(); - if (child) { + if (child !== undefined) { if (child.pop) { for (let i=child.length; i--; ) stack.push(child[i]); } diff --git a/test/vhtml.js b/test/vhtml.js index 606d762..74fcf5b 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -165,4 +165,15 @@ describe('vhtml', () => { '
' ); }); + + it('should render a child of 0', () => { + function Child ({ children }) { + return {children}; + } + expect( +
{0}
+ ).to.equal( + '
0
' + ); + }); }); From 015b1b9e214ad3d893d78349c4b40eff1ae6d48d Mon Sep 17 00:00:00 2001 From: David Burles Date: Mon, 18 Mar 2019 18:50:51 +1100 Subject: [PATCH 2/2] don't attempt to render null children --- src/vhtml.js | 2 +- test/vhtml.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/vhtml.js b/src/vhtml.js index 3fb7b95..b625916 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -36,7 +36,7 @@ export default function h(name, attrs) { while (stack.length) { let child = stack.pop(); - if (child !== undefined) { + if (child !== undefined && child !== null) { if (child.pop) { for (let i=child.length; i--; ) stack.push(child[i]); } diff --git a/test/vhtml.js b/test/vhtml.js index 74fcf5b..a479838 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -176,4 +176,20 @@ describe('vhtml', () => { '
0
' ); }); + + it('should not attempt to render a child of null or undefined', () => { + function Child ({ children }) { + return {children}; + } + expect( +
{null}
+ ).to.equal( + '
' + ); + expect( +
{undefined}
+ ).to.equal( + '
' + ); + }); });