Skip to content

Commit b3d5f7b

Browse files
Merge pull request #116 from remarkablemark/fix/script
fix: revert commit so <script> is parsed or else server-side rendering breaks
2 parents 9b3c5ea + fecd5d0 commit b3d5f7b

File tree

7 files changed

+40
-10
lines changed

7 files changed

+40
-10
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,19 @@ Run tests with coverage:
223223
$ npm run test:coverage
224224
```
225225

226+
View coverage in browser:
227+
228+
```sh
229+
$ npm run test:coverage
230+
$ npm run test:coverage:report
231+
$ open coverage/index.html
232+
```
233+
226234
Lint files:
227235

228236
```sh
229237
$ npm run lint
238+
$ npm run dtslint
230239
```
231240

232241
Fix lint errors:

lib/attributes-to-props.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var DOMProperty = require('react-dom-core/lib/DOMProperty');
22
var propertyConfig = require('./property-config');
33
var styleToObject = require('style-to-object');
44
var utilities = require('./utilities');
5+
var camelCase = utilities.camelCase;
56

67
var config = propertyConfig.config;
78
var isCustomAttribute = propertyConfig.HTMLDOMPropertyConfig.isCustomAttribute;
@@ -79,7 +80,7 @@ function cssToJs(style) {
7980
styleToObject(style, function(propName, propValue) {
8081
// Check if it's not a comment node
8182
if (propName && propValue) {
82-
styleObj[utilities.camelCase(propName)] = propValue;
83+
styleObj[camelCase(propName)] = propValue;
8384
}
8485
});
8586
return styleObj;

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "html-react-parser",
33
"version": "0.8.0",
4-
"description": "An HTML to React parser.",
4+
"description": "HTML to React parser.",
55
"author": "Mark <mark@remarkablemark.org>",
66
"main": "index.js",
77
"scripts": {
@@ -14,7 +14,7 @@
1414
"lint": "eslint --ignore-path .gitignore .",
1515
"lint:fix": "npm run lint -- --fix",
1616
"dtslint": "dtslint .",
17-
"prepublishOnly": "npm run build",
17+
"prepublishOnly": "npm run lint && npm run dtslint && npm test && npm run build",
1818
"release": "standard-version --no-verify",
1919
"test": "mocha",
2020
"test:coverage": "nyc npm test",

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 = parse(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)