html-dom-parser
Version:
HTML to DOM parser.
358 lines (357 loc) • 15.2 kB
JavaScript
const require_decode_codepoint = require("./decode-codepoint.js");
const require_decode_data_html = require("./generated/decode-data-html.js");
const require_decode_data_xml = require("./generated/decode-data-xml.js");
const require_bin_trie_flags = require("./internal/bin-trie-flags.js");
//#region node_modules/entities/dist/decode.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 = {}));
/** Bit that needs to be set to convert an upper case ASCII character to lower case */
const TO_LOWER_BIT = 32;
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);
}
/**
* Checks if the given character is a valid end character for an entity in an attribute.
*
* Attribute values that aren't terminated properly aren't parsed, and shouldn't lead to a parser error.
* See the example in https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
* @param code Code point to decode.
*/
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 = {}));
/**
* Decoding mode for named entities.
*/
var DecodingMode;
(function(DecodingMode) {
/** Entities in text nodes that can end with any character. */
DecodingMode[DecodingMode["Legacy"] = 0] = "Legacy";
/** Only allow entities terminated with a semicolon. */
DecodingMode[DecodingMode["Strict"] = 1] = "Strict";
/** Entities in attributes have limitations on ending characters. */
DecodingMode[DecodingMode["Attribute"] = 2] = "Attribute";
})(DecodingMode || (DecodingMode = {}));
/**
* Token decoder with support of writing partial entities.
*/
var EntityDecoder = class {
decodeTree;
emitCodePoint;
errors;
constructor(decodeTree, emitCodePoint, errors) {
this.decodeTree = decodeTree;
this.emitCodePoint = emitCodePoint;
this.errors = errors;
}
/** The current state of the decoder. */
state = EntityDecoderState.EntityStart;
/** Characters that were consumed while parsing an entity. */
consumed = 1;
/**
* The result of the entity.
*
* Either the result index of a numeric entity, or the codepoint of a
* numeric entity.
*/
result = 0;
/** The current index in the decode tree. */
treeIndex = 0;
/** The number of characters that were consumed in excess. */
excess = 1;
/** The mode in which the decoder is operating. */
decodeMode = DecodingMode.Strict;
/** The number of characters that have been consumed in the current run. */
runConsumed = 0;
/**
* Resets the instance to make it reusable.
* @param decodeMode Entity decoding mode to use.
*/
startEntity(decodeMode) {
this.decodeMode = decodeMode;
this.state = EntityDecoderState.EntityStart;
this.result = 0;
this.treeIndex = 0;
this.excess = 1;
this.consumed = 1;
this.runConsumed = 0;
}
/**
* Write an entity to the decoder. This can be called multiple times with partial entities.
* If the entity is incomplete, the decoder will return -1.
*
* Mirrors the implementation of `getDecoder`, but with the ability to stop decoding if the
* entity is incomplete, and resume when the next string is written.
* @param input The string containing the entity (or a continuation of the entity).
* @param offset The offset at which the entity begins. Should be 0 if this is not the first call.
* @returns The number of characters that were consumed, or -1 if the entity is incomplete.
*/
write(input, offset) {
switch (this.state) {
case EntityDecoderState.EntityStart:
if (input.charCodeAt(offset) === CharCodes.NUM) {
this.state = EntityDecoderState.NumericStart;
this.consumed += 1;
return this.stateNumericStart(input, offset + 1);
}
this.state = EntityDecoderState.NamedEntity;
return this.stateNamedEntity(input, offset);
case EntityDecoderState.NumericStart: return this.stateNumericStart(input, offset);
case EntityDecoderState.NumericDecimal: return this.stateNumericDecimal(input, offset);
case EntityDecoderState.NumericHex: return this.stateNumericHex(input, offset);
case EntityDecoderState.NamedEntity: return this.stateNamedEntity(input, offset);
}
}
/**
* Switches between the numeric decimal and hexadecimal states.
*
* Equivalent to the `Numeric character reference state` in the HTML spec.
* @param input The string containing the entity (or a continuation of the entity).
* @param offset The current offset.
* @returns The number of characters that were consumed, or -1 if the entity is incomplete.
*/
stateNumericStart(input, offset) {
if (offset >= input.length) return -1;
if ((input.charCodeAt(offset) | TO_LOWER_BIT) === CharCodes.LOWER_X) {
this.state = EntityDecoderState.NumericHex;
this.consumed += 1;
return this.stateNumericHex(input, offset + 1);
}
this.state = EntityDecoderState.NumericDecimal;
return this.stateNumericDecimal(input, offset);
}
/**
* Parses a hexadecimal numeric entity.
*
* Equivalent to the `Hexademical character reference state` in the HTML spec.
* @param input The string containing the entity (or a continuation of the entity).
* @param offset The current offset.
* @returns The number of characters that were consumed, or -1 if the entity is incomplete.
*/
stateNumericHex(input, offset) {
while (offset < input.length) {
const char = input.charCodeAt(offset);
if (isNumber(char) || isHexadecimalCharacter(char)) {
const digit = char <= CharCodes.NINE ? char - CharCodes.ZERO : (char | TO_LOWER_BIT) - CharCodes.LOWER_A + 10;
this.result = this.result * 16 + digit;
this.consumed++;
offset++;
} else return this.emitNumericEntity(char, 3);
}
return -1;
}
/**
* Parses a decimal numeric entity.
*
* Equivalent to the `Decimal character reference state` in the HTML spec.
* @param input The string containing the entity (or a continuation of the entity).
* @param offset The current offset.
* @returns The number of characters that were consumed, or -1 if the entity is incomplete.
*/
stateNumericDecimal(input, offset) {
while (offset < input.length) {
const char = input.charCodeAt(offset);
if (isNumber(char)) {
this.result = this.result * 10 + (char - CharCodes.ZERO);
this.consumed++;
offset++;
} else return this.emitNumericEntity(char, 2);
}
return -1;
}
/**
* Validate and emit a numeric entity.
*
* Implements the logic from the `Hexademical character reference start
* state` and `Numeric character reference end state` in the HTML spec.
* @param lastCp The last code point of the entity. Used to see if the
* entity was terminated with a semicolon.
* @param expectedLength The minimum number of characters that should be
* consumed. Used to validate that at least one digit
* was consumed.
* @returns The number of characters that were consumed.
*/
emitNumericEntity(lastCp, expectedLength) {
if (this.consumed <= expectedLength) {
this.errors?.absenceOfDigitsInNumericCharacterReference(this.consumed);
return 0;
}
if (lastCp === CharCodes.SEMI) this.consumed += 1;
else if (this.decodeMode === DecodingMode.Strict) return 0;
this.emitCodePoint(require_decode_codepoint.replaceCodePoint(this.result), this.consumed);
if (this.errors) {
if (lastCp !== CharCodes.SEMI) this.errors.missingSemicolonAfterCharacterReference();
this.errors.validateNumericCharacterReference(this.result);
}
return this.consumed;
}
/**
* Parses a named entity.
*
* Equivalent to the `Named character reference state` in the HTML spec.
* @param input The string containing the entity (or a continuation of the entity).
* @param offset The current offset.
* @returns The number of characters that were consumed, or -1 if the entity is incomplete.
*/
stateNamedEntity(input, offset) {
const { decodeTree } = this;
let current = decodeTree[this.treeIndex];
let valueLength = (current & require_bin_trie_flags.BinTrieFlags.VALUE_LENGTH) >> 14;
while (offset < input.length) {
if (valueLength === 0 && (current & require_bin_trie_flags.BinTrieFlags.FLAG13) !== 0) {
const runLength = (current & require_bin_trie_flags.BinTrieFlags.BRANCH_LENGTH) >> 7;
if (this.runConsumed === 0) {
const firstChar = current & require_bin_trie_flags.BinTrieFlags.JUMP_TABLE;
if (input.charCodeAt(offset) !== firstChar) return this.result === 0 ? 0 : this.emitNotTerminatedNamedEntity();
offset++;
this.excess++;
this.runConsumed++;
}
while (this.runConsumed < runLength) {
if (offset >= input.length) return -1;
const charIndexInPacked = this.runConsumed - 1;
const packedWord = decodeTree[this.treeIndex + 1 + (charIndexInPacked >> 1)];
const expectedChar = charIndexInPacked % 2 === 0 ? packedWord & 255 : packedWord >> 8 & 255;
if (input.charCodeAt(offset) !== expectedChar) {
this.runConsumed = 0;
return this.result === 0 ? 0 : this.emitNotTerminatedNamedEntity();
}
offset++;
this.excess++;
this.runConsumed++;
}
this.runConsumed = 0;
this.treeIndex += 1 + (runLength >> 1);
current = decodeTree[this.treeIndex];
valueLength = (current & require_bin_trie_flags.BinTrieFlags.VALUE_LENGTH) >> 14;
}
if (offset >= input.length) break;
const char = input.charCodeAt(offset);
if (char === CharCodes.SEMI && valueLength !== 0 && (current & require_bin_trie_flags.BinTrieFlags.FLAG13) !== 0) return this.emitNamedEntityData(this.treeIndex, valueLength, this.consumed + this.excess);
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 & require_bin_trie_flags.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 && (current & require_bin_trie_flags.BinTrieFlags.FLAG13) === 0) {
this.result = this.treeIndex;
this.consumed += this.excess;
this.excess = 0;
}
}
offset++;
this.excess++;
}
return -1;
}
/**
* Emit a named entity that was not terminated with a semicolon.
* @returns The number of characters consumed.
*/
emitNotTerminatedNamedEntity() {
const { result, decodeTree } = this;
const valueLength = (decodeTree[result] & require_bin_trie_flags.BinTrieFlags.VALUE_LENGTH) >> 14;
this.emitNamedEntityData(result, valueLength, this.consumed);
this.errors?.missingSemicolonAfterCharacterReference();
return this.consumed;
}
/**
* Emit a named entity.
* @param result The index of the entity in the decode tree.
* @param valueLength The number of bytes in the entity.
* @param consumed The number of characters consumed.
* @returns The number of characters consumed.
*/
emitNamedEntityData(result, valueLength, consumed) {
const { decodeTree } = this;
this.emitCodePoint(valueLength === 1 ? decodeTree[result] & ~(require_bin_trie_flags.BinTrieFlags.VALUE_LENGTH | require_bin_trie_flags.BinTrieFlags.FLAG13) : decodeTree[result + 1], consumed);
if (valueLength === 3) this.emitCodePoint(decodeTree[result + 2], consumed);
return consumed;
}
/**
* Signal to the parser that the end of the input was reached.
*
* Remaining data will be emitted and relevant errors will be produced.
* @returns The number of characters consumed.
*/
end() {
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:
this.errors?.absenceOfDigitsInNumericCharacterReference(this.consumed);
return 0;
case EntityDecoderState.EntityStart: return 0;
}
}
};
/**
* Determines the branch of the current node that is taken given the current
* character. This function is used to traverse the trie.
* @param decodeTree The trie.
* @param current The current node.
* @param nodeIndex Index immediately after the current node header.
* @param char The current character.
* @returns The index of the next node, or -1 if no branch is taken.
*/
function determineBranch(decodeTree, current, nodeIndex, char) {
const branchCount = (current & require_bin_trie_flags.BinTrieFlags.BRANCH_LENGTH) >> 7;
const jumpOffset = current & require_bin_trie_flags.BinTrieFlags.JUMP_TABLE;
if (branchCount === 0) return jumpOffset !== 0 && char === jumpOffset ? nodeIndex : -1;
if (jumpOffset) {
const value = char - jumpOffset;
return value < 0 || value >= branchCount ? -1 : decodeTree[nodeIndex + value] - 1;
}
const packedKeySlots = branchCount + 1 >> 1;
let lo = 0;
let hi = branchCount - 1;
while (lo <= hi) {
const mid = lo + hi >>> 1;
const midKey = decodeTree[nodeIndex + (mid >> 1)] >> (mid & 1) * 8 & 255;
if (midKey < char) lo = mid + 1;
else if (midKey > char) hi = mid - 1;
else return decodeTree[nodeIndex + packedKeySlots + mid];
}
return -1;
}
//#endregion
Object.defineProperty(exports, "DecodingMode", {
enumerable: true,
get: function() {
return DecodingMode;
}
});
exports.EntityDecoder = EntityDecoder;
exports.determineBranch = determineBranch;
exports.htmlDecodeTree = require_decode_data_html.htmlDecodeTree;
exports.xmlDecodeTree = require_decode_data_xml.xmlDecodeTree;
//# sourceMappingURL=decode.js.map