Skip to content

Commit

Permalink
Decode HTML entities by default on node/server
Browse files Browse the repository at this point in the history
Set parser options `decodeEntities` to true for `html-dom-parser`,
which gets passed to `htmlparser2`.

Write test to confirm that entities passed to React element's
children are decoded (but they will still be encoded when rendered
by ReactDOMServer).
  • Loading branch information
remarkablemark committed Jan 17, 2017
1 parent 99870f3 commit 68a5169
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -6,6 +6,9 @@
var domToReact = require('./lib/dom-to-react');
var htmlToDOM = require('html-dom-parser');

// decode HTML entities by default for `htmlparser2`
var domParserOptions = { decodeEntities: true };

/**
* Convert HTML string to React elements.
*
Expand All @@ -18,7 +21,7 @@ function HTMLReactParser(html, options) {
if (typeof html !== 'string') {
throw new TypeError('First argument must be a string');
}
return domToReact(htmlToDOM(html), options);
return domToReact(htmlToDOM(html, domParserOptions), options);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/html-to-react.js
Expand Up @@ -78,6 +78,13 @@ describe('html-to-react', function() {
assert.equal(render(reactElement), svg);
});

it('decodes HTML entities', function() {
var encodedEntities = 'asdf & ÿ ü '';
var decodedEntities = 'asdf & 每 眉 \'';
var reactElement = Parser('<i>' + encodedEntities + '</i>');
assert.equal(reactElement.props.children, decodedEntities);
});

});

/**
Expand Down

0 comments on commit 68a5169

Please sign in to comment.