Socket
Socket
Sign inDemoInstall

domhandler

Package Overview
Dependencies
1
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.3.0 to 4.0.0

README.md

20

lib/index.d.ts

@@ -1,2 +0,2 @@

import { Node, Element, DataNode } from "./node";
import { Node, Element, DataNode, NodeWithChildren, Document } from "./node";
export * from "./node";

@@ -37,18 +37,20 @@ export interface DomHandlerOptions {

export declare class DomHandler {
/** The constructed DOM */
/** The elements of the DOM */
dom: Node[];
/** The root element for the DOM */
root: Document;
/** Called once parsing has completed. */
private readonly _callback;
private readonly callback;
/** Settings for the handler. */
private readonly _options;
private readonly options;
/** Callback whenever a tag is closed. */
private readonly _elementCB;
private readonly elementCB;
/** Indicated whether parsing has been completed. */
private _done;
private done;
/** Stack of open tags. */
private _tagStack;
protected tagStack: NodeWithChildren[];
/** A data node that is still being written to. */
private _lastNode;
protected lastNode: DataNode | null;
/** Reference to the parser instance. Used for location information. */
private _parser;
private parser;
/**

@@ -55,0 +57,0 @@ * @param callback Called once parsing has completed.

@@ -30,12 +30,14 @@ "use strict";

function DomHandler(callback, options, elementCB) {
/** The constructed DOM */
/** The elements of the DOM */
this.dom = [];
/** The root element for the DOM */
this.root = new node_1.Document(this.dom);
/** Indicated whether parsing has been completed. */
this._done = false;
this.done = false;
/** Stack of open tags. */
this._tagStack = [];
this.tagStack = [this.root];
/** A data node that is still being written to. */
this._lastNode = null;
this.lastNode = null;
/** Reference to the parser instance. Used for location information. */
this._parser = null;
this.parser = null;
// Make it possible to skip arguments, for backwards-compatibility

@@ -50,8 +52,8 @@ if (typeof options === "function") {

}
this._callback = callback !== null && callback !== void 0 ? callback : null;
this._options = options !== null && options !== void 0 ? options : defaultOpts;
this._elementCB = elementCB !== null && elementCB !== void 0 ? elementCB : null;
this.callback = callback !== null && callback !== void 0 ? callback : null;
this.options = options !== null && options !== void 0 ? options : defaultOpts;
this.elementCB = elementCB !== null && elementCB !== void 0 ? elementCB : null;
}
DomHandler.prototype.onparserinit = function (parser) {
this._parser = parser;
this.parser = parser;
};

@@ -62,13 +64,14 @@ // Resets the handler back to starting state

this.dom = [];
this._done = false;
this._tagStack = [];
this._lastNode = null;
this._parser = (_a = this._parser) !== null && _a !== void 0 ? _a : null;
this.root = new node_1.Document(this.dom);
this.done = false;
this.tagStack = [this.root];
this.lastNode = null;
this.parser = (_a = this.parser) !== null && _a !== void 0 ? _a : null;
};
// Signals the handler that parsing is done
DomHandler.prototype.onend = function () {
if (this._done)
if (this.done)
return;
this._done = true;
this._parser = null;
this.done = true;
this.parser = null;
this.handleCallback(null);

@@ -80,12 +83,9 @@ };

DomHandler.prototype.onclosetag = function () {
this._lastNode = null;
var elem = this._tagStack.pop();
if (!elem || !this._parser) {
return;
this.lastNode = null;
var elem = this.tagStack.pop();
if (this.options.withEndIndices) {
elem.endIndex = this.parser.endIndex;
}
if (this._options.withEndIndices) {
elem.endIndex = this._parser.endIndex;
}
if (this._elementCB)
this._elementCB(elem);
if (this.elementCB)
this.elementCB(elem);
};

@@ -95,17 +95,17 @@ DomHandler.prototype.onopentag = function (name, attribs) {

this.addNode(element);
this._tagStack.push(element);
this.tagStack.push(element);
};
DomHandler.prototype.ontext = function (data) {
var normalize = this._options.normalizeWhitespace;
var _lastNode = this._lastNode;
if (_lastNode && _lastNode.type === "text" /* Text */) {
if (normalize) {
_lastNode.data = (_lastNode.data + data).replace(reWhitespace, " ");
var normalizeWhitespace = this.options.normalizeWhitespace;
var lastNode = this.lastNode;
if (lastNode && lastNode.type === "text" /* Text */) {
if (normalizeWhitespace) {
lastNode.data = (lastNode.data + data).replace(reWhitespace, " ");
}
else {
_lastNode.data += data;
lastNode.data += data;
}
}
else {
if (normalize) {
if (normalizeWhitespace) {
data = data.replace(reWhitespace, " ");

@@ -115,8 +115,8 @@ }

this.addNode(node);
this._lastNode = node;
this.lastNode = node;
}
};
DomHandler.prototype.oncomment = function (data) {
if (this._lastNode && this._lastNode.type === "comment" /* Comment */) {
this._lastNode.data += data;
if (this.lastNode && this.lastNode.type === "comment" /* Comment */) {
this.lastNode.data += data;
return;

@@ -126,6 +126,6 @@ }

this.addNode(node);
this._lastNode = node;
this.lastNode = node;
};
DomHandler.prototype.oncommentend = function () {
this._lastNode = null;
this.lastNode = null;
};

@@ -137,6 +137,6 @@ DomHandler.prototype.oncdatastart = function () {

text.parent = node;
this._lastNode = text;
this.lastNode = text;
};
DomHandler.prototype.oncdataend = function () {
this._lastNode = null;
this.lastNode = null;
};

@@ -148,4 +148,4 @@ DomHandler.prototype.onprocessinginstruction = function (name, data) {

DomHandler.prototype.handleCallback = function (error) {
if (typeof this._callback === "function") {
this._callback(error, this.dom);
if (typeof this.callback === "function") {
this.callback(error, this.dom);
}

@@ -157,14 +157,11 @@ else if (error) {

DomHandler.prototype.addNode = function (node) {
var parent = this._tagStack[this._tagStack.length - 1];
var siblings = parent ? parent.children : this.dom;
var previousSibling = siblings[siblings.length - 1];
if (this._parser) {
if (this._options.withStartIndices) {
node.startIndex = this._parser.startIndex;
}
if (this._options.withEndIndices) {
node.endIndex = this._parser.endIndex;
}
var parent = this.tagStack[this.tagStack.length - 1];
var previousSibling = parent.children[parent.children.length - 1];
if (this.options.withStartIndices) {
node.startIndex = this.parser.startIndex;
}
siblings.push(node);
if (this.options.withEndIndices) {
node.endIndex = this.parser.endIndex;
}
parent.children.push(node);
if (previousSibling) {

@@ -174,10 +171,8 @@ node.prev = previousSibling;

}
if (parent) {
node.parent = parent;
}
this._lastNode = null;
node.parent = parent;
this.lastNode = null;
};
DomHandler.prototype.addDataNode = function (node) {
this.addNode(node);
this._lastNode = node;
this.lastNode = node;
};

@@ -184,0 +179,0 @@ return DomHandler;

@@ -57,11 +57,16 @@ import { ElementType } from "domelementtype";

constructor(name: string, data: string);
"x-name"?: string;
"x-publicId"?: string;
"x-systemId"?: string;
}
/**
* A `Node` that can have children.
*/
export declare class NodeWithChildren extends Node {
children: Node[];
/**
*
* @param type Type of the node.
* @param children Children of the node. Only certain node types can have children.
*/
constructor(type: ElementType.CDATA | ElementType.Script | ElementType.Style | ElementType.Tag, children: Node[]);
constructor(type: ElementType.Root | ElementType.CDATA | ElementType.Script | ElementType.Style | ElementType.Tag, children: Node[]);
get firstChild(): Node | null;

@@ -72,2 +77,12 @@ get lastChild(): Node | null;

}
export declare class Document extends NodeWithChildren {
constructor(children: Node[]);
"x-mode"?: "no-quirks" | "quirks" | "limited-quirks";
}
interface Attribute {
name: string;
value: string;
namespace?: string;
prefix?: string;
}
export declare class Element extends NodeWithChildren {

@@ -88,6 +103,5 @@ name: string;

set tagName(name: string);
get attributes(): {
name: string;
value: string;
}[];
get attributes(): Attribute[];
"x-attribsNamespace"?: Record<string, string>;
"x-attribsPrefix"?: Record<string, string>;
}

@@ -101,2 +115,3 @@ /**

export declare function cloneNode(node: Node, recursive?: boolean): Node;
export {};
//# sourceMappingURL=node.d.ts.map

@@ -27,3 +27,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.cloneNode = exports.Element = exports.NodeWithChildren = exports.ProcessingInstruction = exports.Comment = exports.Text = exports.DataNode = exports.Node = void 0;
exports.cloneNode = exports.Element = exports.Document = exports.NodeWithChildren = exports.ProcessingInstruction = exports.Comment = exports.Text = exports.DataNode = exports.Node = void 0;
var nodeTypes = new Map([

@@ -37,2 +37,3 @@ ["tag" /* Tag */, 1],

["comment" /* Comment */, 8],
["root" /* Root */, 9],
]);

@@ -164,6 +165,8 @@ /**

exports.ProcessingInstruction = ProcessingInstruction;
/**
* A `Node` that can have children.
*/
var NodeWithChildren = /** @class */ (function (_super) {
__extends(NodeWithChildren, _super);
/**
*
* @param type Type of the node.

@@ -208,2 +211,10 @@ * @param children Children of the node. Only certain node types can have children.

exports.NodeWithChildren = NodeWithChildren;
var Document = /** @class */ (function (_super) {
__extends(Document, _super);
function Document(children) {
return _super.call(this, "root" /* Root */, children) || this;
}
return Document;
}(NodeWithChildren));
exports.Document = Document;
var Element = /** @class */ (function (_super) {

@@ -242,6 +253,11 @@ __extends(Element, _super);

var _this = this;
return Object.keys(this.attribs).map(function (name) { return ({
name: name,
value: _this.attribs[name],
}); });
return Object.keys(this.attribs).map(function (name) {
var _a, _b;
return ({
name: name,
value: _this.attribs[name],
namespace: (_a = _this["x-attribsNamespace"]) === null || _a === void 0 ? void 0 : _a[name],
prefix: (_b = _this["x-attribsPrefix"]) === null || _b === void 0 ? void 0 : _b[name],
});
});
},

@@ -262,11 +278,20 @@ enumerable: false,

if (recursive === void 0) { recursive = false; }
var result;
switch (node.type) {
case "text" /* Text */:
return new Text(node.data);
result = new Text(node.data);
break;
case "directive" /* Directive */: {
var instr = node;
return new ProcessingInstruction(instr.name, instr.data);
result = new ProcessingInstruction(instr.name, instr.data);
if (instr["x-name"] != null) {
result["x-name"] = instr["x-name"];
result["x-publicId"] = instr["x-publicId"];
result["x-systemId"] = instr["x-systemId"];
}
break;
}
case "comment" /* Comment */:
return new Comment(node.data);
result = new Comment(node.data);
break;
case "tag" /* Tag */:

@@ -279,3 +304,10 @@ case "script" /* Script */:

children.forEach(function (child) { return (child.parent = clone_1); });
return clone_1;
if (elem["x-attribsNamespace"]) {
clone_1["x-attribsNamespace"] = __assign({}, elem["x-attribsNamespace"]);
}
if (elem["x-attribsPrefix"]) {
clone_1["x-attribsPrefix"] = __assign({}, elem["x-attribsPrefix"]);
}
result = clone_1;
break;
}

@@ -285,6 +317,18 @@ case "cdata" /* CDATA */: {

var children = recursive ? cloneChildren(cdata.children) : [];
var clone_2 = new NodeWithChildren("cdata" /* CDATA */, children);
var clone_2 = new NodeWithChildren(node.type, children);
children.forEach(function (child) { return (child.parent = clone_2); });
return clone_2;
result = clone_2;
break;
}
case "root" /* Root */: {
var doc = node;
var children = recursive ? cloneChildren(doc.children) : [];
var clone_3 = new Document(children);
children.forEach(function (child) { return (child.parent = clone_3); });
if (doc["x-mode"]) {
clone_3["x-mode"] = doc["x-mode"];
}
result = clone_3;
break;
}
case "doctype" /* Doctype */: {

@@ -295,2 +339,5 @@ // This type isn't used yet.

}
result.startIndex = node.startIndex;
result.endIndex = node.endIndex;
return result;
}

@@ -297,0 +344,0 @@ exports.cloneNode = cloneNode;

{
"name": "domhandler",
"version": "3.3.0",
"version": "4.0.0",
"description": "Handler for htmlparser2 that turns pages into a dom",
"author": "Felix Boehm <me@feedic.com>",
"funding": "https://github.com/fb55/domhandler?sponsor=1",
"funding": {
"url": "https://github.com/fb55/domhandler?sponsor=1"
},
"license": "BSD-2-Clause",

@@ -34,3 +36,3 @@ "main": "lib/index.js",

"dependencies": {
"domelementtype": "^2.0.1"
"domelementtype": "^2.1.0"
},

@@ -45,3 +47,3 @@ "devDependencies": {

"eslint-config-prettier": "^6.0.0",
"htmlparser2": "^4.1.0",
"htmlparser2": "^5.0.0",
"jest": "^26.0.1",

@@ -48,0 +50,0 @@ "prettier": "^2.0.5",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc