| 1 | "use strict";
|
|---|
| 2 | var __assign = (this && this.__assign) || function () {
|
|---|
| 3 | __assign = Object.assign || function(t) {
|
|---|
| 4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|---|
| 5 | s = arguments[i];
|
|---|
| 6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|---|
| 7 | t[p] = s[p];
|
|---|
| 8 | }
|
|---|
| 9 | return t;
|
|---|
| 10 | };
|
|---|
| 11 | return __assign.apply(this, arguments);
|
|---|
| 12 | };
|
|---|
| 13 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 14 | exports.encode = encode;
|
|---|
| 15 | exports.decodeEntity = decodeEntity;
|
|---|
| 16 | exports.decode = decode;
|
|---|
| 17 | var named_references_js_1 = require("./named-references.js");
|
|---|
| 18 | var numeric_unicode_map_js_1 = require("./numeric-unicode-map.js");
|
|---|
| 19 | var surrogate_pairs_js_1 = require("./surrogate-pairs.js");
|
|---|
| 20 | var allNamedReferences = __assign(__assign({}, named_references_js_1.namedReferences), { all: named_references_js_1.namedReferences.html5 });
|
|---|
| 21 | var encodeRegExps = {
|
|---|
| 22 | specialChars: /[<>'"&]/g,
|
|---|
| 23 | nonAscii: /[<>'"&\u0080-\uD7FF\uE000-\uFFFF\uDC00-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g,
|
|---|
| 24 | nonAsciiPrintable: /[<>'"&\x01-\x08\x11-\x15\x17-\x1F\x7f-\uD7FF\uE000-\uFFFF\uDC00-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g,
|
|---|
| 25 | nonAsciiPrintableOnly: /[\x01-\x08\x11-\x15\x17-\x1F\x7f-\uD7FF\uE000-\uFFFF\uDC00-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g,
|
|---|
| 26 | extensive: /[\x01-\x0c\x0e-\x1f\x21-\x2c\x2e-\x2f\x3a-\x40\x5b-\x60\x7b-\x7d\x7f-\uD7FF\uE000-\uFFFF\uDC00-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g
|
|---|
| 27 | };
|
|---|
| 28 | var defaultEncodeOptions = {
|
|---|
| 29 | mode: 'specialChars',
|
|---|
| 30 | level: 'all',
|
|---|
| 31 | numeric: 'decimal'
|
|---|
| 32 | };
|
|---|
| 33 | /** Encodes all the necessary (specified by `level`) characters in the text */
|
|---|
| 34 | function encode(text, _a) {
|
|---|
| 35 | var _b = _a === void 0 ? defaultEncodeOptions : _a, _c = _b.mode, mode = _c === void 0 ? 'specialChars' : _c, _d = _b.numeric, numeric = _d === void 0 ? 'decimal' : _d, _e = _b.level, level = _e === void 0 ? 'all' : _e;
|
|---|
| 36 | if (!text) {
|
|---|
| 37 | return '';
|
|---|
| 38 | }
|
|---|
| 39 | var encodeRegExp = encodeRegExps[mode];
|
|---|
| 40 | var references = allNamedReferences[level].characters;
|
|---|
| 41 | var isHex = numeric === 'hexadecimal';
|
|---|
| 42 | return String.prototype.replace.call(text, encodeRegExp, function (input) {
|
|---|
| 43 | var result = references[input];
|
|---|
| 44 | if (!result) {
|
|---|
| 45 | var code = input.length > 1 ? (0, surrogate_pairs_js_1.getCodePoint)(input, 0) : input.charCodeAt(0);
|
|---|
| 46 | result = (isHex ? '&#x' + code.toString(16) : '&#' + code) + ';';
|
|---|
| 47 | }
|
|---|
| 48 | return result;
|
|---|
| 49 | });
|
|---|
| 50 | }
|
|---|
| 51 | var defaultDecodeOptions = {
|
|---|
| 52 | scope: 'body',
|
|---|
| 53 | level: 'all'
|
|---|
| 54 | };
|
|---|
| 55 | var strict = /&(?:#\d+|#[xX][\da-fA-F]+|[0-9a-zA-Z]+);/g;
|
|---|
| 56 | var attribute = /&(?:#\d+|#[xX][\da-fA-F]+|[0-9a-zA-Z]+)[;=]?/g;
|
|---|
| 57 | var baseDecodeRegExps = {
|
|---|
| 58 | xml: {
|
|---|
| 59 | strict: strict,
|
|---|
| 60 | attribute: attribute,
|
|---|
| 61 | body: named_references_js_1.bodyRegExps.xml
|
|---|
| 62 | },
|
|---|
| 63 | html4: {
|
|---|
| 64 | strict: strict,
|
|---|
| 65 | attribute: attribute,
|
|---|
| 66 | body: named_references_js_1.bodyRegExps.html4
|
|---|
| 67 | },
|
|---|
| 68 | html5: {
|
|---|
| 69 | strict: strict,
|
|---|
| 70 | attribute: attribute,
|
|---|
| 71 | body: named_references_js_1.bodyRegExps.html5
|
|---|
| 72 | }
|
|---|
| 73 | };
|
|---|
| 74 | var decodeRegExps = __assign(__assign({}, baseDecodeRegExps), { all: baseDecodeRegExps.html5 });
|
|---|
| 75 | var fromCharCode = String.fromCharCode;
|
|---|
| 76 | var outOfBoundsChar = fromCharCode(65533);
|
|---|
| 77 | var defaultDecodeEntityOptions = {
|
|---|
| 78 | level: 'all'
|
|---|
| 79 | };
|
|---|
| 80 | function getDecodedEntity(entity, references, isAttribute, isStrict) {
|
|---|
| 81 | var decodeResult = entity;
|
|---|
| 82 | var decodeEntityLastChar = entity[entity.length - 1];
|
|---|
| 83 | if (isAttribute && decodeEntityLastChar === '=') {
|
|---|
| 84 | decodeResult = entity;
|
|---|
| 85 | }
|
|---|
| 86 | else if (isStrict && decodeEntityLastChar !== ';') {
|
|---|
| 87 | decodeResult = entity;
|
|---|
| 88 | }
|
|---|
| 89 | else {
|
|---|
| 90 | var decodeResultByReference = references[entity];
|
|---|
| 91 | if (decodeResultByReference) {
|
|---|
| 92 | decodeResult = decodeResultByReference;
|
|---|
| 93 | }
|
|---|
| 94 | else if (entity[0] === '&' && entity[1] === '#') {
|
|---|
| 95 | var decodeSecondChar = entity[2];
|
|---|
| 96 | var decodeCode = decodeSecondChar == 'x' || decodeSecondChar == 'X'
|
|---|
| 97 | ? parseInt(entity.substr(3), 16)
|
|---|
| 98 | : parseInt(entity.substr(2));
|
|---|
| 99 | decodeResult =
|
|---|
| 100 | decodeCode >= 0x10ffff
|
|---|
| 101 | ? outOfBoundsChar
|
|---|
| 102 | : decodeCode > 65535
|
|---|
| 103 | ? (0, surrogate_pairs_js_1.fromCodePoint)(decodeCode)
|
|---|
| 104 | : fromCharCode(numeric_unicode_map_js_1.numericUnicodeMap[decodeCode] || decodeCode);
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | return decodeResult;
|
|---|
| 108 | }
|
|---|
| 109 | /** Decodes a single entity */
|
|---|
| 110 | function decodeEntity(entity, _a) {
|
|---|
| 111 | var _b = _a === void 0 ? defaultDecodeEntityOptions : _a, _c = _b.level, level = _c === void 0 ? 'all' : _c;
|
|---|
| 112 | if (!entity) {
|
|---|
| 113 | return '';
|
|---|
| 114 | }
|
|---|
| 115 | return getDecodedEntity(entity, allNamedReferences[level].entities, false, false);
|
|---|
| 116 | }
|
|---|
| 117 | /** Decodes all entities in the text */
|
|---|
| 118 | function decode(text, _a) {
|
|---|
| 119 | var _b = _a === void 0 ? defaultDecodeOptions : _a, _c = _b.level, level = _c === void 0 ? 'all' : _c, _d = _b.scope, scope = _d === void 0 ? level === 'xml' ? 'strict' : 'body' : _d;
|
|---|
| 120 | if (!text) {
|
|---|
| 121 | return '';
|
|---|
| 122 | }
|
|---|
| 123 | var decodeRegExp = decodeRegExps[level][scope];
|
|---|
| 124 | var references = allNamedReferences[level].entities;
|
|---|
| 125 | var isAttribute = scope === 'attribute';
|
|---|
| 126 | var isStrict = scope === 'strict';
|
|---|
| 127 | return text.replace(decodeRegExp, function (entity) { return getDecodedEntity(entity, references, isAttribute, isStrict); });
|
|---|
| 128 | }
|
|---|
| 129 | //# sourceMappingURL=index.js.map |
|---|