Skip to content
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

Fix edge case with rendering children as number 0 #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/vhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function h(name, attrs) {

while (stack.length) {
let child = stack.pop();
if (child) {
if (child !== undefined && child !== null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow <div>{true}</div> to render <div>true</div>. Do we want that? The VDOM libs ignore booleans.

In any case, this statement can compress to:

Suggested change
if (child !== undefined && child !== null) {
if (child != null) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also allowed it before so we could leave as is or correct it, up to you. The shorter version while shorter is not explicit, it's better to always use the === and !== operators. Just my thoughts.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would concur with @dburles that being more explicit is often clearer. Also on the <div>{true}</div> debate mimicking the behaviour of React/Preact etc. seems intuitive.

if (child.pop) {
for (let i=child.length; i--; ) stack.push(child[i]);
}
Expand Down
27 changes: 27 additions & 0 deletions test/vhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,31 @@ describe('vhtml', () => {
'<div class="my-class" for="id"></div>'
);
});

it('should render a child of 0', () => {
function Child ({ children }) {
return <span>{children}</span>;
}
expect(
<div><Child>{0}</Child></div>
).to.equal(
'<div><span>0</span></div>'
);
});

it('should not attempt to render a child of null or undefined', () => {
function Child ({ children }) {
return <span>{children}</span>;
}
expect(
<div><Child>{null}</Child></div>
).to.equal(
'<div><span></span></div>'
);
expect(
<div><Child>{undefined}</Child></div>
).to.equal(
'<div><span></span></div>'
);
});
});