Last change
on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | var punycode = require('punycode');
|
---|
2 | var entities = require('./entities.json');
|
---|
3 |
|
---|
4 | module.exports = decode;
|
---|
5 |
|
---|
6 | function decode (str) {
|
---|
7 | if (typeof str !== 'string') {
|
---|
8 | throw new TypeError('Expected a String');
|
---|
9 | }
|
---|
10 |
|
---|
11 | return str.replace(/&(#?[^;\W]+;?)/g, function (_, match) {
|
---|
12 | var m;
|
---|
13 | if (m = /^#(\d+);?$/.exec(match)) {
|
---|
14 | return punycode.ucs2.encode([ parseInt(m[1], 10) ]);
|
---|
15 | } else if (m = /^#[Xx]([A-Fa-f0-9]+);?/.exec(match)) {
|
---|
16 | return punycode.ucs2.encode([ parseInt(m[1], 16) ]);
|
---|
17 | } else {
|
---|
18 | // named entity
|
---|
19 | var hasSemi = /;$/.test(match);
|
---|
20 | var withoutSemi = hasSemi ? match.replace(/;$/, '') : match;
|
---|
21 | var target = entities[withoutSemi] || (hasSemi && entities[match]);
|
---|
22 |
|
---|
23 | if (typeof target === 'number') {
|
---|
24 | return punycode.ucs2.encode([ target ]);
|
---|
25 | } else if (typeof target === 'string') {
|
---|
26 | return target;
|
---|
27 | } else {
|
---|
28 | return '&' + match;
|
---|
29 | }
|
---|
30 | }
|
---|
31 | });
|
---|
32 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.