entities 1.1.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ export declare const decodeXML: (str: string) => string;
2
+ export declare const decodeHTMLStrict: (str: string) => string;
3
+ export interface MapType {
4
+ [key: string]: string;
5
+ }
6
+ export declare const decodeHTML: (str: string) => string;
7
+ //# sourceMappingURL=decode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,SAAS,yBAA2B,CAAC;AAClD,eAAO,MAAM,gBAAgB,yBAA8B,CAAC;AAE5D,MAAM,WAAW,OAAO;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACzB;AAeD,eAAO,MAAM,UAAU,yBA0BnB,CAAC"}
package/lib/decode.js CHANGED
@@ -1,72 +1,53 @@
1
- var entityMap = require("../maps/entities.json"),
2
- legacyMap = require("../maps/legacy.json"),
3
- xmlMap = require("../maps/xml.json"),
4
- decodeCodePoint = require("./decode_codepoint.js");
5
-
6
- var decodeXMLStrict = getStrictDecoder(xmlMap),
7
- decodeHTMLStrict = getStrictDecoder(entityMap);
8
-
9
- function getStrictDecoder(map){
10
- var keys = Object.keys(map).join("|"),
11
- replace = getReplacer(map);
12
-
13
- keys += "|#[xX][\\da-fA-F]+|#\\d+";
14
-
15
- var re = new RegExp("&(?:" + keys + ");", "g");
16
-
17
- return function(str){
18
- return String(str).replace(re, replace);
19
- };
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var entities_json_1 = __importDefault(require("./maps/entities.json"));
7
+ var legacy_json_1 = __importDefault(require("./maps/legacy.json"));
8
+ var xml_json_1 = __importDefault(require("./maps/xml.json"));
9
+ var decode_codepoint_1 = __importDefault(require("./decode_codepoint"));
10
+ exports.decodeXML = getStrictDecoder(xml_json_1.default);
11
+ exports.decodeHTMLStrict = getStrictDecoder(entities_json_1.default);
12
+ function getStrictDecoder(map) {
13
+ var keys = Object.keys(map).join("|");
14
+ var replace = getReplacer(map);
15
+ keys += "|#[xX][\\da-fA-F]+|#\\d+";
16
+ var re = new RegExp("&(?:" + keys + ");", "g");
17
+ return function (str) { return String(str).replace(re, replace); };
20
18
  }
21
-
22
- var decodeHTML = (function(){
23
- var legacy = Object.keys(legacyMap)
24
- .sort(sorter);
25
-
26
- var keys = Object.keys(entityMap)
27
- .sort(sorter);
28
-
29
- for(var i = 0, j = 0; i < keys.length; i++){
30
- if(legacy[j] === keys[i]){
31
- keys[i] += ";?";
32
- j++;
33
- } else {
34
- keys[i] += ";";
35
- }
36
- }
37
-
38
- var re = new RegExp("&(?:" + keys.join("|") + "|#[xX][\\da-fA-F]+;?|#\\d+;?)", "g"),
39
- replace = getReplacer(entityMap);
40
-
41
- function replacer(str){
42
- if(str.substr(-1) !== ";") str += ";";
43
- return replace(str);
44
- }
45
-
46
- //TODO consider creating a merged map
47
- return function(str){
48
- return String(str).replace(re, replacer);
49
- };
50
- }());
51
-
52
- function sorter(a, b){
53
- return a < b ? 1 : -1;
19
+ var sorter = function (a, b) { return (a < b ? 1 : -1); };
20
+ exports.decodeHTML = (function () {
21
+ var legacy = Object.keys(legacy_json_1.default).sort(sorter);
22
+ var keys = Object.keys(entities_json_1.default).sort(sorter);
23
+ for (var i = 0, j = 0; i < keys.length; i++) {
24
+ if (legacy[j] === keys[i]) {
25
+ keys[i] += ";?";
26
+ j++;
27
+ }
28
+ else {
29
+ keys[i] += ";";
30
+ }
31
+ }
32
+ var re = new RegExp("&(?:" + keys.join("|") + "|#[xX][\\da-fA-F]+;?|#\\d+;?)", "g");
33
+ var replace = getReplacer(entities_json_1.default);
34
+ function replacer(str) {
35
+ if (str.substr(-1) !== ";")
36
+ str += ";";
37
+ return replace(str);
38
+ }
39
+ //TODO consider creating a merged map
40
+ return function (str) { return String(str).replace(re, replacer); };
41
+ })();
42
+ function getReplacer(map) {
43
+ return function replace(str) {
44
+ if (str.charAt(1) === "#") {
45
+ var secondChar = str.charAt(2);
46
+ if (secondChar === "X" || secondChar === "x") {
47
+ return decode_codepoint_1.default(parseInt(str.substr(3), 16));
48
+ }
49
+ return decode_codepoint_1.default(parseInt(str.substr(2), 10));
50
+ }
51
+ return map[str.slice(1, -1)];
52
+ };
54
53
  }
55
-
56
- function getReplacer(map){
57
- return function replace(str){
58
- if(str.charAt(1) === "#"){
59
- if(str.charAt(2) === "X" || str.charAt(2) === "x"){
60
- return decodeCodePoint(parseInt(str.substr(3), 16));
61
- }
62
- return decodeCodePoint(parseInt(str.substr(2), 10));
63
- }
64
- return map[str.slice(1, -1)];
65
- };
66
- }
67
-
68
- module.exports = {
69
- XML: decodeXMLStrict,
70
- HTML: decodeHTML,
71
- HTMLStrict: decodeHTMLStrict
72
- };
@@ -0,0 +1,2 @@
1
+ export default function decodeCodePoint(codePoint: number): string;
2
+ //# sourceMappingURL=decode_codepoint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode_codepoint.d.ts","sourceRoot":"","sources":["../src/decode_codepoint.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,OAAO,UAAU,eAAe,CAAC,SAAS,EAAE,MAAM,UAmBxD"}
@@ -1,26 +1,24 @@
1
- var decodeMap = require("../maps/decode.json");
2
-
3
- module.exports = decodeCodePoint;
4
-
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var decode_json_1 = __importDefault(require("./maps/decode.json"));
5
7
  // modified version of https://github.com/mathiasbynens/he/blob/master/src/he.js#L94-L119
6
- function decodeCodePoint(codePoint){
7
-
8
- if((codePoint >= 0xD800 && codePoint <= 0xDFFF) || codePoint > 0x10FFFF){
9
- return "\uFFFD";
10
- }
11
-
12
- if(codePoint in decodeMap){
13
- codePoint = decodeMap[codePoint];
14
- }
15
-
16
- var output = "";
17
-
18
- if(codePoint > 0xFFFF){
19
- codePoint -= 0x10000;
20
- output += String.fromCharCode(codePoint >>> 10 & 0x3FF | 0xD800);
21
- codePoint = 0xDC00 | codePoint & 0x3FF;
22
- }
23
-
24
- output += String.fromCharCode(codePoint);
25
- return output;
8
+ function decodeCodePoint(codePoint) {
9
+ if ((codePoint >= 0xd800 && codePoint <= 0xdfff) || codePoint > 0x10ffff) {
10
+ return "\uFFFD";
11
+ }
12
+ if (codePoint in decode_json_1.default) {
13
+ codePoint = decode_json_1.default[codePoint];
14
+ }
15
+ var output = "";
16
+ if (codePoint > 0xffff) {
17
+ codePoint -= 0x10000;
18
+ output += String.fromCharCode(((codePoint >>> 10) & 0x3ff) | 0xd800);
19
+ codePoint = 0xdc00 | (codePoint & 0x3ff);
20
+ }
21
+ output += String.fromCharCode(codePoint);
22
+ return output;
26
23
  }
24
+ exports.default = decodeCodePoint;
@@ -0,0 +1,4 @@
1
+ export declare const encodeXML: (data: string) => string;
2
+ export declare const encodeHTML: (data: string) => string;
3
+ export declare function escape(data: string): string;
4
+ //# sourceMappingURL=encode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../src/encode.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,SAAS,0BAAsC,CAAC;AAO7D,eAAO,MAAM,UAAU,0BAAwC,CAAC;AAoEhE,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,UAIlC"}
package/lib/encode.js CHANGED
@@ -1,73 +1,72 @@
1
- var inverseXML = getInverseObj(require("../maps/xml.json")),
2
- xmlReplacer = getInverseReplacer(inverseXML);
3
-
4
- exports.XML = getInverse(inverseXML, xmlReplacer);
5
-
6
- var inverseHTML = getInverseObj(require("../maps/entities.json")),
7
- htmlReplacer = getInverseReplacer(inverseHTML);
8
-
9
- exports.HTML = getInverse(inverseHTML, htmlReplacer);
10
-
11
- function getInverseObj(obj){
12
- return Object.keys(obj).sort().reduce(function(inverse, name){
13
- inverse[obj[name]] = "&" + name + ";";
14
- return inverse;
15
- }, {});
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var xml_json_1 = __importDefault(require("./maps/xml.json"));
7
+ var inverseXML = getInverseObj(xml_json_1.default);
8
+ var xmlReplacer = getInverseReplacer(inverseXML);
9
+ exports.encodeXML = getInverse(inverseXML, xmlReplacer);
10
+ var entities_json_1 = __importDefault(require("./maps/entities.json"));
11
+ var inverseHTML = getInverseObj(entities_json_1.default);
12
+ var htmlReplacer = getInverseReplacer(inverseHTML);
13
+ exports.encodeHTML = getInverse(inverseHTML, htmlReplacer);
14
+ function getInverseObj(obj) {
15
+ return Object.keys(obj)
16
+ .sort()
17
+ .reduce(function (inverse, name) {
18
+ inverse[obj[name]] = "&" + name + ";";
19
+ return inverse;
20
+ }, {});
16
21
  }
17
-
18
- function getInverseReplacer(inverse){
19
- var single = [],
20
- multiple = [];
21
-
22
- Object.keys(inverse).forEach(function(k){
23
- if(k.length === 1){
24
- single.push("\\" + k);
25
- } else {
26
- multiple.push(k);
27
- }
28
- });
29
-
30
- //TODO add ranges
31
- multiple.unshift("[" + single.join("") + "]");
32
-
33
- return new RegExp(multiple.join("|"), "g");
22
+ function getInverseReplacer(inverse) {
23
+ var single = [];
24
+ var multiple = [];
25
+ for (var _i = 0, _a = Object.keys(inverse); _i < _a.length; _i++) {
26
+ var k = _a[_i];
27
+ if (k.length === 1) {
28
+ // Add value to single array
29
+ single.push("\\" + k);
30
+ }
31
+ else {
32
+ // Add value to multiple array
33
+ multiple.push(k);
34
+ }
35
+ }
36
+ // Add ranges to single characters.
37
+ single.sort();
38
+ for (var start = 0; start < single.length - 1; start++) {
39
+ // Find the end of a run of characters
40
+ var end = start;
41
+ while (end < single.length - 1 &&
42
+ single[end].charCodeAt(1) + 1 === single[end + 1].charCodeAt(1)) {
43
+ end += 1;
44
+ }
45
+ var count = 1 + end - start;
46
+ // We want to replace at least three characters
47
+ if (count < 3)
48
+ continue;
49
+ single.splice(start, count, single[start] + "-" + single[end]);
50
+ }
51
+ multiple.unshift("[" + single.join("") + "]");
52
+ return new RegExp(multiple.join("|"), "g");
34
53
  }
35
-
36
- var re_nonASCII = /[^\0-\x7F]/g,
37
- re_astralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
38
-
39
- function singleCharReplacer(c){
40
- return "&#x" + c.charCodeAt(0).toString(16).toUpperCase() + ";";
54
+ var reNonASCII = /[^\0-\x7F]/gu;
55
+ function singleCharReplacer(c) {
56
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
57
+ return "&#x" + c.codePointAt(0).toString(16).toUpperCase() + ";";
41
58
  }
42
-
43
- function astralReplacer(c){
44
- // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
45
- var high = c.charCodeAt(0);
46
- var low = c.charCodeAt(1);
47
- var codePoint = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;
48
- return "&#x" + codePoint.toString(16).toUpperCase() + ";";
59
+ function getInverse(inverse, re) {
60
+ return function (data) {
61
+ return data
62
+ .replace(re, function (name) { return inverse[name]; })
63
+ .replace(reNonASCII, singleCharReplacer);
64
+ };
49
65
  }
50
-
51
- function getInverse(inverse, re){
52
- function func(name){
53
- return inverse[name];
54
- }
55
-
56
- return function(data){
57
- return data
58
- .replace(re, func)
59
- .replace(re_astralSymbols, astralReplacer)
60
- .replace(re_nonASCII, singleCharReplacer);
61
- };
66
+ var reXmlChars = getInverseReplacer(inverseXML);
67
+ function escape(data) {
68
+ return data
69
+ .replace(reXmlChars, singleCharReplacer)
70
+ .replace(reNonASCII, singleCharReplacer);
62
71
  }
63
-
64
- var re_xmlChars = getInverseReplacer(inverseXML);
65
-
66
- function escapeXML(data){
67
- return data
68
- .replace(re_xmlChars, singleCharReplacer)
69
- .replace(re_astralSymbols, astralReplacer)
70
- .replace(re_nonASCII, singleCharReplacer);
71
- }
72
-
73
- exports.escape = escapeXML;
72
+ exports.escape = escape;
package/lib/index.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Decodes a string with entities.
3
+ *
4
+ * @param data String to decode.
5
+ * @param level Optional level to decode at. 0 = XML, 1 = HTML. Default is 0.
6
+ */
7
+ export declare function decode(data: string, level?: number): string;
8
+ /**
9
+ * Decodes a string with entities. Does not allow missing trailing semicolons for entities.
10
+ *
11
+ * @param data String to decode.
12
+ * @param level Optional level to decode at. 0 = XML, 1 = HTML. Default is 0.
13
+ */
14
+ export declare function decodeStrict(data: string, level?: number): string;
15
+ /**
16
+ * Encodes a string with entities.
17
+ *
18
+ * @param data String to encode.
19
+ * @param level Optional level to encode at. 0 = XML, 1 = HTML. Default is 0.
20
+ */
21
+ export declare function encode(data: string, level?: number): string;
22
+ export { encodeXML, encodeHTML, escape, encodeHTML as encodeHTML4, encodeHTML as encodeHTML5, } from "./encode";
23
+ export { decodeXML, decodeHTML, decodeHTMLStrict, decodeHTML as decodeHTML4, decodeHTML as decodeHTML5, decodeHTMLStrict as decodeHTML4Strict, decodeHTMLStrict as decodeHTML5Strict, decodeXML as decodeXMLStrict, } from "./decode";
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAE3D;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAEjE;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAE3D;AAED,OAAO,EACH,SAAS,EACT,UAAU,EACV,MAAM,EAEN,UAAU,IAAI,WAAW,EACzB,UAAU,IAAI,WAAW,GAC5B,MAAM,UAAU,CAAC;AAElB,OAAO,EACH,SAAS,EACT,UAAU,EACV,gBAAgB,EAEhB,UAAU,IAAI,WAAW,EACzB,UAAU,IAAI,WAAW,EACzB,gBAAgB,IAAI,iBAAiB,EACrC,gBAAgB,IAAI,iBAAiB,EACrC,SAAS,IAAI,eAAe,GAC/B,MAAM,UAAU,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var decode_1 = require("./decode");
4
+ var encode_1 = require("./encode");
5
+ /**
6
+ * Decodes a string with entities.
7
+ *
8
+ * @param data String to decode.
9
+ * @param level Optional level to decode at. 0 = XML, 1 = HTML. Default is 0.
10
+ */
11
+ function decode(data, level) {
12
+ return (!level || level <= 0 ? decode_1.decodeXML : decode_1.decodeHTML)(data);
13
+ }
14
+ exports.decode = decode;
15
+ /**
16
+ * Decodes a string with entities. Does not allow missing trailing semicolons for entities.
17
+ *
18
+ * @param data String to decode.
19
+ * @param level Optional level to decode at. 0 = XML, 1 = HTML. Default is 0.
20
+ */
21
+ function decodeStrict(data, level) {
22
+ return (!level || level <= 0 ? decode_1.decodeXML : decode_1.decodeHTMLStrict)(data);
23
+ }
24
+ exports.decodeStrict = decodeStrict;
25
+ /**
26
+ * Encodes a string with entities.
27
+ *
28
+ * @param data String to encode.
29
+ * @param level Optional level to encode at. 0 = XML, 1 = HTML. Default is 0.
30
+ */
31
+ function encode(data, level) {
32
+ return (!level || level <= 0 ? encode_1.encodeXML : encode_1.encodeHTML)(data);
33
+ }
34
+ exports.encode = encode;
35
+ var encode_2 = require("./encode");
36
+ exports.encodeXML = encode_2.encodeXML;
37
+ exports.encodeHTML = encode_2.encodeHTML;
38
+ exports.escape = encode_2.escape;
39
+ // Legacy aliases
40
+ exports.encodeHTML4 = encode_2.encodeHTML;
41
+ exports.encodeHTML5 = encode_2.encodeHTML;
42
+ var decode_2 = require("./decode");
43
+ exports.decodeXML = decode_2.decodeXML;
44
+ exports.decodeHTML = decode_2.decodeHTML;
45
+ exports.decodeHTMLStrict = decode_2.decodeHTMLStrict;
46
+ // Legacy aliases
47
+ exports.decodeHTML4 = decode_2.decodeHTML;
48
+ exports.decodeHTML5 = decode_2.decodeHTML;
49
+ exports.decodeHTML4Strict = decode_2.decodeHTMLStrict;
50
+ exports.decodeHTML5Strict = decode_2.decodeHTMLStrict;
51
+ exports.decodeXMLStrict = decode_2.decodeXML;
@@ -0,0 +1,30 @@
1
+ {
2
+ "0": 65533,
3
+ "128": 8364,
4
+ "130": 8218,
5
+ "131": 402,
6
+ "132": 8222,
7
+ "133": 8230,
8
+ "134": 8224,
9
+ "135": 8225,
10
+ "136": 710,
11
+ "137": 8240,
12
+ "138": 352,
13
+ "139": 8249,
14
+ "140": 338,
15
+ "142": 381,
16
+ "145": 8216,
17
+ "146": 8217,
18
+ "147": 8220,
19
+ "148": 8221,
20
+ "149": 8226,
21
+ "150": 8211,
22
+ "151": 8212,
23
+ "152": 732,
24
+ "153": 8482,
25
+ "154": 353,
26
+ "155": 8250,
27
+ "156": 339,
28
+ "158": 382,
29
+ "159": 376
30
+ }