From e78b9e2f411b8e603bcc380e3160048d02f0d097 Mon Sep 17 00:00:00 2001 From: Mike Nikles Date: Sat, 30 Jan 2016 22:01:42 -0800 Subject: [PATCH 1/2] Deal with void element tags --- lib/process-node-definitions.js | 14 ++++++++++++-- test/html-to-react-tests.js | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/process-node-definitions.js b/lib/process-node-definitions.js index 433696e..e512ba4 100644 --- a/lib/process-node-definitions.js +++ b/lib/process-node-definitions.js @@ -2,6 +2,12 @@ var _ = require('lodash'); +// https://github.com/facebook/react/blob/0.14-stable/src/renderers/dom/shared/ReactDOMComponent.js#L457 +var voidElementTags = [ + 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', + 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' +]; + function createStyleJsonFromString(styleString) { if (_.isNull(styleString) || _.isUndefined(styleString) || styleString === '') { return {}; @@ -51,7 +57,12 @@ var ProcessNodeDefinitions = function(React) { }); } - return React.createElement(node.name, elementProps, node.data, children); + if (_.contains(voidElementTags, node.name)) { + return React.createElement(node.name, elementProps) + } else { + return React.createElement(node.name, elementProps, node.data, children); + } + } return { @@ -60,4 +71,3 @@ var ProcessNodeDefinitions = function(React) { }; module.exports = ProcessNodeDefinitions; - diff --git a/test/html-to-react-tests.js b/test/html-to-react-tests.js index 5ca6afe..7ffcbd9 100644 --- a/test/html-to-react-tests.js +++ b/test/html-to-react-tests.js @@ -101,6 +101,25 @@ describe('Html2React', function() { assert.equal(reactHtml, htmlExpected); }); + + it('should parse br elements without warnings', function() { + var htmlInput = '

Line one
Line two
Line three

'; + var htmlExpected = '

Line one
Line two
Line three

'; + + var reactComponent = parser.parse(htmlInput); + var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent); + + assert.equal(reactHtml, htmlExpected); + }); + + it('should parse src elements with all attributes but without warnings', function() { + var htmlInput = '

'; + + var reactComponent = parser.parse(htmlInput); + var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent); + + assert.equal(reactHtml, htmlInput); + }); }); describe('parse invalid HTML', function() { From c3f0433c3b46c3024af4ae2674f077d19c3b9b8b Mon Sep 17 00:00:00 2001 From: Mike Nikles Date: Sat, 6 Feb 2016 15:10:46 -0800 Subject: [PATCH 2/2] Update based on feedback --- test/html-to-react-tests.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/html-to-react-tests.js b/test/html-to-react-tests.js index 7ffcbd9..b2ea089 100644 --- a/test/html-to-react-tests.js +++ b/test/html-to-react-tests.js @@ -112,7 +112,14 @@ describe('Html2React', function() { assert.equal(reactHtml, htmlExpected); }); - it('should parse src elements with all attributes but without warnings', function() { + it('should not generate children for br tags', function() { + var htmlInput = '
'; + + var reactComponent = parser.parse(htmlInput); + assert.strictEqual((reactComponent.props.children || []).length, 0); + }); + + it('should parse void elements with all attributes and no warnings', function() { var htmlInput = '

'; var reactComponent = parser.parse(htmlInput);