Skip to content

Fix <script> and <style> parsing #19

@remarkablemark

Description

@remarkablemark

Current behavior:

var Parser = require('html-react-parser');

Parser('<script>1 < 2;</script>'); // works
Parser('<script></script>'); // returns `[]`
Parser('<style></style>'); // does not work

The error stems from the flawed logic here. The if-check for node.children[0] needs to be separated and there needs to be a check for style tag as well.

React's Dangerously Set innerHTML needs to be used for setting style content (similar to script) to prevent CSS from being escaped.

var React = require('react');
var render = require('react-dom/server').renderToStaticMarkup;

render(React.createElement('style', {}, 'div > p { font-family: "Open Sans"; }'));
// '<style>div &gt; p { font-family: &quot;Open Sans&quot;; }</style>'

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions