html-dom-parser
Version:
HTML to DOM parser.
155 lines (154 loc) • 4.68 kB
JavaScript
const require_index = require("../../domelementtype/dist/index.js");
const require_node = require("./node.js");
//#region node_modules/domhandler/dist/index.js
const defaultOptions = {
withStartIndices: false,
withEndIndices: false,
xmlMode: false
};
/**
* Event-based handler that builds a DOM tree from parser callbacks.
*/
var DomHandler = class {
/** The elements of the DOM */
dom = [];
/** The root element for the DOM */
root = new require_node.Document(this.dom);
/** Called once parsing has completed. */
callback;
/** Settings for the handler. */
options;
/** Callback whenever a tag is closed. */
elementCB;
/** Indicated whether parsing has been completed. */
done = false;
/** Stack of open tags. */
tagStack = [this.root];
/** A data node that is still being written to. */
lastNode = null;
/** Reference to the parser instance. Used for location information. */
parser = null;
/**
* @param callback Called once parsing has completed.
* @param options Settings for the handler.
* @param elementCB Callback whenever a tag is closed.
*/
constructor(callback, options, elementCB) {
if (typeof options === "function") {
elementCB = options;
options = defaultOptions;
}
if (typeof callback === "object") {
options = callback;
callback = void 0;
}
this.callback = callback ?? null;
this.options = options ?? defaultOptions;
this.elementCB = elementCB ?? null;
}
onparserinit(parser) {
this.parser = parser;
}
onreset() {
this.dom = [];
this.root = new require_node.Document(this.dom);
this.done = false;
this.tagStack = [this.root];
this.lastNode = null;
this.parser = null;
}
onend() {
if (this.done) return;
this.done = true;
this.parser = null;
this.handleCallback(null);
}
onerror(error) {
this.handleCallback(error);
}
onclosetag() {
this.lastNode = null;
const element = this.tagStack.pop();
if (this.options.withEndIndices && this.parser) element.endIndex = this.parser.endIndex;
if (this.elementCB) this.elementCB(element);
}
onopentag(name, attribs) {
const element = new require_node.Element(name, attribs, void 0, this.options.xmlMode ? require_index.ElementType.Tag : void 0);
this.addNode(element);
this.tagStack.push(element);
}
ontext(data) {
const { lastNode } = this;
if (lastNode && lastNode.type === require_index.ElementType.Text) {
lastNode.data += data;
if (this.options.withEndIndices && this.parser) lastNode.endIndex = this.parser.endIndex;
} else {
const node = new require_node.Text(data);
this.addNode(node);
this.lastNode = node;
}
}
oncomment(data) {
if (this.lastNode && this.lastNode.type === require_index.ElementType.Comment) {
this.lastNode.data += data;
return;
}
const node = new require_node.Comment(data);
this.addNode(node);
this.lastNode = node;
}
oncommentend() {
this.lastNode = null;
}
oncdatastart() {
const text = new require_node.Text("");
const node = new require_node.CDATA([text]);
this.addNode(node);
text.parent = node;
this.lastNode = text;
}
oncdataend() {
this.lastNode = null;
}
onprocessinginstruction(name, data) {
const node = new require_node.ProcessingInstruction(name, data);
this.addNode(node);
}
handleCallback(error) {
if (typeof this.callback === "function") this.callback(error, this.dom);
else if (error) throw error;
}
addNode(node) {
const parent = this.tagStack[this.tagStack.length - 1];
const previousSibling = parent.children[parent.children.length - 1];
if (this.options.withStartIndices && this.parser) node.startIndex = this.parser.startIndex;
if (this.options.withEndIndices && this.parser) node.endIndex = this.parser.endIndex;
parent.children.push(node);
if (previousSibling) {
node.prev = previousSibling;
previousSibling.next = node;
}
node.parent = parent;
this.lastNode = null;
}
};
//#endregion
exports.CDATA = require_node.CDATA;
exports.Comment = require_node.Comment;
exports.DataNode = require_node.DataNode;
exports.Document = require_node.Document;
exports.DomHandler = DomHandler;
exports.default = DomHandler;
exports.Element = require_node.Element;
exports.Node = require_node.Node;
exports.NodeWithChildren = require_node.NodeWithChildren;
exports.ProcessingInstruction = require_node.ProcessingInstruction;
exports.Text = require_node.Text;
exports.cloneNode = require_node.cloneNode;
exports.isCDATA = require_node.isCDATA;
exports.isComment = require_node.isComment;
exports.isDirective = require_node.isDirective;
exports.isDocument = require_node.isDocument;
exports.isTag = require_node.isTag;
exports.isText = require_node.isText;
//# sourceMappingURL=index.js.map