Skip to content

Commit 1fb5ee2

Browse files
feat(dom-to-react): skip and do not parse <script> content
The "script" node is skipped since `react-dom` was never really rendering the contents. Update tests.
1 parent 471ddfe commit 1fb5ee2

File tree

4 files changed

+7
-27
lines changed

4 files changed

+7
-27
lines changed

lib/dom-to-react.js

+4-4
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"
6059
// node type for <style> is "style"
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
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
6464
if (node.children[0]) {
6565
props.dangerouslySetInnerHTML = {
6666
__html: node.children[0].data

test/dom-to-react.js

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

59-
it('does not escape <script> content', () => {
59+
it('does not parse <script> content', () => {
6060
const html = data.html.script;
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-
);
61+
assert.deepEqual(domToReact(htmlToDOM(html)), []);
7662
});
7763

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

test/helpers/data.json

+1-1
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><script>alert();</script></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></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

-6
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ 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-
5448
it('converts empty <style> to React', () => {
5549
const html = '<style></style>';
5650
const reactElement = Parser(html);

0 commit comments

Comments
 (0)