Skip to content

Commit e625e1d

Browse files
revert: feat(dom-to-react): skip and do not parse <script> content
This reverts commit 1fb5ee2. The reason for the revert is because removing the <script> from the parsing causes server-side rendering to break. Fixes #115
1 parent 9b3c5ea commit e625e1d

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

lib/dom-to-react.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ function domToReact(nodes, options) {
5656

5757
children = null;
5858

59+
// node type for <script> is "script"
5960
// node type for <style> is "style"
60-
// skip node type "script" as react-dom does not render the contents
61-
if (node.type === 'style') {
62-
// prevent text in <style> from being escaped
63-
// https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
61+
if (node.type === 'script' || node.type === 'style') {
62+
// prevent text in <script> or <style> from being escaped
63+
// https://facebook.github.io/react/tips/dangerously-set-inner-html.html
6464
if (node.children[0]) {
6565
props.dangerouslySetInnerHTML = {
6666
__html: node.children[0].data

test/dom-to-react.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,23 @@ describe('dom-to-react parser', () => {
5656
);
5757
});
5858

59-
it('does not parse <script> content', () => {
59+
it('does not escape <script> content', () => {
6060
const html = data.html.script;
61-
assert.deepEqual(domToReact(htmlToDOM(html)), []);
61+
const reactElement = domToReact(htmlToDOM(html));
62+
63+
assert(React.isValidElement(reactElement));
64+
assert.deepEqual(
65+
reactElement,
66+
React.createElement(
67+
'script',
68+
{
69+
dangerouslySetInnerHTML: {
70+
__html: 'alert(1 < 2);'
71+
}
72+
},
73+
null
74+
)
75+
);
6276
});
6377

6478
it('does not escape <style> content', () => {

test/helpers/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"multiple": "<p>foo</p><p>bar</p>",
55
"nested": "<div><p>foo <em>bar</em></p></div>",
66
"attributes": "<hr id=\"foo\" class=\"bar baz\" style=\"background: #fff; text-align: center;\" data-foo=\"bar\" />",
7-
"complex": "<html><head><meta charSet=\"utf-8\"/><title>Title</title><link rel=\"stylesheet\" href=\"style.css\"/></head><body><header id=\"header\">Header</header><h1 style=\"color:#000;font-size:42px\">Heading</h1><hr/><p>Paragraph</p><img src=\"image.jpg\"/><div class=\"class1 class2\">Some <em>text</em>.</div></body></html>",
7+
"complex": "<html><head><meta charSet=\"utf-8\"/><title>Title</title><link rel=\"stylesheet\" href=\"style.css\"/></head><body><header id=\"header\">Header</header><h1 style=\"color:#000;font-size:42px\">Heading</h1><hr/><p>Paragraph</p><img src=\"image.jpg\"/><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>",
1010
"style": "<style>body > .foo { color: #f00; }</style>",

test/html-to-react.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ describe('html-to-react', () => {
4545
assert.equal(render(reactElement), html);
4646
});
4747

48+
it('converts empty <script> to React', () => {
49+
const html = '<script></script>';
50+
const reactElement = Parser(html);
51+
assert.equal(render(reactElement), html);
52+
});
53+
4854
it('converts empty <style> to React', () => {
4955
const html = '<style></style>';
5056
const reactElement = parse(html);

0 commit comments

Comments
 (0)