htmlparser2-svelte
Version:
Fast & forgiving HTML/Svelte/XML/RSS parser
367 lines (366 loc) • 12.9 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parser = void 0;
var Tokenizer_1 = __importDefault(require("./Tokenizer"));
var events_1 = require("events");
var formTags = new Set([
"input",
"option",
"optgroup",
"select",
"button",
"datalist",
"textarea",
]);
var pTag = new Set(["p"]);
var openImpliesClose = {
tr: new Set(["tr", "th", "td"]),
th: new Set(["th"]),
td: new Set(["thead", "th", "td"]),
body: new Set(["head", "link", "script"]),
li: new Set(["li"]),
p: pTag,
h1: pTag,
h2: pTag,
h3: pTag,
h4: pTag,
h5: pTag,
h6: pTag,
select: formTags,
input: formTags,
output: formTags,
button: formTags,
datalist: formTags,
textarea: formTags,
option: new Set(["option"]),
optgroup: new Set(["optgroup", "option"]),
dd: new Set(["dt", "dd"]),
dt: new Set(["dt", "dd"]),
address: pTag,
article: pTag,
aside: pTag,
blockquote: pTag,
details: pTag,
div: pTag,
dl: pTag,
fieldset: pTag,
figcaption: pTag,
figure: pTag,
footer: pTag,
form: pTag,
header: pTag,
hr: pTag,
main: pTag,
nav: pTag,
ol: pTag,
pre: pTag,
section: pTag,
table: pTag,
ul: pTag,
rt: new Set(["rt", "rp"]),
rp: new Set(["rt", "rp"]),
tbody: new Set(["thead", "tbody"]),
tfoot: new Set(["thead", "tbody"]),
};
var voidElements = new Set([
"area",
"base",
"basefont",
"br",
"col",
"command",
"embed",
"frame",
"hr",
"img",
"input",
"isindex",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
]);
var foreignContextElements = new Set(["math", "svg"]);
var htmlIntegrationElements = new Set([
"mi",
"mo",
"mn",
"ms",
"mtext",
"annotation-xml",
"foreignObject",
"desc",
"title",
]);
var reNameEnd = /\s|\//;
var Parser = /** @class */ (function (_super) {
__extends(Parser, _super);
function Parser(cbs, options) {
var _a, _b, _c;
var _this = _super.call(this) || this;
_this._tagname = "";
_this._attribname = "";
_this._attribvalue = "";
_this._attribs = null;
_this._stack = [];
_this._foreignContext = [];
_this.startIndex = 0;
_this.endIndex = null;
// Aliases for backwards compatibility
_this.parseChunk = Parser.prototype.write;
_this.done = Parser.prototype.end;
_this._options = options || {};
_this._cbs = cbs || {};
_this._tagname = "";
_this._attribname = "";
_this._attribvalue = "";
_this._attribs = null;
_this._stack = [];
_this._foreignContext = [];
_this.startIndex = 0;
_this.endIndex = null;
_this._lowerCaseTagNames =
"lowerCaseTags" in _this._options
? !!_this._options.lowerCaseTags
: !_this._options.xmlMode;
_this._lowerCaseAttributeNames =
"lowerCaseAttributeNames" in _this._options
? !!_this._options.lowerCaseAttributeNames
: !_this._options.xmlMode;
_this._tokenizer = new ((_a = _this._options.Tokenizer) !== null && _a !== void 0 ? _a : Tokenizer_1.default)(_this._options, _this);
(_c = (_b = _this._cbs).onparserinit) === null || _c === void 0 ? void 0 : _c.call(_b, _this);
return _this;
}
Parser.prototype._updatePosition = function (initialOffset) {
if (this.endIndex === null) {
if (this._tokenizer._sectionStart <= initialOffset) {
this.startIndex = 0;
}
else {
this.startIndex = this._tokenizer._sectionStart - initialOffset;
}
}
else
this.startIndex = this.endIndex + 1;
this.endIndex = this._tokenizer.getAbsoluteIndex();
};
//Tokenizer event handlers
Parser.prototype.ontext = function (data) {
var _a, _b;
this._updatePosition(1);
if (this.endIndex !== null) {
this.endIndex--;
}
(_b = (_a = this._cbs).ontext) === null || _b === void 0 ? void 0 : _b.call(_a, data);
};
Parser.prototype.onopentagname = function (name) {
var _a, _b, _c;
if (this._lowerCaseTagNames) {
name = name.toLowerCase();
}
this._tagname = name;
if (!this._options.xmlMode &&
Object.prototype.hasOwnProperty.call(openImpliesClose, name)) {
for (var el = void 0; (_a = openImpliesClose[name]) === null || _a === void 0 ? void 0 : _a.has((el = this._stack[this._stack.length - 1])); this.onclosetag(el))
;
}
if (this._options.xmlMode || !voidElements.has(name)) {
this._stack.push(name);
if (foreignContextElements.has(name)) {
this._foreignContext.push(true);
}
else if (htmlIntegrationElements.has(name)) {
this._foreignContext.push(false);
}
}
(_c = (_b = this._cbs).onopentagname) === null || _c === void 0 ? void 0 : _c.call(_b, name);
if (this._cbs.onopentag)
this._attribs = {};
};
Parser.prototype.onopentagend = function () {
var _a, _b;
this._updatePosition(1);
if (this._attribs) {
(_b = (_a = this._cbs).onopentag) === null || _b === void 0 ? void 0 : _b.call(_a, this._tagname, this._attribs);
this._attribs = null;
}
if (!this._options.xmlMode &&
this._cbs.onclosetag &&
voidElements.has(this._tagname)) {
this._cbs.onclosetag(this._tagname);
}
this._tagname = "";
};
Parser.prototype.onclosetag = function (name) {
this._updatePosition(1);
if (this._lowerCaseTagNames) {
name = name.toLowerCase();
}
if (foreignContextElements.has(name) ||
htmlIntegrationElements.has(name)) {
this._foreignContext.pop();
}
if (this._stack.length &&
(this._options.xmlMode || !voidElements.has(name))) {
var pos = this._stack.lastIndexOf(name);
if (pos !== -1) {
if (this._cbs.onclosetag) {
pos = this._stack.length - pos;
while (pos--) {
// We know the stack has sufficient elements.
this._cbs.onclosetag(this._stack.pop());
}
}
else
this._stack.length = pos;
}
else if (name === "p" && !this._options.xmlMode) {
this.onopentagname(name);
this._closeCurrentTag();
}
}
else if (!this._options.xmlMode && (name === "br" || name === "p")) {
this.onopentagname(name);
this._closeCurrentTag();
}
};
Parser.prototype.onselfclosingtag = function () {
if (this._options.xmlMode ||
this._options.recognizeSelfClosing ||
this._foreignContext[this._foreignContext.length - 1]) {
this._closeCurrentTag();
}
else {
this.onopentagend();
}
};
Parser.prototype._closeCurrentTag = function () {
var _a, _b;
var name = this._tagname;
this.onopentagend();
//self-closing tags will be on the top of the stack
//(cheaper check than in onclosetag)
if (this._stack[this._stack.length - 1] === name) {
(_b = (_a = this._cbs).onclosetag) === null || _b === void 0 ? void 0 : _b.call(_a, name);
this._stack.pop();
}
};
Parser.prototype.onattribname = function (name) {
if (this._lowerCaseAttributeNames) {
name = name.toLowerCase();
}
this._attribname = name;
};
Parser.prototype.onattribdata = function (value) {
this._attribvalue += value;
};
Parser.prototype.onattribend = function () {
var _a, _b;
(_b = (_a = this._cbs).onattribute) === null || _b === void 0 ? void 0 : _b.call(_a, this._attribname, this._attribvalue);
if (this._attribs &&
!Object.prototype.hasOwnProperty.call(this._attribs, this._attribname)) {
this._attribs[this._attribname] = this._attribvalue;
}
this._attribname = "";
this._attribvalue = "";
};
Parser.prototype._getInstructionName = function (value) {
var idx = value.search(reNameEnd);
var name = idx < 0 ? value : value.substr(0, idx);
if (this._lowerCaseTagNames) {
name = name.toLowerCase();
}
return name;
};
Parser.prototype.ondeclaration = function (value) {
if (this._cbs.onprocessinginstruction) {
var name_1 = this._getInstructionName(value);
this._cbs.onprocessinginstruction("!" + name_1, "!" + value);
}
};
Parser.prototype.onprocessinginstruction = function (value) {
if (this._cbs.onprocessinginstruction) {
var name_2 = this._getInstructionName(value);
this._cbs.onprocessinginstruction("?" + name_2, "?" + value);
}
};
Parser.prototype.oncomment = function (value) {
var _a, _b, _c, _d;
this._updatePosition(4);
(_b = (_a = this._cbs).oncomment) === null || _b === void 0 ? void 0 : _b.call(_a, value);
(_d = (_c = this._cbs).oncommentend) === null || _d === void 0 ? void 0 : _d.call(_c);
};
Parser.prototype.oncdata = function (value) {
var _a, _b, _c, _d, _e, _f;
this._updatePosition(1);
if (this._options.xmlMode || this._options.recognizeCDATA) {
(_b = (_a = this._cbs).oncdatastart) === null || _b === void 0 ? void 0 : _b.call(_a);
(_d = (_c = this._cbs).ontext) === null || _d === void 0 ? void 0 : _d.call(_c, value);
(_f = (_e = this._cbs).oncdataend) === null || _f === void 0 ? void 0 : _f.call(_e);
}
else {
this.oncomment("[CDATA[" + value + "]]");
}
};
Parser.prototype.onerror = function (err) {
var _a, _b;
(_b = (_a = this._cbs).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, err);
};
Parser.prototype.onend = function () {
var _a, _b;
if (this._cbs.onclosetag) {
for (var i = this._stack.length; i > 0; this._cbs.onclosetag(this._stack[--i]))
;
}
(_b = (_a = this._cbs).onend) === null || _b === void 0 ? void 0 : _b.call(_a);
};
//Resets the parser to a blank state, ready to parse a new HTML document
Parser.prototype.reset = function () {
var _a, _b, _c, _d;
(_b = (_a = this._cbs).onreset) === null || _b === void 0 ? void 0 : _b.call(_a);
this._tokenizer.reset();
this._tagname = "";
this._attribname = "";
this._attribs = null;
this._stack = [];
(_d = (_c = this._cbs).onparserinit) === null || _d === void 0 ? void 0 : _d.call(_c, this);
};
//Parses a complete HTML document and pushes it to the handler
Parser.prototype.parseComplete = function (data) {
this.reset();
this.end(data);
};
Parser.prototype.write = function (chunk) {
this._tokenizer.write(chunk);
};
Parser.prototype.end = function (chunk) {
this._tokenizer.end(chunk);
};
Parser.prototype.pause = function () {
this._tokenizer.pause();
};
Parser.prototype.resume = function () {
this._tokenizer.resume();
};
return Parser;
}(events_1.EventEmitter));
exports.Parser = Parser;