Skip to content

Commit 0ed0183

Browse files
SamyPesseSoreine
authored andcommitted
Fix decoding of HTML entities (emojis) (#105)
* Add failing tests * Use entities instead of html-entities * Add test for rendering emoji to markdown * Use only decodeHTML * Only decodeHTML
1 parent af9f3b6 commit 0ed0183

File tree

12 files changed

+47
-21
lines changed

12 files changed

+47
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
"dependencies": {
4848
"asciidoctor.js": "^1.5.5-4",
4949
"detect-newline": "^2.1.0",
50+
"entities": "^1.1.1",
5051
"escape-string-regexp": "^1.0.5",
5152
"front-matter": "^2.1.2",
52-
"html-entities": "1.2.0",
5353
"htmlclean": "^2.7.8",
5454
"htmlparser2": "3.9.2",
5555
"indent-string": "2.1.0",

src/asciidoc/inlines/escape.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
const entities = require('html-entities');
1+
const entities = require('entities');
22
const { Map } = require('immutable');
33
const { Serializer, MARKS } = require('../../');
44
const { escapeWith } = require('../../utils/escape');
55

6-
const xmlEntities = new entities.XmlEntities();
7-
86
// Replacements for Asciidoc escaping
97
const REPLACEMENTS = Map([
108
[ '*', '\\*' ],
@@ -29,7 +27,7 @@ const REPLACEMENTS = Map([
2927
*/
3028
function escape(text) {
3129
text = escapeWith(REPLACEMENTS, text);
32-
return xmlEntities.encode(text);
30+
return entities.encodeXML(text);
3331
}
3432

3533
/**

src/html/escape.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
const entities = require('html-entities');
2-
const htmlEntities = new entities.AllHtmlEntities();
1+
const entities = require('entities');
32

43
/**
54
* Escape all entities (HTML + XML)
65
* @param {String} str
76
* @return {String}
87
*/
98
function escape(str) {
10-
return htmlEntities.encode(str);
9+
return entities.encodeXML(str);
1110
}
1211

1312
module.exports = escape;

src/html/unescape.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
const entities = require('html-entities');
2-
const htmlEntities = new entities.AllHtmlEntities();
1+
const entities = require('entities');
32

43
/**
54
* Unescape all entities (HTML + XML)
65
* @param {String} str
76
* @return {String}
87
*/
98
function unescape(str) {
10-
return htmlEntities.decode(str);
9+
str = entities.decodeHTML(str);
10+
11+
return str;
1112
}
1213

1314
module.exports = unescape;

src/markdown/utils.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
const { Map } = require('immutable');
2-
const entities = require('html-entities');
2+
const entities = require('entities');
33
const { escapeWith, unescapeWith } = require('../utils/escape');
44

5-
const htmlEntities = new entities.AllHtmlEntities();
6-
const xmlEntities = new entities.XmlEntities();
7-
85
// Replacements for Markdown escaping
96
// See http://spec.commonmark.org/0.15/#backslash-escapes
107
const REPLACEMENTS_ESCAPE = Map([
@@ -48,7 +45,7 @@ const URL_REPLACEMENTS_ESCAPE = Map([
4845
*/
4946
function escapeMarkdown(str, escapeXML) {
5047
str = escapeWith(REPLACEMENTS_ESCAPE, str);
51-
return escapeXML === false ? str : xmlEntities.encode(str);
48+
return escapeXML === false ? str : entities.encodeXML(str);
5249
}
5350

5451
/**
@@ -60,7 +57,9 @@ function escapeMarkdown(str, escapeXML) {
6057
*/
6158
function unescapeMarkdown(str) {
6259
str = unescapeWith(REPLACEMENTS_UNESCAPE, str);
63-
return htmlEntities.decode(str);
60+
str = entities.decodeHTML(str);
61+
62+
return str;
6463
}
6564

6665
/**

test/from-html/entities/input.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<body>
2+
<p>Hello world &#128268;</p>
3+
</body>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
document:
2+
nodes:
3+
- object: block
4+
type: paragraph
5+
nodes:
6+
- object: text
7+
leaves:
8+
- text: 'Hello world 🔌'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world &#128268;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
document:
2+
data: {}
3+
object: document
4+
nodes:
5+
- object: block
6+
type: paragraph
7+
nodes:
8+
- object: text
9+
leaves:
10+
- object: leaf
11+
marks: []
12+
text: 'Hello world 🔌'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
document:
2+
nodes:
3+
- object: block
4+
type: paragraph
5+
nodes:
6+
- object: text
7+
leaves:
8+
- text: Hello World 🚀

0 commit comments

Comments
 (0)