From 3a37f2b39a0a80b01c1fa748a8fce800cbaa1259 Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Tue, 3 May 2016 10:41:44 +0200 Subject: [PATCH] Don't add null or nested children to elements --- lib/process-node-definitions.js | 10 ++++++---- test/html-to-react-tests.js | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/process-node-definitions.js b/lib/process-node-definitions.js index 21d0ede..f5a69f6 100644 --- a/lib/process-node-definitions.js +++ b/lib/process-node-definitions.js @@ -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, }; }; diff --git a/test/html-to-react-tests.js b/test/html-to-react-tests.js index 0d2a740..989d214 100644 --- a/test/html-to-react-tests.js +++ b/test/html-to-react-tests.js @@ -142,6 +142,14 @@ describe('Html2React', function() { assert.equal(reactHtml, htmlInput); }); + it('should not generate children for childless elements', function () { + var htmlInput = '
'; + + 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 = ''; var htmlExpected = ''