domhandler
Advanced tools
Comparing version 4.3.1 to 5.0.0
@@ -1,3 +0,3 @@ | ||
import { Node, Element, DataNode, NodeWithChildren, Document } from "./node"; | ||
export * from "./node"; | ||
import { ChildNode, Element, DataNode, Document, ParentNode } from "./node.js"; | ||
export * from "./node.js"; | ||
export interface DomHandlerOptions { | ||
@@ -21,11 +21,2 @@ /** | ||
/** | ||
* Replace all whitespace with single spaces. | ||
* | ||
* **Note:** Enabling this might break your markup. | ||
* | ||
* @default false | ||
* @deprecated | ||
*/ | ||
normalizeWhitespace?: boolean; | ||
/** | ||
* Treat the markup as XML. | ||
@@ -41,7 +32,7 @@ * | ||
} | ||
declare type Callback = (error: Error | null, dom: Node[]) => void; | ||
declare type Callback = (error: Error | null, dom: ChildNode[]) => void; | ||
declare type ElementCallback = (element: Element) => void; | ||
export declare class DomHandler { | ||
/** The elements of the DOM */ | ||
dom: Node[]; | ||
dom: ChildNode[]; | ||
/** The root element for the DOM */ | ||
@@ -58,3 +49,3 @@ root: Document; | ||
/** Stack of open tags. */ | ||
protected tagStack: NodeWithChildren[]; | ||
protected tagStack: ParentNode[]; | ||
/** A data node that is still being written to. */ | ||
@@ -85,5 +76,5 @@ protected lastNode: DataNode | null; | ||
protected handleCallback(error: Error | null): void; | ||
protected addNode(node: Node): void; | ||
protected addNode(node: ChildNode): void; | ||
} | ||
export default DomHandler; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -19,8 +19,6 @@ "use strict"; | ||
var domelementtype_1 = require("domelementtype"); | ||
var node_1 = require("./node"); | ||
__exportStar(require("./node"), exports); | ||
var reWhitespace = /\s+/g; | ||
var node_js_1 = require("./node.js"); | ||
__exportStar(require("./node.js"), exports); | ||
// Default options | ||
var defaultOpts = { | ||
normalizeWhitespace: false, | ||
withStartIndices: false, | ||
@@ -40,3 +38,3 @@ withEndIndices: false, | ||
/** The root element for the DOM */ | ||
this.root = new node_1.Document(this.dom); | ||
this.root = new node_js_1.Document(this.dom); | ||
/** Indicated whether parsing has been completed. */ | ||
@@ -69,3 +67,3 @@ this.done = false; | ||
this.dom = []; | ||
this.root = new node_1.Document(this.dom); | ||
this.root = new node_js_1.Document(this.dom); | ||
this.done = false; | ||
@@ -98,3 +96,3 @@ this.tagStack = [this.root]; | ||
var type = this.options.xmlMode ? domelementtype_1.ElementType.Tag : undefined; | ||
var element = new node_1.Element(name, attribs, undefined, type); | ||
var element = new node_js_1.Element(name, attribs, undefined, type); | ||
this.addNode(element); | ||
@@ -104,11 +102,5 @@ this.tagStack.push(element); | ||
DomHandler.prototype.ontext = function (data) { | ||
var normalizeWhitespace = this.options.normalizeWhitespace; | ||
var lastNode = this.lastNode; | ||
if (lastNode && lastNode.type === domelementtype_1.ElementType.Text) { | ||
if (normalizeWhitespace) { | ||
lastNode.data = (lastNode.data + data).replace(reWhitespace, " "); | ||
} | ||
else { | ||
lastNode.data += data; | ||
} | ||
lastNode.data += data; | ||
if (this.options.withEndIndices) { | ||
@@ -119,6 +111,3 @@ lastNode.endIndex = this.parser.endIndex; | ||
else { | ||
if (normalizeWhitespace) { | ||
data = data.replace(reWhitespace, " "); | ||
} | ||
var node = new node_1.Text(data); | ||
var node = new node_js_1.Text(data); | ||
this.addNode(node); | ||
@@ -133,3 +122,3 @@ this.lastNode = node; | ||
} | ||
var node = new node_1.Comment(data); | ||
var node = new node_js_1.Comment(data); | ||
this.addNode(node); | ||
@@ -142,4 +131,4 @@ this.lastNode = node; | ||
DomHandler.prototype.oncdatastart = function () { | ||
var text = new node_1.Text(""); | ||
var node = new node_1.NodeWithChildren(domelementtype_1.ElementType.CDATA, [text]); | ||
var text = new node_js_1.Text(""); | ||
var node = new node_js_1.CDATA([text]); | ||
this.addNode(node); | ||
@@ -153,3 +142,3 @@ text.parent = node; | ||
DomHandler.prototype.onprocessinginstruction = function (name, data) { | ||
var node = new node_1.ProcessingInstruction(name, data); | ||
var node = new node_js_1.ProcessingInstruction(name, data); | ||
this.addNode(node); | ||
@@ -156,0 +145,0 @@ }; |
@@ -20,2 +20,5 @@ import { ElementType } from "domelementtype"; | ||
} | ||
export declare type ParentNode = Document | Element | CDATA; | ||
export declare type ChildNode = DataNode | Element | CDATA; | ||
export declare type AnyNode = ParentNode | ChildNode; | ||
/** | ||
@@ -25,10 +28,11 @@ * This object will be used as the prototype for Nodes when creating a | ||
*/ | ||
export declare class Node { | ||
type: ElementType; | ||
export declare abstract class Node { | ||
/** The type of the node. */ | ||
abstract readonly type: ElementType; | ||
/** Parent of the node */ | ||
parent: NodeWithChildren | null; | ||
parent: ParentNode | null; | ||
/** Previous sibling */ | ||
prev: Node | null; | ||
prev: ChildNode | null; | ||
/** Next sibling */ | ||
next: Node | null; | ||
next: ChildNode | null; | ||
/** The start index of the node. Requires `withStartIndices` on the handler to be `true. */ | ||
@@ -45,11 +49,6 @@ startIndex: number | null; | ||
/** | ||
* | ||
* @param type The type of the node. | ||
*/ | ||
constructor(type: ElementType); | ||
/** | ||
* [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible | ||
* node {@link type}. | ||
*/ | ||
get nodeType(): number; | ||
abstract readonly nodeType: number; | ||
/** | ||
@@ -59,4 +58,4 @@ * Same as {@link parent}. | ||
*/ | ||
get parentNode(): NodeWithChildren | null; | ||
set parentNode(parent: NodeWithChildren | null); | ||
get parentNode(): ParentNode | null; | ||
set parentNode(parent: ParentNode | null); | ||
/** | ||
@@ -66,4 +65,4 @@ * Same as {@link prev}. | ||
*/ | ||
get previousSibling(): Node | null; | ||
set previousSibling(prev: Node | null); | ||
get previousSibling(): ChildNode | null; | ||
set previousSibling(prev: ChildNode | null); | ||
/** | ||
@@ -73,4 +72,4 @@ * Same as {@link next}. | ||
*/ | ||
get nextSibling(): Node | null; | ||
set nextSibling(next: Node | null); | ||
get nextSibling(): ChildNode | null; | ||
set nextSibling(next: ChildNode | null); | ||
/** | ||
@@ -87,9 +86,8 @@ * Clone this node, and optionally its children. | ||
*/ | ||
export declare class DataNode extends Node { | ||
export declare abstract class DataNode extends Node { | ||
data: string; | ||
/** | ||
* @param type The type of the node | ||
* @param data The content of the data node | ||
*/ | ||
constructor(type: ElementType.Comment | ElementType.Text | ElementType.Directive, data: string); | ||
constructor(data: string); | ||
/** | ||
@@ -106,3 +104,4 @@ * Same as {@link data}. | ||
export declare class Text extends DataNode { | ||
constructor(data: string); | ||
type: ElementType.Text; | ||
get nodeType(): 3; | ||
} | ||
@@ -113,3 +112,4 @@ /** | ||
export declare class Comment extends DataNode { | ||
constructor(data: string); | ||
type: ElementType.Comment; | ||
get nodeType(): 8; | ||
} | ||
@@ -121,3 +121,5 @@ /** | ||
name: string; | ||
type: ElementType.Directive; | ||
constructor(name: string, data: string); | ||
get nodeType(): 1; | ||
/** If this is a doctype, the document type name (parse5 only). */ | ||
@@ -133,13 +135,12 @@ "x-name"?: string; | ||
*/ | ||
export declare class NodeWithChildren extends Node { | ||
children: Node[]; | ||
export declare abstract class NodeWithChildren extends Node { | ||
children: ChildNode[]; | ||
/** | ||
* @param type Type of the node. | ||
* @param children Children of the node. Only certain node types can have children. | ||
*/ | ||
constructor(type: ElementType.Root | ElementType.CDATA | ElementType.Script | ElementType.Style | ElementType.Tag, children: Node[]); | ||
constructor(children: ChildNode[]); | ||
/** First child of the node. */ | ||
get firstChild(): Node | null; | ||
get firstChild(): ChildNode | null; | ||
/** Last child of the node. */ | ||
get lastChild(): Node | null; | ||
get lastChild(): ChildNode | null; | ||
/** | ||
@@ -149,5 +150,9 @@ * Same as {@link children}. | ||
*/ | ||
get childNodes(): Node[]; | ||
set childNodes(children: Node[]); | ||
get childNodes(): ChildNode[]; | ||
set childNodes(children: ChildNode[]); | ||
} | ||
export declare class CDATA extends NodeWithChildren { | ||
type: ElementType.CDATA; | ||
get nodeType(): 4; | ||
} | ||
/** | ||
@@ -157,3 +162,4 @@ * The root node of the document. | ||
export declare class Document extends NodeWithChildren { | ||
constructor(children: Node[]); | ||
type: ElementType.Root; | ||
get nodeType(): 9; | ||
/** [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks) (parse5 only). */ | ||
@@ -179,2 +185,3 @@ "x-mode"?: "no-quirks" | "quirks" | "limited-quirks"; | ||
}; | ||
type: ElementType.Tag | ElementType.Script | ElementType.Style; | ||
/** | ||
@@ -187,3 +194,4 @@ * @param name Name of the tag, eg. `div`, `span`. | ||
[name: string]: string; | ||
}, children?: Node[], type?: ElementType.Tag | ElementType.Script | ElementType.Style); | ||
}, children?: ChildNode[], type?: ElementType.Tag | ElementType.Script | ElementType.Style); | ||
get nodeType(): 1; | ||
/** | ||
@@ -218,3 +226,3 @@ * `parse5` source code location info, with start & end tags. | ||
*/ | ||
export declare function isCDATA(node: Node): node is NodeWithChildren; | ||
export declare function isCDATA(node: Node): node is CDATA; | ||
/** | ||
@@ -242,5 +250,5 @@ * @param node Node to check. | ||
* @param node Node to check. | ||
* @returns `true` if the node is a `NodeWithChildren` (has children), `false` otherwise. | ||
* @returns `true` if the node has children, `false` otherwise. | ||
*/ | ||
export declare function hasChildren(node: Node): node is NodeWithChildren; | ||
export declare function hasChildren(node: Node): node is ParentNode; | ||
/** | ||
@@ -247,0 +255,0 @@ * Clone a node, and optionally its children. |
122
lib/node.js
@@ -29,14 +29,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cloneNode = exports.hasChildren = exports.isDocument = exports.isDirective = exports.isComment = exports.isText = exports.isCDATA = exports.isTag = exports.Element = exports.Document = exports.NodeWithChildren = exports.ProcessingInstruction = exports.Comment = exports.Text = exports.DataNode = exports.Node = void 0; | ||
exports.cloneNode = exports.hasChildren = exports.isDocument = exports.isDirective = exports.isComment = exports.isText = exports.isCDATA = exports.isTag = exports.Element = exports.Document = exports.CDATA = exports.NodeWithChildren = exports.ProcessingInstruction = exports.Comment = exports.Text = exports.DataNode = exports.Node = void 0; | ||
var domelementtype_1 = require("domelementtype"); | ||
var nodeTypes = new Map([ | ||
[domelementtype_1.ElementType.Tag, 1], | ||
[domelementtype_1.ElementType.Script, 1], | ||
[domelementtype_1.ElementType.Style, 1], | ||
[domelementtype_1.ElementType.Directive, 1], | ||
[domelementtype_1.ElementType.Text, 3], | ||
[domelementtype_1.ElementType.CDATA, 4], | ||
[domelementtype_1.ElementType.Comment, 8], | ||
[domelementtype_1.ElementType.Root, 9], | ||
]); | ||
/** | ||
@@ -47,8 +37,3 @@ * This object will be used as the prototype for Nodes when creating a | ||
var Node = /** @class */ (function () { | ||
/** | ||
* | ||
* @param type The type of the node. | ||
*/ | ||
function Node(type) { | ||
this.type = type; | ||
function Node() { | ||
/** Parent of the node */ | ||
@@ -65,15 +50,2 @@ this.parent = null; | ||
} | ||
Object.defineProperty(Node.prototype, "nodeType", { | ||
// Read-only aliases | ||
/** | ||
* [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible | ||
* node {@link type}. | ||
*/ | ||
get: function () { | ||
var _a; | ||
return (_a = nodeTypes.get(this.type)) !== null && _a !== void 0 ? _a : 1; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Node.prototype, "parentNode", { | ||
@@ -141,7 +113,6 @@ // Read-write aliases for properties | ||
/** | ||
* @param type The type of the node | ||
* @param data The content of the data node | ||
*/ | ||
function DataNode(type, data) { | ||
var _this = _super.call(this, type) || this; | ||
function DataNode(data) { | ||
var _this = _super.call(this) || this; | ||
_this.data = data; | ||
@@ -172,5 +143,14 @@ return _this; | ||
__extends(Text, _super); | ||
function Text(data) { | ||
return _super.call(this, domelementtype_1.ElementType.Text, data) || this; | ||
function Text() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.type = domelementtype_1.ElementType.Text; | ||
return _this; | ||
} | ||
Object.defineProperty(Text.prototype, "nodeType", { | ||
get: function () { | ||
return 3; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
return Text; | ||
@@ -184,5 +164,14 @@ }(DataNode)); | ||
__extends(Comment, _super); | ||
function Comment(data) { | ||
return _super.call(this, domelementtype_1.ElementType.Comment, data) || this; | ||
function Comment() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.type = domelementtype_1.ElementType.Comment; | ||
return _this; | ||
} | ||
Object.defineProperty(Comment.prototype, "nodeType", { | ||
get: function () { | ||
return 8; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
return Comment; | ||
@@ -197,6 +186,14 @@ }(DataNode)); | ||
function ProcessingInstruction(name, data) { | ||
var _this = _super.call(this, domelementtype_1.ElementType.Directive, data) || this; | ||
var _this = _super.call(this, data) || this; | ||
_this.name = name; | ||
_this.type = domelementtype_1.ElementType.Directive; | ||
return _this; | ||
} | ||
Object.defineProperty(ProcessingInstruction.prototype, "nodeType", { | ||
get: function () { | ||
return 1; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
return ProcessingInstruction; | ||
@@ -211,7 +208,6 @@ }(DataNode)); | ||
/** | ||
* @param type Type of the node. | ||
* @param children Children of the node. Only certain node types can have children. | ||
*/ | ||
function NodeWithChildren(type, children) { | ||
var _this = _super.call(this, type) || this; | ||
function NodeWithChildren(children) { | ||
var _this = _super.call(this) || this; | ||
_this.children = children; | ||
@@ -257,2 +253,19 @@ return _this; | ||
exports.NodeWithChildren = NodeWithChildren; | ||
var CDATA = /** @class */ (function (_super) { | ||
__extends(CDATA, _super); | ||
function CDATA() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.type = domelementtype_1.ElementType.CDATA; | ||
return _this; | ||
} | ||
Object.defineProperty(CDATA.prototype, "nodeType", { | ||
get: function () { | ||
return 4; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
return CDATA; | ||
}(NodeWithChildren)); | ||
exports.CDATA = CDATA; | ||
/** | ||
@@ -263,5 +276,14 @@ * The root node of the document. | ||
__extends(Document, _super); | ||
function Document(children) { | ||
return _super.call(this, domelementtype_1.ElementType.Root, children) || this; | ||
function Document() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.type = domelementtype_1.ElementType.Root; | ||
return _this; | ||
} | ||
Object.defineProperty(Document.prototype, "nodeType", { | ||
get: function () { | ||
return 9; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
return Document; | ||
@@ -287,7 +309,15 @@ }(NodeWithChildren)); | ||
: domelementtype_1.ElementType.Tag; } | ||
var _this = _super.call(this, type, children) || this; | ||
var _this = _super.call(this, children) || this; | ||
_this.name = name; | ||
_this.attribs = attribs; | ||
_this.type = type; | ||
return _this; | ||
} | ||
Object.defineProperty(Element.prototype, "nodeType", { | ||
get: function () { | ||
return 1; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Element.prototype, "tagName", { | ||
@@ -377,3 +407,3 @@ // DOM Level 1 aliases | ||
* @param node Node to check. | ||
* @returns `true` if the node is a `NodeWithChildren` (has children), `false` otherwise. | ||
* @returns `true` if the node has children, `false` otherwise. | ||
*/ | ||
@@ -416,3 +446,3 @@ function hasChildren(node) { | ||
var children = recursive ? cloneChildren(node.children) : []; | ||
var clone_2 = new NodeWithChildren(domelementtype_1.ElementType.CDATA, children); | ||
var clone_2 = new CDATA(children); | ||
children.forEach(function (child) { return (child.parent = clone_2); }); | ||
@@ -419,0 +449,0 @@ result = clone_2; |
{ | ||
"name": "domhandler", | ||
"version": "4.3.1", | ||
"version": "5.0.0", | ||
"description": "Handler for htmlparser2 that turns pages into a dom", | ||
@@ -12,2 +12,7 @@ "author": "Felix Boehm <me@feedic.com>", | ||
"types": "lib/index.d.ts", | ||
"module": "lib/esm/index.js", | ||
"exports": { | ||
"require": "./lib/index.js", | ||
"import": "./lib/esm/index.js" | ||
}, | ||
"sideEffects": false, | ||
@@ -22,3 +27,5 @@ "files": [ | ||
"format": "prettier --write '**/*.{ts,md,json}'", | ||
"build": "tsc", | ||
"build": "npm run build:cjs && npm run build:esm", | ||
"build:cjs": "tsc", | ||
"build:esm": "tsc --module esnext --target es2019 --outDir lib/esm && echo '{\"type\":\"module\"}' > lib/esm/package.json", | ||
"prepare": "npm run build" | ||
@@ -38,20 +45,23 @@ }, | ||
"dependencies": { | ||
"domelementtype": "^2.2.0" | ||
"domelementtype": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^27.4.1", | ||
"@types/node": "^17.0.21", | ||
"@typescript-eslint/eslint-plugin": "^5.15.0", | ||
"@typescript-eslint/parser": "^5.15.0", | ||
"eslint": "^8.11.0", | ||
"@types/node": "^17.0.23", | ||
"@typescript-eslint/eslint-plugin": "^5.17.0", | ||
"@typescript-eslint/parser": "^5.18.0", | ||
"eslint": "^8.12.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"htmlparser2": "^7.2.0", | ||
"jest": "^27.5.1", | ||
"prettier": "^2.6.0", | ||
"ts-jest": "^27.1.3", | ||
"typescript": "^4.6.2" | ||
"prettier": "^2.6.2", | ||
"ts-jest": "^27.1.4", | ||
"typescript": "^4.6.3" | ||
}, | ||
"jest": { | ||
"preset": "ts-jest", | ||
"testEnvironment": "node" | ||
"testEnvironment": "node", | ||
"moduleNameMapper": { | ||
"^(.*)\\.js$": "$1" | ||
} | ||
}, | ||
@@ -58,0 +68,0 @@ "prettier": { |
@@ -79,73 +79,2 @@ # domhandler [![Build Status](https://travis-ci.com/fb55/domhandler.svg?branch=master)](https://travis-ci.com/fb55/domhandler) | ||
## Option: `normalizeWhitespace` _(deprecated)_ | ||
Replace all whitespace with single spaces. | ||
The default value is `false`. | ||
**Note:** Enabling this might break your markup. | ||
For the following examples, this HTML will be used: | ||
```html | ||
<font> <br />this is the text <font></font></font> | ||
``` | ||
### Example: `normalizeWhitespace: true` | ||
```javascript | ||
[ | ||
{ | ||
type: "tag", | ||
name: "font", | ||
children: [ | ||
{ | ||
data: " ", | ||
type: "text", | ||
}, | ||
{ | ||
type: "tag", | ||
name: "br", | ||
}, | ||
{ | ||
data: "this is the text ", | ||
type: "text", | ||
}, | ||
{ | ||
type: "tag", | ||
name: "font", | ||
}, | ||
], | ||
}, | ||
]; | ||
``` | ||
### Example: `normalizeWhitespace: false` | ||
```javascript | ||
[ | ||
{ | ||
type: "tag", | ||
name: "font", | ||
children: [ | ||
{ | ||
data: "\n\t", | ||
type: "text", | ||
}, | ||
{ | ||
type: "tag", | ||
name: "br", | ||
}, | ||
{ | ||
data: "this is the text\n", | ||
type: "text", | ||
}, | ||
{ | ||
type: "tag", | ||
name: "font", | ||
}, | ||
], | ||
}, | ||
]; | ||
``` | ||
--- | ||
@@ -152,0 +81,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
74855
16
1762
93
Updateddomelementtype@^2.3.0