Skip to content

Commit 3f830f2

Browse files
rexxarswooorm
andcommitted
Use document instead of bundled map in browsers
Closes GH-14. Reviewed-by: Titus Wormer <[email protected]> Co-authored-by: Titus Wormer <[email protected]>
1 parent 668d1fc commit 3f830f2

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

.travis.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
language: node_js
22
node_js:
3-
- lts/boron
4-
- node
3+
- lts/boron
4+
- node
5+
addons:
6+
apt:
7+
packages:
8+
- xvfb
9+
install:
10+
- export DISPLAY=':99.0'
11+
- Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
12+
- npm install
13+
script:
14+
- npm test
15+
- npm run test-browser -- --browser=firefox
516
after_script: bash <(curl -s https://codecov.io/bash)
617
deploy:
718
provider: releases

decode-entity.browser.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
/* eslint-env browser */
4+
5+
var el
6+
7+
module.exports = decodeEntity
8+
9+
function decodeEntity(characters) {
10+
var char
11+
var entity
12+
13+
el = el || document.createElement('i')
14+
entity = '&' + characters + ';'
15+
el.innerHTML = entity
16+
char = el.textContent
17+
18+
// Some entities do not require the closing semicolon (&not - for instance),
19+
// which leads to situations where parsing the assumed entity of &notit; will
20+
// result in the string `¬it;`. When we encounter a trailing semicolon after
21+
// parsing and the entity to decode was not a semicolon (&semi;), we can
22+
// assume that the matching was incomplete
23+
if (char.slice(-1) === ';' && characters !== 'semi') {
24+
return false
25+
}
26+
27+
// If the decoded string is equal to the input, the entity was not valid
28+
return char === entity ? false : char
29+
}

decode-entity.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
var characterEntities = require('character-entities')
4+
5+
module.exports = decodeEntity
6+
7+
var own = {}.hasOwnProperty
8+
9+
function decodeEntity(characters) {
10+
return own.call(characterEntities, characters)
11+
? characterEntities[characters]
12+
: false
13+
}

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict'
22

3-
var characterEntities = require('character-entities')
43
var legacy = require('character-entities-legacy')
54
var invalid = require('character-reference-invalid')
65
var decimal = require('is-decimal')
76
var hexadecimal = require('is-hexadecimal')
87
var alphanumerical = require('is-alphanumerical')
8+
var decodeEntity = require('./decode-entity')
99

1010
module.exports = parseEntities
1111

@@ -115,6 +115,7 @@ function parse(value, settings) {
115115
var queue = ''
116116
var result = []
117117
var entityCharacters
118+
var namedEntity
118119
var terminated
119120
var characters
120121
var character
@@ -246,9 +247,11 @@ function parse(value, settings) {
246247
if (terminated) {
247248
end++
248249

249-
if (type === NAMED && own.call(characterEntities, characters)) {
250+
namedEntity = type === NAMED ? decodeEntity(characters) : false
251+
252+
if (namedEntity) {
250253
entityCharacters = characters
251-
entity = characterEntities[characters]
254+
entity = namedEntity
252255
}
253256
}
254257

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
"contributors": [
1818
"Titus Wormer <[email protected]> (http://wooorm.com)"
1919
],
20+
"browser": {
21+
"./decode-entity.js": "./decode-entity.browser.js"
22+
},
2023
"files": [
21-
"index.js"
24+
"index.js",
25+
"decode-entity.js",
26+
"decode-entity.browser.js"
2227
],
2328
"dependencies": {
2429
"character-entities": "^1.0.0",
@@ -36,6 +41,7 @@
3641
"remark-cli": "^5.0.0",
3742
"remark-preset-wooorm": "^4.0.0",
3843
"tape": "^4.2.0",
44+
"tape-run": "^4.0.0",
3945
"xo": "^0.20.0"
4046
},
4147
"scripts": {
@@ -45,7 +51,8 @@
4551
"build": "npm run build-bundle && npm run build-mangle",
4652
"test-api": "node test",
4753
"test-coverage": "nyc --reporter lcov tape test.js",
48-
"test": "npm run format && npm run build && npm run test-coverage"
54+
"test-browser": "browserify test.js | tape-run",
55+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-browser"
4956
},
5057
"nyc": {
5158
"check-coverage": true,

0 commit comments

Comments
 (0)