910 lines
35 KiB
JavaScript
910 lines
35 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var decode_codepoint_1 = __importDefault(require("entities/lib/decode_codepoint"));
|
|
var entities_json_1 = __importDefault(require("entities/lib/maps/entities.json"));
|
|
var legacy_json_1 = __importDefault(require("entities/lib/maps/legacy.json"));
|
|
var xml_json_1 = __importDefault(require("entities/lib/maps/xml.json"));
|
|
function whitespace(c) {
|
|
return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
|
|
}
|
|
function isASCIIAlpha(c) {
|
|
return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z");
|
|
}
|
|
function ifElseState(upper, SUCCESS, FAILURE) {
|
|
var lower = upper.toLowerCase();
|
|
if (upper === lower) {
|
|
return function (t, c) {
|
|
if (c === lower) {
|
|
t._state = SUCCESS;
|
|
}
|
|
else {
|
|
t._state = FAILURE;
|
|
t._index--;
|
|
}
|
|
};
|
|
}
|
|
return function (t, c) {
|
|
if (c === lower || c === upper) {
|
|
t._state = SUCCESS;
|
|
}
|
|
else {
|
|
t._state = FAILURE;
|
|
t._index--;
|
|
}
|
|
};
|
|
}
|
|
function consumeSpecialNameChar(upper, NEXT_STATE) {
|
|
var lower = upper.toLowerCase();
|
|
return function (t, c) {
|
|
if (c === lower || c === upper) {
|
|
t._state = NEXT_STATE;
|
|
}
|
|
else {
|
|
t._state = 3 /* InTagName */;
|
|
t._index--; // Consume the token again
|
|
}
|
|
};
|
|
}
|
|
var stateBeforeCdata1 = ifElseState("C", 24 /* BeforeCdata2 */, 16 /* InDeclaration */);
|
|
var stateBeforeCdata2 = ifElseState("D", 25 /* BeforeCdata3 */, 16 /* InDeclaration */);
|
|
var stateBeforeCdata3 = ifElseState("A", 26 /* BeforeCdata4 */, 16 /* InDeclaration */);
|
|
var stateBeforeCdata4 = ifElseState("T", 27 /* BeforeCdata5 */, 16 /* InDeclaration */);
|
|
var stateBeforeCdata5 = ifElseState("A", 28 /* BeforeCdata6 */, 16 /* InDeclaration */);
|
|
var stateBeforeScript1 = consumeSpecialNameChar("R", 35 /* BeforeScript2 */);
|
|
var stateBeforeScript2 = consumeSpecialNameChar("I", 36 /* BeforeScript3 */);
|
|
var stateBeforeScript3 = consumeSpecialNameChar("P", 37 /* BeforeScript4 */);
|
|
var stateBeforeScript4 = consumeSpecialNameChar("T", 38 /* BeforeScript5 */);
|
|
var stateAfterScript1 = ifElseState("R", 40 /* AfterScript2 */, 1 /* Text */);
|
|
var stateAfterScript2 = ifElseState("I", 41 /* AfterScript3 */, 1 /* Text */);
|
|
var stateAfterScript3 = ifElseState("P", 42 /* AfterScript4 */, 1 /* Text */);
|
|
var stateAfterScript4 = ifElseState("T", 43 /* AfterScript5 */, 1 /* Text */);
|
|
var stateBeforeStyle1 = consumeSpecialNameChar("Y", 45 /* BeforeStyle2 */);
|
|
var stateBeforeStyle2 = consumeSpecialNameChar("L", 46 /* BeforeStyle3 */);
|
|
var stateBeforeStyle3 = consumeSpecialNameChar("E", 47 /* BeforeStyle4 */);
|
|
var stateAfterStyle1 = ifElseState("Y", 49 /* AfterStyle2 */, 1 /* Text */);
|
|
var stateAfterStyle2 = ifElseState("L", 50 /* AfterStyle3 */, 1 /* Text */);
|
|
var stateAfterStyle3 = ifElseState("E", 51 /* AfterStyle4 */, 1 /* Text */);
|
|
var stateBeforeSpecialT = consumeSpecialNameChar("I", 54 /* BeforeTitle1 */);
|
|
var stateBeforeTitle1 = consumeSpecialNameChar("T", 55 /* BeforeTitle2 */);
|
|
var stateBeforeTitle2 = consumeSpecialNameChar("L", 56 /* BeforeTitle3 */);
|
|
var stateBeforeTitle3 = consumeSpecialNameChar("E", 57 /* BeforeTitle4 */);
|
|
var stateAfterSpecialTEnd = ifElseState("I", 58 /* AfterTitle1 */, 1 /* Text */);
|
|
var stateAfterTitle1 = ifElseState("T", 59 /* AfterTitle2 */, 1 /* Text */);
|
|
var stateAfterTitle2 = ifElseState("L", 60 /* AfterTitle3 */, 1 /* Text */);
|
|
var stateAfterTitle3 = ifElseState("E", 61 /* AfterTitle4 */, 1 /* Text */);
|
|
var stateBeforeEntity = ifElseState("#", 63 /* BeforeNumericEntity */, 64 /* InNamedEntity */);
|
|
var stateBeforeNumericEntity = ifElseState("X", 66 /* InHexEntity */, 65 /* InNumericEntity */);
|
|
var Tokenizer = /** @class */ (function () {
|
|
function Tokenizer(options, cbs) {
|
|
var _a;
|
|
/** The current state the tokenizer is in. */
|
|
this._state = 1 /* Text */;
|
|
/** The read buffer. */
|
|
this.buffer = "";
|
|
/** The beginning of the section that is currently being read. */
|
|
this.sectionStart = 0;
|
|
/** The index within the buffer that we are currently looking at. */
|
|
this._index = 0;
|
|
/**
|
|
* Data that has already been processed will be removed from the buffer occasionally.
|
|
* `_bufferOffset` keeps track of how many characters have been removed, to make sure position information is accurate.
|
|
*/
|
|
this.bufferOffset = 0;
|
|
/** Some behavior, eg. when decoding entities, is done while we are in another state. This keeps track of the other state type. */
|
|
this.baseState = 1 /* Text */;
|
|
/** For special parsing behavior inside of script and style tags. */
|
|
this.special = 1 /* None */;
|
|
/** Indicates whether the tokenizer has been paused. */
|
|
this.running = true;
|
|
/** Indicates whether the tokenizer has finished running / `.end` has been called. */
|
|
this.ended = false;
|
|
this.cbs = cbs;
|
|
this.xmlMode = !!(options === null || options === void 0 ? void 0 : options.xmlMode);
|
|
this.decodeEntities = (_a = options === null || options === void 0 ? void 0 : options.decodeEntities) !== null && _a !== void 0 ? _a : true;
|
|
}
|
|
Tokenizer.prototype.reset = function () {
|
|
this._state = 1 /* Text */;
|
|
this.buffer = "";
|
|
this.sectionStart = 0;
|
|
this._index = 0;
|
|
this.bufferOffset = 0;
|
|
this.baseState = 1 /* Text */;
|
|
this.special = 1 /* None */;
|
|
this.running = true;
|
|
this.ended = false;
|
|
};
|
|
Tokenizer.prototype.write = function (chunk) {
|
|
if (this.ended)
|
|
this.cbs.onerror(Error(".write() after done!"));
|
|
this.buffer += chunk;
|
|
this.parse();
|
|
};
|
|
Tokenizer.prototype.end = function (chunk) {
|
|
if (this.ended)
|
|
this.cbs.onerror(Error(".end() after done!"));
|
|
if (chunk)
|
|
this.write(chunk);
|
|
this.ended = true;
|
|
if (this.running)
|
|
this.finish();
|
|
};
|
|
Tokenizer.prototype.pause = function () {
|
|
this.running = false;
|
|
};
|
|
Tokenizer.prototype.resume = function () {
|
|
this.running = true;
|
|
if (this._index < this.buffer.length) {
|
|
this.parse();
|
|
}
|
|
if (this.ended) {
|
|
this.finish();
|
|
}
|
|
};
|
|
/**
|
|
* The current index within all of the written data.
|
|
*/
|
|
Tokenizer.prototype.getAbsoluteIndex = function () {
|
|
return this.bufferOffset + this._index;
|
|
};
|
|
Tokenizer.prototype.stateText = function (c) {
|
|
if (c === "<") {
|
|
if (this._index > this.sectionStart) {
|
|
this.cbs.ontext(this.getSection());
|
|
}
|
|
this._state = 2 /* BeforeTagName */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
else if (this.decodeEntities &&
|
|
c === "&" &&
|
|
(this.special === 1 /* None */ || this.special === 4 /* Title */)) {
|
|
if (this._index > this.sectionStart) {
|
|
this.cbs.ontext(this.getSection());
|
|
}
|
|
this.baseState = 1 /* Text */;
|
|
this._state = 62 /* BeforeEntity */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
};
|
|
/**
|
|
* HTML only allows ASCII alpha characters (a-z and A-Z) at the beginning of a tag name.
|
|
*
|
|
* XML allows a lot more characters here (@see https://www.w3.org/TR/REC-xml/#NT-NameStartChar).
|
|
* We allow anything that wouldn't end the tag.
|
|
*/
|
|
Tokenizer.prototype.isTagStartChar = function (c) {
|
|
return (isASCIIAlpha(c) ||
|
|
(this.xmlMode && !whitespace(c) && c !== "/" && c !== ">"));
|
|
};
|
|
Tokenizer.prototype.stateBeforeTagName = function (c) {
|
|
if (c === "/") {
|
|
this._state = 5 /* BeforeClosingTagName */;
|
|
}
|
|
else if (c === "<") {
|
|
this.cbs.ontext(this.getSection());
|
|
this.sectionStart = this._index;
|
|
}
|
|
else if (c === ">" ||
|
|
this.special !== 1 /* None */ ||
|
|
whitespace(c)) {
|
|
this._state = 1 /* Text */;
|
|
}
|
|
else if (c === "!") {
|
|
this._state = 15 /* BeforeDeclaration */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else if (c === "?") {
|
|
this._state = 17 /* InProcessingInstruction */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else if (!this.isTagStartChar(c)) {
|
|
this._state = 1 /* Text */;
|
|
}
|
|
else {
|
|
this._state =
|
|
!this.xmlMode && (c === "s" || c === "S")
|
|
? 32 /* BeforeSpecialS */
|
|
: !this.xmlMode && (c === "t" || c === "T")
|
|
? 52 /* BeforeSpecialT */
|
|
: 3 /* InTagName */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInTagName = function (c) {
|
|
if (c === "/" || c === ">" || whitespace(c)) {
|
|
this.emitToken("onopentagname");
|
|
this._state = 8 /* BeforeAttributeName */;
|
|
this._index--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateBeforeClosingTagName = function (c) {
|
|
if (whitespace(c)) {
|
|
// Ignore
|
|
}
|
|
else if (c === ">") {
|
|
this._state = 1 /* Text */;
|
|
}
|
|
else if (this.special !== 1 /* None */) {
|
|
if (this.special !== 4 /* Title */ && (c === "s" || c === "S")) {
|
|
this._state = 33 /* BeforeSpecialSEnd */;
|
|
}
|
|
else if (this.special === 4 /* Title */ &&
|
|
(c === "t" || c === "T")) {
|
|
this._state = 53 /* BeforeSpecialTEnd */;
|
|
}
|
|
else {
|
|
this._state = 1 /* Text */;
|
|
this._index--;
|
|
}
|
|
}
|
|
else if (!this.isTagStartChar(c)) {
|
|
this._state = 20 /* InSpecialComment */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
else {
|
|
this._state = 6 /* InClosingTagName */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInClosingTagName = function (c) {
|
|
if (c === ">" || whitespace(c)) {
|
|
this.emitToken("onclosetag");
|
|
this._state = 7 /* AfterClosingTagName */;
|
|
this._index--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateAfterClosingTagName = function (c) {
|
|
// Skip everything until ">"
|
|
if (c === ">") {
|
|
this._state = 1 /* Text */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateBeforeAttributeName = function (c) {
|
|
if (c === ">") {
|
|
this.cbs.onopentagend();
|
|
this._state = 1 /* Text */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else if (c === "/") {
|
|
this._state = 4 /* InSelfClosingTag */;
|
|
}
|
|
else if (!whitespace(c)) {
|
|
this._state = 9 /* InAttributeName */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInSelfClosingTag = function (c) {
|
|
if (c === ">") {
|
|
this.cbs.onselfclosingtag();
|
|
this._state = 1 /* Text */;
|
|
this.sectionStart = this._index + 1;
|
|
this.special = 1 /* None */; // Reset special state, in case of self-closing special tags
|
|
}
|
|
else if (!whitespace(c)) {
|
|
this._state = 8 /* BeforeAttributeName */;
|
|
this._index--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInAttributeName = function (c) {
|
|
if (c === "=" || c === "/" || c === ">" || whitespace(c)) {
|
|
this.cbs.onattribname(this.getSection());
|
|
this.sectionStart = -1;
|
|
this._state = 10 /* AfterAttributeName */;
|
|
this._index--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateAfterAttributeName = function (c) {
|
|
if (c === "=") {
|
|
this._state = 11 /* BeforeAttributeValue */;
|
|
}
|
|
else if (c === "/" || c === ">") {
|
|
this.cbs.onattribend(undefined);
|
|
this._state = 8 /* BeforeAttributeName */;
|
|
this._index--;
|
|
}
|
|
else if (!whitespace(c)) {
|
|
this.cbs.onattribend(undefined);
|
|
this._state = 9 /* InAttributeName */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateBeforeAttributeValue = function (c) {
|
|
if (c === '"') {
|
|
this._state = 12 /* InAttributeValueDq */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else if (c === "'") {
|
|
this._state = 13 /* InAttributeValueSq */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else if (!whitespace(c)) {
|
|
this._state = 14 /* InAttributeValueNq */;
|
|
this.sectionStart = this._index;
|
|
this._index--; // Reconsume token
|
|
}
|
|
};
|
|
Tokenizer.prototype.handleInAttributeValue = function (c, quote) {
|
|
if (c === quote) {
|
|
this.emitToken("onattribdata");
|
|
this.cbs.onattribend(quote);
|
|
this._state = 8 /* BeforeAttributeName */;
|
|
}
|
|
else if (this.decodeEntities && c === "&") {
|
|
this.emitToken("onattribdata");
|
|
this.baseState = this._state;
|
|
this._state = 62 /* BeforeEntity */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInAttributeValueDoubleQuotes = function (c) {
|
|
this.handleInAttributeValue(c, '"');
|
|
};
|
|
Tokenizer.prototype.stateInAttributeValueSingleQuotes = function (c) {
|
|
this.handleInAttributeValue(c, "'");
|
|
};
|
|
Tokenizer.prototype.stateInAttributeValueNoQuotes = function (c) {
|
|
if (whitespace(c) || c === ">") {
|
|
this.emitToken("onattribdata");
|
|
this.cbs.onattribend(null);
|
|
this._state = 8 /* BeforeAttributeName */;
|
|
this._index--;
|
|
}
|
|
else if (this.decodeEntities && c === "&") {
|
|
this.emitToken("onattribdata");
|
|
this.baseState = this._state;
|
|
this._state = 62 /* BeforeEntity */;
|
|
this.sectionStart = this._index;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateBeforeDeclaration = function (c) {
|
|
this._state =
|
|
c === "["
|
|
? 23 /* BeforeCdata1 */
|
|
: c === "-"
|
|
? 18 /* BeforeComment */
|
|
: 16 /* InDeclaration */;
|
|
};
|
|
Tokenizer.prototype.stateInDeclaration = function (c) {
|
|
if (c === ">") {
|
|
this.cbs.ondeclaration(this.getSection());
|
|
this._state = 1 /* Text */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInProcessingInstruction = function (c) {
|
|
if (c === ">") {
|
|
this.cbs.onprocessinginstruction(this.getSection());
|
|
this._state = 1 /* Text */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateBeforeComment = function (c) {
|
|
if (c === "-") {
|
|
this._state = 19 /* InComment */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else {
|
|
this._state = 16 /* InDeclaration */;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInComment = function (c) {
|
|
if (c === "-")
|
|
this._state = 21 /* AfterComment1 */;
|
|
};
|
|
Tokenizer.prototype.stateInSpecialComment = function (c) {
|
|
if (c === ">") {
|
|
this.cbs.oncomment(this.buffer.substring(this.sectionStart, this._index));
|
|
this._state = 1 /* Text */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateAfterComment1 = function (c) {
|
|
if (c === "-") {
|
|
this._state = 22 /* AfterComment2 */;
|
|
}
|
|
else {
|
|
this._state = 19 /* InComment */;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateAfterComment2 = function (c) {
|
|
if (c === ">") {
|
|
// Remove 2 trailing chars
|
|
this.cbs.oncomment(this.buffer.substring(this.sectionStart, this._index - 2));
|
|
this._state = 1 /* Text */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else if (c !== "-") {
|
|
this._state = 19 /* InComment */;
|
|
}
|
|
// Else: stay in AFTER_COMMENT_2 (`--->`)
|
|
};
|
|
Tokenizer.prototype.stateBeforeCdata6 = function (c) {
|
|
if (c === "[") {
|
|
this._state = 29 /* InCdata */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else {
|
|
this._state = 16 /* InDeclaration */;
|
|
this._index--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInCdata = function (c) {
|
|
if (c === "]")
|
|
this._state = 30 /* AfterCdata1 */;
|
|
};
|
|
Tokenizer.prototype.stateAfterCdata1 = function (c) {
|
|
if (c === "]")
|
|
this._state = 31 /* AfterCdata2 */;
|
|
else
|
|
this._state = 29 /* InCdata */;
|
|
};
|
|
Tokenizer.prototype.stateAfterCdata2 = function (c) {
|
|
if (c === ">") {
|
|
// Remove 2 trailing chars
|
|
this.cbs.oncdata(this.buffer.substring(this.sectionStart, this._index - 2));
|
|
this._state = 1 /* Text */;
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
else if (c !== "]") {
|
|
this._state = 29 /* InCdata */;
|
|
}
|
|
// Else: stay in AFTER_CDATA_2 (`]]]>`)
|
|
};
|
|
Tokenizer.prototype.stateBeforeSpecialS = function (c) {
|
|
if (c === "c" || c === "C") {
|
|
this._state = 34 /* BeforeScript1 */;
|
|
}
|
|
else if (c === "t" || c === "T") {
|
|
this._state = 44 /* BeforeStyle1 */;
|
|
}
|
|
else {
|
|
this._state = 3 /* InTagName */;
|
|
this._index--; // Consume the token again
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateBeforeSpecialSEnd = function (c) {
|
|
if (this.special === 2 /* Script */ && (c === "c" || c === "C")) {
|
|
this._state = 39 /* AfterScript1 */;
|
|
}
|
|
else if (this.special === 3 /* Style */ && (c === "t" || c === "T")) {
|
|
this._state = 48 /* AfterStyle1 */;
|
|
}
|
|
else
|
|
this._state = 1 /* Text */;
|
|
};
|
|
Tokenizer.prototype.stateBeforeSpecialLast = function (c, special) {
|
|
if (c === "/" || c === ">" || whitespace(c)) {
|
|
this.special = special;
|
|
}
|
|
this._state = 3 /* InTagName */;
|
|
this._index--; // Consume the token again
|
|
};
|
|
Tokenizer.prototype.stateAfterSpecialLast = function (c, sectionStartOffset) {
|
|
if (c === ">" || whitespace(c)) {
|
|
this.special = 1 /* None */;
|
|
this._state = 6 /* InClosingTagName */;
|
|
this.sectionStart = this._index - sectionStartOffset;
|
|
this._index--; // Reconsume the token
|
|
}
|
|
else
|
|
this._state = 1 /* Text */;
|
|
};
|
|
// For entities terminated with a semicolon
|
|
Tokenizer.prototype.parseFixedEntity = function (map) {
|
|
if (map === void 0) { map = this.xmlMode ? xml_json_1.default : entities_json_1.default; }
|
|
// Offset = 1
|
|
if (this.sectionStart + 1 < this._index) {
|
|
var entity = this.buffer.substring(this.sectionStart + 1, this._index);
|
|
if (Object.prototype.hasOwnProperty.call(map, entity)) {
|
|
this.emitPartial(map[entity]);
|
|
this.sectionStart = this._index + 1;
|
|
}
|
|
}
|
|
};
|
|
// Parses legacy entities (without trailing semicolon)
|
|
Tokenizer.prototype.parseLegacyEntity = function () {
|
|
var start = this.sectionStart + 1;
|
|
// The max length of legacy entities is 6
|
|
var limit = Math.min(this._index - start, 6);
|
|
while (limit >= 2) {
|
|
// The min length of legacy entities is 2
|
|
var entity = this.buffer.substr(start, limit);
|
|
if (Object.prototype.hasOwnProperty.call(legacy_json_1.default, entity)) {
|
|
this.emitPartial(legacy_json_1.default[entity]);
|
|
this.sectionStart += limit + 1;
|
|
return;
|
|
}
|
|
limit--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInNamedEntity = function (c) {
|
|
if (c === ";") {
|
|
this.parseFixedEntity();
|
|
// Retry as legacy entity if entity wasn't parsed
|
|
if (this.baseState === 1 /* Text */ &&
|
|
this.sectionStart + 1 < this._index &&
|
|
!this.xmlMode) {
|
|
this.parseLegacyEntity();
|
|
}
|
|
this._state = this.baseState;
|
|
}
|
|
else if ((c < "0" || c > "9") && !isASCIIAlpha(c)) {
|
|
if (this.xmlMode || this.sectionStart + 1 === this._index) {
|
|
// Ignore
|
|
}
|
|
else if (this.baseState !== 1 /* Text */) {
|
|
if (c !== "=") {
|
|
// Parse as legacy entity, without allowing additional characters.
|
|
this.parseFixedEntity(legacy_json_1.default);
|
|
}
|
|
}
|
|
else {
|
|
this.parseLegacyEntity();
|
|
}
|
|
this._state = this.baseState;
|
|
this._index--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.decodeNumericEntity = function (offset, base, strict) {
|
|
var sectionStart = this.sectionStart + offset;
|
|
if (sectionStart !== this._index) {
|
|
// Parse entity
|
|
var entity = this.buffer.substring(sectionStart, this._index);
|
|
var parsed = parseInt(entity, base);
|
|
this.emitPartial(decode_codepoint_1.default(parsed));
|
|
this.sectionStart = strict ? this._index + 1 : this._index;
|
|
}
|
|
this._state = this.baseState;
|
|
};
|
|
Tokenizer.prototype.stateInNumericEntity = function (c) {
|
|
if (c === ";") {
|
|
this.decodeNumericEntity(2, 10, true);
|
|
}
|
|
else if (c < "0" || c > "9") {
|
|
if (!this.xmlMode) {
|
|
this.decodeNumericEntity(2, 10, false);
|
|
}
|
|
else {
|
|
this._state = this.baseState;
|
|
}
|
|
this._index--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.stateInHexEntity = function (c) {
|
|
if (c === ";") {
|
|
this.decodeNumericEntity(3, 16, true);
|
|
}
|
|
else if ((c < "a" || c > "f") &&
|
|
(c < "A" || c > "F") &&
|
|
(c < "0" || c > "9")) {
|
|
if (!this.xmlMode) {
|
|
this.decodeNumericEntity(3, 16, false);
|
|
}
|
|
else {
|
|
this._state = this.baseState;
|
|
}
|
|
this._index--;
|
|
}
|
|
};
|
|
Tokenizer.prototype.cleanup = function () {
|
|
if (this.sectionStart < 0) {
|
|
this.buffer = "";
|
|
this.bufferOffset += this._index;
|
|
this._index = 0;
|
|
}
|
|
else if (this.running) {
|
|
if (this._state === 1 /* Text */) {
|
|
if (this.sectionStart !== this._index) {
|
|
this.cbs.ontext(this.buffer.substr(this.sectionStart));
|
|
}
|
|
this.buffer = "";
|
|
this.bufferOffset += this._index;
|
|
this._index = 0;
|
|
}
|
|
else if (this.sectionStart === this._index) {
|
|
// The section just started
|
|
this.buffer = "";
|
|
this.bufferOffset += this._index;
|
|
this._index = 0;
|
|
}
|
|
else {
|
|
// Remove everything unnecessary
|
|
this.buffer = this.buffer.substr(this.sectionStart);
|
|
this._index -= this.sectionStart;
|
|
this.bufferOffset += this.sectionStart;
|
|
}
|
|
this.sectionStart = 0;
|
|
}
|
|
};
|
|
/**
|
|
* Iterates through the buffer, calling the function corresponding to the current state.
|
|
*
|
|
* States that are more likely to be hit are higher up, as a performance improvement.
|
|
*/
|
|
Tokenizer.prototype.parse = function () {
|
|
while (this._index < this.buffer.length && this.running) {
|
|
var c = this.buffer.charAt(this._index);
|
|
if (this._state === 1 /* Text */) {
|
|
this.stateText(c);
|
|
}
|
|
else if (this._state === 12 /* InAttributeValueDq */) {
|
|
this.stateInAttributeValueDoubleQuotes(c);
|
|
}
|
|
else if (this._state === 9 /* InAttributeName */) {
|
|
this.stateInAttributeName(c);
|
|
}
|
|
else if (this._state === 19 /* InComment */) {
|
|
this.stateInComment(c);
|
|
}
|
|
else if (this._state === 20 /* InSpecialComment */) {
|
|
this.stateInSpecialComment(c);
|
|
}
|
|
else if (this._state === 8 /* BeforeAttributeName */) {
|
|
this.stateBeforeAttributeName(c);
|
|
}
|
|
else if (this._state === 3 /* InTagName */) {
|
|
this.stateInTagName(c);
|
|
}
|
|
else if (this._state === 6 /* InClosingTagName */) {
|
|
this.stateInClosingTagName(c);
|
|
}
|
|
else if (this._state === 2 /* BeforeTagName */) {
|
|
this.stateBeforeTagName(c);
|
|
}
|
|
else if (this._state === 10 /* AfterAttributeName */) {
|
|
this.stateAfterAttributeName(c);
|
|
}
|
|
else if (this._state === 13 /* InAttributeValueSq */) {
|
|
this.stateInAttributeValueSingleQuotes(c);
|
|
}
|
|
else if (this._state === 11 /* BeforeAttributeValue */) {
|
|
this.stateBeforeAttributeValue(c);
|
|
}
|
|
else if (this._state === 5 /* BeforeClosingTagName */) {
|
|
this.stateBeforeClosingTagName(c);
|
|
}
|
|
else if (this._state === 7 /* AfterClosingTagName */) {
|
|
this.stateAfterClosingTagName(c);
|
|
}
|
|
else if (this._state === 32 /* BeforeSpecialS */) {
|
|
this.stateBeforeSpecialS(c);
|
|
}
|
|
else if (this._state === 21 /* AfterComment1 */) {
|
|
this.stateAfterComment1(c);
|
|
}
|
|
else if (this._state === 14 /* InAttributeValueNq */) {
|
|
this.stateInAttributeValueNoQuotes(c);
|
|
}
|
|
else if (this._state === 4 /* InSelfClosingTag */) {
|
|
this.stateInSelfClosingTag(c);
|
|
}
|
|
else if (this._state === 16 /* InDeclaration */) {
|
|
this.stateInDeclaration(c);
|
|
}
|
|
else if (this._state === 15 /* BeforeDeclaration */) {
|
|
this.stateBeforeDeclaration(c);
|
|
}
|
|
else if (this._state === 22 /* AfterComment2 */) {
|
|
this.stateAfterComment2(c);
|
|
}
|
|
else if (this._state === 18 /* BeforeComment */) {
|
|
this.stateBeforeComment(c);
|
|
}
|
|
else if (this._state === 33 /* BeforeSpecialSEnd */) {
|
|
this.stateBeforeSpecialSEnd(c);
|
|
}
|
|
else if (this._state === 53 /* BeforeSpecialTEnd */) {
|
|
stateAfterSpecialTEnd(this, c);
|
|
}
|
|
else if (this._state === 39 /* AfterScript1 */) {
|
|
stateAfterScript1(this, c);
|
|
}
|
|
else if (this._state === 40 /* AfterScript2 */) {
|
|
stateAfterScript2(this, c);
|
|
}
|
|
else if (this._state === 41 /* AfterScript3 */) {
|
|
stateAfterScript3(this, c);
|
|
}
|
|
else if (this._state === 34 /* BeforeScript1 */) {
|
|
stateBeforeScript1(this, c);
|
|
}
|
|
else if (this._state === 35 /* BeforeScript2 */) {
|
|
stateBeforeScript2(this, c);
|
|
}
|
|
else if (this._state === 36 /* BeforeScript3 */) {
|
|
stateBeforeScript3(this, c);
|
|
}
|
|
else if (this._state === 37 /* BeforeScript4 */) {
|
|
stateBeforeScript4(this, c);
|
|
}
|
|
else if (this._state === 38 /* BeforeScript5 */) {
|
|
this.stateBeforeSpecialLast(c, 2 /* Script */);
|
|
}
|
|
else if (this._state === 42 /* AfterScript4 */) {
|
|
stateAfterScript4(this, c);
|
|
}
|
|
else if (this._state === 43 /* AfterScript5 */) {
|
|
this.stateAfterSpecialLast(c, 6);
|
|
}
|
|
else if (this._state === 44 /* BeforeStyle1 */) {
|
|
stateBeforeStyle1(this, c);
|
|
}
|
|
else if (this._state === 29 /* InCdata */) {
|
|
this.stateInCdata(c);
|
|
}
|
|
else if (this._state === 45 /* BeforeStyle2 */) {
|
|
stateBeforeStyle2(this, c);
|
|
}
|
|
else if (this._state === 46 /* BeforeStyle3 */) {
|
|
stateBeforeStyle3(this, c);
|
|
}
|
|
else if (this._state === 47 /* BeforeStyle4 */) {
|
|
this.stateBeforeSpecialLast(c, 3 /* Style */);
|
|
}
|
|
else if (this._state === 48 /* AfterStyle1 */) {
|
|
stateAfterStyle1(this, c);
|
|
}
|
|
else if (this._state === 49 /* AfterStyle2 */) {
|
|
stateAfterStyle2(this, c);
|
|
}
|
|
else if (this._state === 50 /* AfterStyle3 */) {
|
|
stateAfterStyle3(this, c);
|
|
}
|
|
else if (this._state === 51 /* AfterStyle4 */) {
|
|
this.stateAfterSpecialLast(c, 5);
|
|
}
|
|
else if (this._state === 52 /* BeforeSpecialT */) {
|
|
stateBeforeSpecialT(this, c);
|
|
}
|
|
else if (this._state === 54 /* BeforeTitle1 */) {
|
|
stateBeforeTitle1(this, c);
|
|
}
|
|
else if (this._state === 55 /* BeforeTitle2 */) {
|
|
stateBeforeTitle2(this, c);
|
|
}
|
|
else if (this._state === 56 /* BeforeTitle3 */) {
|
|
stateBeforeTitle3(this, c);
|
|
}
|
|
else if (this._state === 57 /* BeforeTitle4 */) {
|
|
this.stateBeforeSpecialLast(c, 4 /* Title */);
|
|
}
|
|
else if (this._state === 58 /* AfterTitle1 */) {
|
|
stateAfterTitle1(this, c);
|
|
}
|
|
else if (this._state === 59 /* AfterTitle2 */) {
|
|
stateAfterTitle2(this, c);
|
|
}
|
|
else if (this._state === 60 /* AfterTitle3 */) {
|
|
stateAfterTitle3(this, c);
|
|
}
|
|
else if (this._state === 61 /* AfterTitle4 */) {
|
|
this.stateAfterSpecialLast(c, 5);
|
|
}
|
|
else if (this._state === 17 /* InProcessingInstruction */) {
|
|
this.stateInProcessingInstruction(c);
|
|
}
|
|
else if (this._state === 64 /* InNamedEntity */) {
|
|
this.stateInNamedEntity(c);
|
|
}
|
|
else if (this._state === 23 /* BeforeCdata1 */) {
|
|
stateBeforeCdata1(this, c);
|
|
}
|
|
else if (this._state === 62 /* BeforeEntity */) {
|
|
stateBeforeEntity(this, c);
|
|
}
|
|
else if (this._state === 24 /* BeforeCdata2 */) {
|
|
stateBeforeCdata2(this, c);
|
|
}
|
|
else if (this._state === 25 /* BeforeCdata3 */) {
|
|
stateBeforeCdata3(this, c);
|
|
}
|
|
else if (this._state === 30 /* AfterCdata1 */) {
|
|
this.stateAfterCdata1(c);
|
|
}
|
|
else if (this._state === 31 /* AfterCdata2 */) {
|
|
this.stateAfterCdata2(c);
|
|
}
|
|
else if (this._state === 26 /* BeforeCdata4 */) {
|
|
stateBeforeCdata4(this, c);
|
|
}
|
|
else if (this._state === 27 /* BeforeCdata5 */) {
|
|
stateBeforeCdata5(this, c);
|
|
}
|
|
else if (this._state === 28 /* BeforeCdata6 */) {
|
|
this.stateBeforeCdata6(c);
|
|
}
|
|
else if (this._state === 66 /* InHexEntity */) {
|
|
this.stateInHexEntity(c);
|
|
}
|
|
else if (this._state === 65 /* InNumericEntity */) {
|
|
this.stateInNumericEntity(c);
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
}
|
|
else if (this._state === 63 /* BeforeNumericEntity */) {
|
|
stateBeforeNumericEntity(this, c);
|
|
}
|
|
else {
|
|
this.cbs.onerror(Error("unknown _state"), this._state);
|
|
}
|
|
this._index++;
|
|
}
|
|
this.cleanup();
|
|
};
|
|
Tokenizer.prototype.finish = function () {
|
|
// If there is remaining data, emit it in a reasonable way
|
|
if (this.sectionStart < this._index) {
|
|
this.handleTrailingData();
|
|
}
|
|
this.cbs.onend();
|
|
};
|
|
Tokenizer.prototype.handleTrailingData = function () {
|
|
var data = this.buffer.substr(this.sectionStart);
|
|
if (this._state === 29 /* InCdata */ ||
|
|
this._state === 30 /* AfterCdata1 */ ||
|
|
this._state === 31 /* AfterCdata2 */) {
|
|
this.cbs.oncdata(data);
|
|
}
|
|
else if (this._state === 19 /* InComment */ ||
|
|
this._state === 21 /* AfterComment1 */ ||
|
|
this._state === 22 /* AfterComment2 */) {
|
|
this.cbs.oncomment(data);
|
|
}
|
|
else if (this._state === 64 /* InNamedEntity */ && !this.xmlMode) {
|
|
this.parseLegacyEntity();
|
|
if (this.sectionStart < this._index) {
|
|
this._state = this.baseState;
|
|
this.handleTrailingData();
|
|
}
|
|
}
|
|
else if (this._state === 65 /* InNumericEntity */ && !this.xmlMode) {
|
|
this.decodeNumericEntity(2, 10, false);
|
|
if (this.sectionStart < this._index) {
|
|
this._state = this.baseState;
|
|
this.handleTrailingData();
|
|
}
|
|
}
|
|
else if (this._state === 66 /* InHexEntity */ && !this.xmlMode) {
|
|
this.decodeNumericEntity(3, 16, false);
|
|
if (this.sectionStart < this._index) {
|
|
this._state = this.baseState;
|
|
this.handleTrailingData();
|
|
}
|
|
}
|
|
else if (this._state !== 3 /* InTagName */ &&
|
|
this._state !== 8 /* BeforeAttributeName */ &&
|
|
this._state !== 11 /* BeforeAttributeValue */ &&
|
|
this._state !== 10 /* AfterAttributeName */ &&
|
|
this._state !== 9 /* InAttributeName */ &&
|
|
this._state !== 13 /* InAttributeValueSq */ &&
|
|
this._state !== 12 /* InAttributeValueDq */ &&
|
|
this._state !== 14 /* InAttributeValueNq */ &&
|
|
this._state !== 6 /* InClosingTagName */) {
|
|
this.cbs.ontext(data);
|
|
}
|
|
/*
|
|
* Else, ignore remaining data
|
|
* TODO add a way to remove current tag
|
|
*/
|
|
};
|
|
Tokenizer.prototype.getSection = function () {
|
|
return this.buffer.substring(this.sectionStart, this._index);
|
|
};
|
|
Tokenizer.prototype.emitToken = function (name) {
|
|
this.cbs[name](this.getSection());
|
|
this.sectionStart = -1;
|
|
};
|
|
Tokenizer.prototype.emitPartial = function (value) {
|
|
if (this.baseState !== 1 /* Text */) {
|
|
this.cbs.onattribdata(value); // TODO implement the new event
|
|
}
|
|
else {
|
|
this.cbs.ontext(value);
|
|
}
|
|
};
|
|
return Tokenizer;
|
|
}());
|
|
exports.default = Tokenizer;
|