123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- "use strict";
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- var desc = Object.getOwnPropertyDescriptor(m, k);
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
- desc = { enumerable: true, get: function() { return m[k]; } };
- }
- Object.defineProperty(o, k2, desc);
- }) : (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- o[k2] = m[k];
- }));
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
- Object.defineProperty(o, "default", { enumerable: true, value: v });
- }) : function(o, v) {
- o["default"] = v;
- });
- var __importStar = (this && this.__importStar) || function (mod) {
- if (mod && mod.__esModule) return mod;
- var result = {};
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
- __setModuleDefault(result, mod);
- return result;
- };
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.decodeXML = exports.decodeHTMLStrict = exports.decodeHTMLAttribute = exports.decodeHTML = exports.determineBranch = exports.EntityDecoder = exports.DecodingMode = exports.BinTrieFlags = exports.fromCodePoint = exports.replaceCodePoint = exports.decodeCodePoint = exports.xmlDecodeTree = exports.htmlDecodeTree = void 0;
- var decode_data_html_js_1 = __importDefault(require("./generated/decode-data-html.js"));
- exports.htmlDecodeTree = decode_data_html_js_1.default;
- var decode_data_xml_js_1 = __importDefault(require("./generated/decode-data-xml.js"));
- exports.xmlDecodeTree = decode_data_xml_js_1.default;
- var decode_codepoint_js_1 = __importStar(require("./decode_codepoint.js"));
- exports.decodeCodePoint = decode_codepoint_js_1.default;
- var decode_codepoint_js_2 = require("./decode_codepoint.js");
- Object.defineProperty(exports, "replaceCodePoint", { enumerable: true, get: function () { return decode_codepoint_js_2.replaceCodePoint; } });
- Object.defineProperty(exports, "fromCodePoint", { enumerable: true, get: function () { return decode_codepoint_js_2.fromCodePoint; } });
- var CharCodes;
- (function (CharCodes) {
- CharCodes[CharCodes["NUM"] = 35] = "NUM";
- CharCodes[CharCodes["SEMI"] = 59] = "SEMI";
- CharCodes[CharCodes["EQUALS"] = 61] = "EQUALS";
- CharCodes[CharCodes["ZERO"] = 48] = "ZERO";
- CharCodes[CharCodes["NINE"] = 57] = "NINE";
- CharCodes[CharCodes["LOWER_A"] = 97] = "LOWER_A";
- CharCodes[CharCodes["LOWER_F"] = 102] = "LOWER_F";
- CharCodes[CharCodes["LOWER_X"] = 120] = "LOWER_X";
- CharCodes[CharCodes["LOWER_Z"] = 122] = "LOWER_Z";
- CharCodes[CharCodes["UPPER_A"] = 65] = "UPPER_A";
- CharCodes[CharCodes["UPPER_F"] = 70] = "UPPER_F";
- CharCodes[CharCodes["UPPER_Z"] = 90] = "UPPER_Z";
- })(CharCodes || (CharCodes = {}));
- var TO_LOWER_BIT = 32;
- var BinTrieFlags;
- (function (BinTrieFlags) {
- BinTrieFlags[BinTrieFlags["VALUE_LENGTH"] = 49152] = "VALUE_LENGTH";
- BinTrieFlags[BinTrieFlags["BRANCH_LENGTH"] = 16256] = "BRANCH_LENGTH";
- BinTrieFlags[BinTrieFlags["JUMP_TABLE"] = 127] = "JUMP_TABLE";
- })(BinTrieFlags = exports.BinTrieFlags || (exports.BinTrieFlags = {}));
- function isNumber(code) {
- return code >= CharCodes.ZERO && code <= CharCodes.NINE;
- }
- function isHexadecimalCharacter(code) {
- return ((code >= CharCodes.UPPER_A && code <= CharCodes.UPPER_F) ||
- (code >= CharCodes.LOWER_A && code <= CharCodes.LOWER_F));
- }
- function isAsciiAlphaNumeric(code) {
- return ((code >= CharCodes.UPPER_A && code <= CharCodes.UPPER_Z) ||
- (code >= CharCodes.LOWER_A && code <= CharCodes.LOWER_Z) ||
- isNumber(code));
- }
- function isEntityInAttributeInvalidEnd(code) {
- return code === CharCodes.EQUALS || isAsciiAlphaNumeric(code);
- }
- var EntityDecoderState;
- (function (EntityDecoderState) {
- EntityDecoderState[EntityDecoderState["EntityStart"] = 0] = "EntityStart";
- EntityDecoderState[EntityDecoderState["NumericStart"] = 1] = "NumericStart";
- EntityDecoderState[EntityDecoderState["NumericDecimal"] = 2] = "NumericDecimal";
- EntityDecoderState[EntityDecoderState["NumericHex"] = 3] = "NumericHex";
- EntityDecoderState[EntityDecoderState["NamedEntity"] = 4] = "NamedEntity";
- })(EntityDecoderState || (EntityDecoderState = {}));
- var DecodingMode;
- (function (DecodingMode) {
-
- DecodingMode[DecodingMode["Legacy"] = 0] = "Legacy";
-
- DecodingMode[DecodingMode["Strict"] = 1] = "Strict";
-
- DecodingMode[DecodingMode["Attribute"] = 2] = "Attribute";
- })(DecodingMode = exports.DecodingMode || (exports.DecodingMode = {}));
- var EntityDecoder = (function () {
- function EntityDecoder(
- /** The tree used to decode entities. */
- decodeTree,
- /**
- * The function that is called when a codepoint is decoded.
- *
- * For multi-byte named entities, this will be called multiple times,
- * with the second codepoint, and the same `consumed` value.
- *
- * @param codepoint The decoded codepoint.
- * @param consumed The number of bytes consumed by the decoder.
- */
- emitCodePoint,
- /** An object that is used to produce errors. */
- errors) {
- this.decodeTree = decodeTree;
- this.emitCodePoint = emitCodePoint;
- this.errors = errors;
-
- this.state = EntityDecoderState.EntityStart;
-
- this.consumed = 1;
-
- this.result = 0;
-
- this.treeIndex = 0;
-
- this.excess = 1;
-
- this.decodeMode = DecodingMode.Strict;
- }
-
- EntityDecoder.prototype.startEntity = function (decodeMode) {
- this.decodeMode = decodeMode;
- this.state = EntityDecoderState.EntityStart;
- this.result = 0;
- this.treeIndex = 0;
- this.excess = 1;
- this.consumed = 1;
- };
-
- EntityDecoder.prototype.write = function (str, offset) {
- switch (this.state) {
- case EntityDecoderState.EntityStart: {
- if (str.charCodeAt(offset) === CharCodes.NUM) {
- this.state = EntityDecoderState.NumericStart;
- this.consumed += 1;
- return this.stateNumericStart(str, offset + 1);
- }
- this.state = EntityDecoderState.NamedEntity;
- return this.stateNamedEntity(str, offset);
- }
- case EntityDecoderState.NumericStart: {
- return this.stateNumericStart(str, offset);
- }
- case EntityDecoderState.NumericDecimal: {
- return this.stateNumericDecimal(str, offset);
- }
- case EntityDecoderState.NumericHex: {
- return this.stateNumericHex(str, offset);
- }
- case EntityDecoderState.NamedEntity: {
- return this.stateNamedEntity(str, offset);
- }
- }
- };
-
- EntityDecoder.prototype.stateNumericStart = function (str, offset) {
- if (offset >= str.length) {
- return -1;
- }
- if ((str.charCodeAt(offset) | TO_LOWER_BIT) === CharCodes.LOWER_X) {
- this.state = EntityDecoderState.NumericHex;
- this.consumed += 1;
- return this.stateNumericHex(str, offset + 1);
- }
- this.state = EntityDecoderState.NumericDecimal;
- return this.stateNumericDecimal(str, offset);
- };
- EntityDecoder.prototype.addToNumericResult = function (str, start, end, base) {
- if (start !== end) {
- var digitCount = end - start;
- this.result =
- this.result * Math.pow(base, digitCount) +
- parseInt(str.substr(start, digitCount), base);
- this.consumed += digitCount;
- }
- };
-
- EntityDecoder.prototype.stateNumericHex = function (str, offset) {
- var startIdx = offset;
- while (offset < str.length) {
- var char = str.charCodeAt(offset);
- if (isNumber(char) || isHexadecimalCharacter(char)) {
- offset += 1;
- }
- else {
- this.addToNumericResult(str, startIdx, offset, 16);
- return this.emitNumericEntity(char, 3);
- }
- }
- this.addToNumericResult(str, startIdx, offset, 16);
- return -1;
- };
-
- EntityDecoder.prototype.stateNumericDecimal = function (str, offset) {
- var startIdx = offset;
- while (offset < str.length) {
- var char = str.charCodeAt(offset);
- if (isNumber(char)) {
- offset += 1;
- }
- else {
- this.addToNumericResult(str, startIdx, offset, 10);
- return this.emitNumericEntity(char, 2);
- }
- }
- this.addToNumericResult(str, startIdx, offset, 10);
- return -1;
- };
-
- EntityDecoder.prototype.emitNumericEntity = function (lastCp, expectedLength) {
- var _a;
-
- if (this.consumed <= expectedLength) {
- (_a = this.errors) === null || _a === void 0 ? void 0 : _a.absenceOfDigitsInNumericCharacterReference(this.consumed);
- return 0;
- }
-
- if (lastCp === CharCodes.SEMI) {
- this.consumed += 1;
- }
- else if (this.decodeMode === DecodingMode.Strict) {
- return 0;
- }
- this.emitCodePoint((0, decode_codepoint_js_1.replaceCodePoint)(this.result), this.consumed);
- if (this.errors) {
- if (lastCp !== CharCodes.SEMI) {
- this.errors.missingSemicolonAfterCharacterReference();
- }
- this.errors.validateNumericCharacterReference(this.result);
- }
- return this.consumed;
- };
-
- EntityDecoder.prototype.stateNamedEntity = function (str, offset) {
- var decodeTree = this.decodeTree;
- var current = decodeTree[this.treeIndex];
-
- var valueLength = (current & BinTrieFlags.VALUE_LENGTH) >> 14;
- for (; offset < str.length; offset++, this.excess++) {
- var char = str.charCodeAt(offset);
- this.treeIndex = determineBranch(decodeTree, current, this.treeIndex + Math.max(1, valueLength), char);
- if (this.treeIndex < 0) {
- return this.result === 0 ||
-
- (this.decodeMode === DecodingMode.Attribute &&
-
- (valueLength === 0 ||
-
- isEntityInAttributeInvalidEnd(char)))
- ? 0
- : this.emitNotTerminatedNamedEntity();
- }
- current = decodeTree[this.treeIndex];
- valueLength = (current & BinTrieFlags.VALUE_LENGTH) >> 14;
-
- if (valueLength !== 0) {
-
- if (char === CharCodes.SEMI) {
- return this.emitNamedEntityData(this.treeIndex, valueLength, this.consumed + this.excess);
- }
-
- if (this.decodeMode !== DecodingMode.Strict) {
- this.result = this.treeIndex;
- this.consumed += this.excess;
- this.excess = 0;
- }
- }
- }
- return -1;
- };
-
- EntityDecoder.prototype.emitNotTerminatedNamedEntity = function () {
- var _a;
- var _b = this, result = _b.result, decodeTree = _b.decodeTree;
- var valueLength = (decodeTree[result] & BinTrieFlags.VALUE_LENGTH) >> 14;
- this.emitNamedEntityData(result, valueLength, this.consumed);
- (_a = this.errors) === null || _a === void 0 ? void 0 : _a.missingSemicolonAfterCharacterReference();
- return this.consumed;
- };
-
- EntityDecoder.prototype.emitNamedEntityData = function (result, valueLength, consumed) {
- var decodeTree = this.decodeTree;
- this.emitCodePoint(valueLength === 1
- ? decodeTree[result] & ~BinTrieFlags.VALUE_LENGTH
- : decodeTree[result + 1], consumed);
- if (valueLength === 3) {
-
- this.emitCodePoint(decodeTree[result + 2], consumed);
- }
- return consumed;
- };
-
- EntityDecoder.prototype.end = function () {
- var _a;
- switch (this.state) {
- case EntityDecoderState.NamedEntity: {
-
- return this.result !== 0 &&
- (this.decodeMode !== DecodingMode.Attribute ||
- this.result === this.treeIndex)
- ? this.emitNotTerminatedNamedEntity()
- : 0;
- }
-
- case EntityDecoderState.NumericDecimal: {
- return this.emitNumericEntity(0, 2);
- }
- case EntityDecoderState.NumericHex: {
- return this.emitNumericEntity(0, 3);
- }
- case EntityDecoderState.NumericStart: {
- (_a = this.errors) === null || _a === void 0 ? void 0 : _a.absenceOfDigitsInNumericCharacterReference(this.consumed);
- return 0;
- }
- case EntityDecoderState.EntityStart: {
-
- return 0;
- }
- }
- };
- return EntityDecoder;
- }());
- exports.EntityDecoder = EntityDecoder;
- function getDecoder(decodeTree) {
- var ret = "";
- var decoder = new EntityDecoder(decodeTree, function (str) { return (ret += (0, decode_codepoint_js_1.fromCodePoint)(str)); });
- return function decodeWithTrie(str, decodeMode) {
- var lastIndex = 0;
- var offset = 0;
- while ((offset = str.indexOf("&", offset)) >= 0) {
- ret += str.slice(lastIndex, offset);
- decoder.startEntity(decodeMode);
- var len = decoder.write(str,
-
- offset + 1);
- if (len < 0) {
- lastIndex = offset + decoder.end();
- break;
- }
- lastIndex = offset + len;
-
- offset = len === 0 ? lastIndex + 1 : lastIndex;
- }
- var result = ret + str.slice(lastIndex);
-
- ret = "";
- return result;
- };
- }
- function determineBranch(decodeTree, current, nodeIdx, char) {
- var branchCount = (current & BinTrieFlags.BRANCH_LENGTH) >> 7;
- var jumpOffset = current & BinTrieFlags.JUMP_TABLE;
-
- if (branchCount === 0) {
- return jumpOffset !== 0 && char === jumpOffset ? nodeIdx : -1;
- }
-
- if (jumpOffset) {
- var value = char - jumpOffset;
- return value < 0 || value >= branchCount
- ? -1
- : decodeTree[nodeIdx + value] - 1;
- }
-
-
- var lo = nodeIdx;
- var hi = lo + branchCount - 1;
- while (lo <= hi) {
- var mid = (lo + hi) >>> 1;
- var midVal = decodeTree[mid];
- if (midVal < char) {
- lo = mid + 1;
- }
- else if (midVal > char) {
- hi = mid - 1;
- }
- else {
- return decodeTree[mid + branchCount];
- }
- }
- return -1;
- }
- exports.determineBranch = determineBranch;
- var htmlDecoder = getDecoder(decode_data_html_js_1.default);
- var xmlDecoder = getDecoder(decode_data_xml_js_1.default);
- function decodeHTML(str, mode) {
- if (mode === void 0) { mode = DecodingMode.Legacy; }
- return htmlDecoder(str, mode);
- }
- exports.decodeHTML = decodeHTML;
- function decodeHTMLAttribute(str) {
- return htmlDecoder(str, DecodingMode.Attribute);
- }
- exports.decodeHTMLAttribute = decodeHTMLAttribute;
- function decodeHTMLStrict(str) {
- return htmlDecoder(str, DecodingMode.Strict);
- }
- exports.decodeHTMLStrict = decodeHTMLStrict;
- function decodeXML(str) {
- return xmlDecoder(str, DecodingMode.Strict);
- }
- exports.decodeXML = decodeXML;
|