Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #34 from aknuds1/flatten-children
Browse files Browse the repository at this point in the history
Don't add null or nested children to elements
  • Loading branch information
aknuds1 committed Sep 23, 2016
2 parents 5f2ed66 + 3a37f2b commit a74e7b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/process-node-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ var ProcessNodeDefinitions = function(React) {
if (includes(voidElementTags, node.name)) {
return React.createElement(node.name, elementProps)
} else {
return React.createElement(node.name, elementProps, node.data, children);
}

var allChildren = node.data != null ? [node.data,].concat(children) : children;
return React.createElement.apply(
this, [node.name, elementProps,].concat(allChildren)
);
}
}

return {
processDefaultNode: processDefaultNode
processDefaultNode: processDefaultNode,
};
};

Expand Down
8 changes: 8 additions & 0 deletions test/html-to-react-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ describe('Html2React', function() {
assert.equal(reactHtml, htmlInput);
});

it('should not generate children for childless elements', function () {
var htmlInput = '<div></div>';

var reactComponent = parser.parse(htmlInput);

assert.strictEqual((reactComponent.props.children || []).length, 0);
});

it('should fill in the key name with boolean attribute', function() {
var htmlInput = '<input type="checkbox" disabled required/>';
var htmlExpected = '<input type="checkbox" disabled="" required=""/>'
Expand Down

0 comments on commit a74e7b2

Please sign in to comment.