Skip to content

Commit 2a12730

Browse files
Make sure props.children is null for void elements like <img>
The error came from the fact that `props.children` would always be at minimum an empty array `[]`. This causes the following error: ```js ReactDOMServer.renderToString( Parser('<img />') ); // Invariant Violation: img is a void element tag and must neither // have `children` nor use `dangerouslySetInnerHTML`. ``` The fix was to make `props.children` equal to `null` unless there were actually more DOM nodes inside the array. Add tests to confirm that void HTML element tags no longer throws an error.
1 parent 15d92ab commit 2a12730

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

lib/dom-to-react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function domToReact(nodes, options) {
5959
if (node.name === 'textarea' && node.children[0]) {
6060
props.defaultValue = node.children[0].data;
6161

62-
} else if (node.children) {
63-
// continue recursion of creating React elements
62+
// continue recursion of creating React elements (if applicable)
63+
} else if (node.children && node.children.length) {
6464
children = domToReact(node.children, options);
6565
}
6666

test/data.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"complex": "<html><head><title>Title</title></head><body><header id=\"header\">Header</header><h1 style=\"color:#000;font-size:42px;\">Heading</h1><p>Paragraph</p><div class=\"class1 class2\">Some <em>text</em>.</div><script>alert();</script></body></html>",
88
"textarea": "<textarea>foo</textarea>",
99
"script": "<script>alert(1 < 2);</script>",
10+
"img": "<img src=\"http://stat.ic/img.jpg\" alt=\"Image\"/>",
11+
"void": "<link/><meta/><img/><br/><hr/><input/>",
1012
"comment": "<!-- comment -->"
1113
},
1214
"svg": {

test/dom-to-react.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
var assert = require('assert');
77
var React = require('react');
8+
var ReactDOMServer = require('react-dom/server');
89
var htmlToDOMServer = require('../lib/html-to-dom-server');
910
var domToReact = require('../lib/dom-to-react');
1011
var data = require('./data');
@@ -67,6 +68,22 @@ describe('dom-to-react parser', function() {
6768
);
6869
});
6970

71+
it('does not have `children` for void elements', function() {
72+
var html = data.html.img;
73+
var reactElement = domToReact(htmlToDOMServer(html));
74+
assert(!reactElement.props.children);
75+
});
76+
77+
it('does not throw an error for void elements', function() {
78+
var html = data.html.void;
79+
var reactElements = domToReact(htmlToDOMServer(html));
80+
assert.doesNotThrow(function() {
81+
ReactDOMServer.renderToStaticMarkup(
82+
React.createElement('div', {}, reactElements)
83+
);
84+
});
85+
});
86+
7087
it('skips HTML comments', function() {
7188
var html = [data.html.single, data.html.comment, data.html.single].join('');
7289
var reactElements = domToReact(htmlToDOMServer(html));

0 commit comments

Comments
 (0)