|
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( |
|
|
|
decodeTree, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
emitCodePoint, |
|
|
|
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); |
|
} |
|
|