Skip to content

Commit 9f54a88

Browse files
committed
Decode HTML entities
1 parent a2a300b commit 9f54a88

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var util = require('util');
22
var Transform = require('stream').Transform;
33
var Parser = require('htmlparser2').Parser;
44
var isEmpty = require('lodash.isempty');
5+
var decode = (new (require('html-entities').AllHtmlEntities)()).decode;
56

67
util.inherits(JSONMLParser, Transform);
78

@@ -36,7 +37,7 @@ JSONMLParser.prototype._createSourceOptions = function() {
3637
transform._parent = element;
3738
},
3839
ontext: function(text) {
39-
transform._parent.push(text);
40+
transform._parent.push(decode(text));
4041
},
4142
oncomment: function(text) {
4243
transform._parent.push(['#comment', text]);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"homepage": "https://github.com/CMTegner/jsonml-parse",
2929
"dependencies": {
3030
"JSONStream": "^0.8.4",
31+
"html-entities": "^1.0.10",
3132
"htmlparser2": "^3.7.3",
3233
"lodash.isempty": "^2.4.1",
3334
"nomnom": "^1.8.0"

test/06-html-entities/expected.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
["button", { "class": "close" }, "×"],
3+
"\n«Yeah, ok»\næøå\n© Awesome Inc."
4+
]

test/06-html-entities/markup.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<button class="close">&times;</button>
2+
&laquo;Yeah, ok&raquo;
3+
&aelig;&oslash;&aring;
4+
&copy; Awesome Inc.

test/cli/expected.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

test/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ test('should parse comments', function(t) {
5757
verify(markup, expected, t);
5858
});
5959

60+
test('should convert HTML entities into unicode characters', function(t) {
61+
var markup = fs.readFileSync(__dirname + '/06-html-entities/markup.html', 'utf8');
62+
var expected = require('./06-html-entities/expected.json');
63+
verify(markup, expected, t);
64+
});
65+
6066
test('should also offer a node-style callback API', function(t) {
6167
parse('<div></div>', function(err, result) {
6268
t.notOk(err);

0 commit comments

Comments
 (0)