123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- import htmlDecodeTree from "./generated/decode-data-html.js";
- import xmlDecodeTree from "./generated/decode-data-xml.js";
- import decodeCodePoint, { replaceCodePoint, fromCodePoint, } from "./decode_codepoint.js";
- export { htmlDecodeTree, xmlDecodeTree, decodeCodePoint };
- export { replaceCodePoint, fromCodePoint } from "./decode_codepoint.js";
- 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 = {}));
- const TO_LOWER_BIT = 0b100000;
- export 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 || (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 = {}));
- export var DecodingMode;
- (function (DecodingMode) {
-
- DecodingMode[DecodingMode["Legacy"] = 0] = "Legacy";
-
- DecodingMode[DecodingMode["Strict"] = 1] = "Strict";
-
- DecodingMode[DecodingMode["Attribute"] = 2] = "Attribute";
- })(DecodingMode || (DecodingMode = {}));
- export class EntityDecoder {
- constructor(
- /** 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;
- }
-
- startEntity(decodeMode) {
- this.decodeMode = decodeMode;
- this.state = EntityDecoderState.EntityStart;
- this.result = 0;
- this.treeIndex = 0;
- this.excess = 1;
- this.consumed = 1;
- }
-
- write(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);
- }
- }
- }
-
- stateNumericStart(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);
- }
- addToNumericResult(str, start, end, base) {
- if (start !== end) {
- const digitCount = end - start;
- this.result =
- this.result * Math.pow(base, digitCount) +
- parseInt(str.substr(start, digitCount), base);
- this.consumed += digitCount;
- }
- }
-
- stateNumericHex(str, offset) {
- const startIdx = offset;
- while (offset < str.length) {
- const 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;
- }
-
- stateNumericDecimal(str, offset) {
- const startIdx = offset;
- while (offset < str.length) {
- const 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;
- }
-
- emitNumericEntity(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(replaceCodePoint(this.result), this.consumed);
- if (this.errors) {
- if (lastCp !== CharCodes.SEMI) {
- this.errors.missingSemicolonAfterCharacterReference();
- }
- this.errors.validateNumericCharacterReference(this.result);
- }
- return this.consumed;
- }
-
- stateNamedEntity(str, offset) {
- const { decodeTree } = this;
- let current = decodeTree[this.treeIndex];
-
- let valueLength = (current & BinTrieFlags.VALUE_LENGTH) >> 14;
- for (; offset < str.length; offset++, this.excess++) {
- const 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;
- }
-
- emitNotTerminatedNamedEntity() {
- var _a;
- const { result, decodeTree } = this;
- const 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;
- }
-
- emitNamedEntityData(result, valueLength, consumed) {
- const { decodeTree } = this;
- this.emitCodePoint(valueLength === 1
- ? decodeTree[result] & ~BinTrieFlags.VALUE_LENGTH
- : decodeTree[result + 1], consumed);
- if (valueLength === 3) {
-
- this.emitCodePoint(decodeTree[result + 2], consumed);
- }
- return consumed;
- }
-
- end() {
- 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;
- }
- }
- }
- }
- function getDecoder(decodeTree) {
- let ret = "";
- const decoder = new EntityDecoder(decodeTree, (str) => (ret += fromCodePoint(str)));
- return function decodeWithTrie(str, decodeMode) {
- let lastIndex = 0;
- let offset = 0;
- while ((offset = str.indexOf("&", offset)) >= 0) {
- ret += str.slice(lastIndex, offset);
- decoder.startEntity(decodeMode);
- const len = decoder.write(str,
-
- offset + 1);
- if (len < 0) {
- lastIndex = offset + decoder.end();
- break;
- }
- lastIndex = offset + len;
-
- offset = len === 0 ? lastIndex + 1 : lastIndex;
- }
- const result = ret + str.slice(lastIndex);
-
- ret = "";
- return result;
- };
- }
- export function determineBranch(decodeTree, current, nodeIdx, char) {
- const branchCount = (current & BinTrieFlags.BRANCH_LENGTH) >> 7;
- const jumpOffset = current & BinTrieFlags.JUMP_TABLE;
-
- if (branchCount === 0) {
- return jumpOffset !== 0 && char === jumpOffset ? nodeIdx : -1;
- }
-
- if (jumpOffset) {
- const value = char - jumpOffset;
- return value < 0 || value >= branchCount
- ? -1
- : decodeTree[nodeIdx + value] - 1;
- }
-
-
- let lo = nodeIdx;
- let hi = lo + branchCount - 1;
- while (lo <= hi) {
- const mid = (lo + hi) >>> 1;
- const midVal = decodeTree[mid];
- if (midVal < char) {
- lo = mid + 1;
- }
- else if (midVal > char) {
- hi = mid - 1;
- }
- else {
- return decodeTree[mid + branchCount];
- }
- }
- return -1;
- }
- const htmlDecoder = getDecoder(htmlDecodeTree);
- const xmlDecoder = getDecoder(xmlDecodeTree);
- export function decodeHTML(str, mode = DecodingMode.Legacy) {
- return htmlDecoder(str, mode);
- }
- export function decodeHTMLAttribute(str) {
- return htmlDecoder(str, DecodingMode.Attribute);
- }
- export function decodeHTMLStrict(str) {
- return htmlDecoder(str, DecodingMode.Strict);
- }
- export function decodeXML(str) {
- return xmlDecoder(str, DecodingMode.Strict);
- }
|