Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from aknuds1/decode-text
Browse files Browse the repository at this point in the history
Decode text nodes before passing to React
  • Loading branch information
Mike Nikles committed Apr 25, 2016
2 parents 273f4a4 + c365291 commit f1efdbb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/process-node-definitions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var _ = require('lodash');
var ent = require('ent');

// https://github.com/facebook/react/blob/0.14-stable/src/renderers/dom/shared/ReactDOMComponent.js#L457
var voidElementTags = [
Expand Down Expand Up @@ -30,7 +31,7 @@ var ProcessNodeDefinitions = function(React) {

function processDefaultNode(node, children) {
if (node.type === 'text') {
return node.data;
return ent.decode(node.data);
} else if (node.type === 'comment') {
// FIXME: The following doesn't work as the generated HTML results in "<!-- This is a comment -->"
//return '<!-- ' + node.data + ' -->';
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
}
},
"dependencies": {
"ent": "^2.2.0",
"htmlparser2": "^3.8.3",
"lodash": "^3.9.3"
},
Expand Down
11 changes: 10 additions & 1 deletion test/html-to-react-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var assert = require("assert");
var assert = require('assert');
var React = require('react');
var ReactDOMServer = require('react-dom/server')

Expand Down Expand Up @@ -131,6 +131,15 @@ describe('Html2React', function() {

assert.equal(reactHtml, htmlInput);
});

it('should decode character entities in text nodes', function () {
var htmlInput = '<div>1 &lt; 2</div>';

var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);

assert.equal(reactHtml, htmlInput);
});
});

describe('parse invalid HTML', function() {
Expand Down

0 comments on commit f1efdbb

Please sign in to comment.