Skip to content

Commit 020973e

Browse files
Merge pull request #20 from remarkablemark/bug-script-style
Fix parsing of <script> and <style> tags
2 parents 7522a0a + 9170e92 commit 020973e

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

lib/dom-to-react.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ function domToReact(nodes, options) {
5050
props = attributesToProps(node.attribs);
5151
children = null;
5252

53-
// node type for script is "script" not "tag"
54-
if (node.name === 'script' && node.children[0]) {
55-
// prevent text in script tag from being escaped
53+
// node type for <script> is "script"
54+
// node type for <style> is "style"
55+
if (node.type === 'script' || node.type === 'style') {
56+
// prevent text in <script> or <style> from being escaped
5657
// https://facebook.github.io/react/tips/dangerously-set-inner-html.html
57-
props.dangerouslySetInnerHTML = {
58-
__html: node.children[0].data
59-
};
58+
if (node.children[0]) {
59+
props.dangerouslySetInnerHTML = {
60+
__html: node.children[0].data
61+
};
62+
}
6063

6164
} else if (node.type === 'tag') {
6265
// setting textarea value in children is an antipattern in React

test/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"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>",
10+
"style": "<style>body > .foo { color: #f00; }</style>",
1011
"img": "<img src=\"http://stat.ic/img.jpg\" alt=\"Image\"/>",
1112
"void": "<link/><meta/><img/><br/><hr/><input/>",
1213
"comment": "<!-- comment -->"

test/dom-to-react.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('dom-to-react parser', function() {
5454
);
5555
});
5656

57-
it('converts <script> correctly', function() {
57+
it('does not escape <script> content', function() {
5858
var html = data.html.script;
5959
var reactElement = domToReact(htmlToDOMServer(html));
6060
assert(React.isValidElement(reactElement));
@@ -68,6 +68,20 @@ describe('dom-to-react parser', function() {
6868
);
6969
});
7070

71+
it('does not escape <style> content', function() {
72+
var html = data.html.style;
73+
var reactElement = domToReact(htmlToDOMServer(html));
74+
assert(React.isValidElement(reactElement));
75+
assert.deepEqual(
76+
reactElement,
77+
React.createElement('style', {
78+
dangerouslySetInnerHTML: {
79+
__html: 'body > .foo { color: #f00; }'
80+
}
81+
}, null)
82+
);
83+
});
84+
7185
it('does not have `children` for void elements', function() {
7286
var html = data.html.img;
7387
var reactElement = domToReact(htmlToDOMServer(html));

test/html-to-react.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ describe('html-to-react', function() {
5959
assert.equal(helpers.render(reactElement), html);
6060
});
6161

62+
it('converts empty <script> to React', function() {
63+
var html = '<script></script>';
64+
var reactElement = Parser(html);
65+
assert.equal(helpers.render(reactElement), html);
66+
});
67+
68+
it('converts empty <style> to React', function() {
69+
var html = '<style></style>';
70+
var reactElement = Parser(html);
71+
assert.equal(helpers.render(reactElement), html);
72+
});
73+
6274
it('converts SVG to React', function() {
6375
var svg = data.svg.complex;
6476
var reactElement = Parser(svg);

0 commit comments

Comments
 (0)