domhandler
Advanced tools
Comparing version 4.2.0 to 4.2.1
@@ -21,11 +21,2 @@ import { Node, Element, DataNode, NodeWithChildren, Document } from "./node"; | ||
/** | ||
* Replace all whitespace with single spaces. | ||
* | ||
* **Note:** Enabling this might break your markup. | ||
* | ||
* @default false | ||
* @deprecated | ||
*/ | ||
normalizeWhitespace?: boolean; | ||
/** | ||
* Treat the markup as XML. | ||
@@ -32,0 +23,0 @@ * |
@@ -17,8 +17,7 @@ "use strict"; | ||
__exportStar(require("./node"), exports); | ||
var reWhitespace = /\s+/g; | ||
// Default options | ||
var defaultOpts = { | ||
normalizeWhitespace: false, | ||
withStartIndices: false, | ||
withEndIndices: false, | ||
xmlMode: false, | ||
}; | ||
@@ -62,3 +61,2 @@ var DomHandler = /** @class */ (function () { | ||
DomHandler.prototype.onreset = function () { | ||
var _a; | ||
this.dom = []; | ||
@@ -69,3 +67,3 @@ this.root = new node_1.Document(this.dom); | ||
this.lastNode = null; | ||
this.parser = (_a = this.parser) !== null && _a !== void 0 ? _a : null; | ||
this.parser = null; | ||
}; | ||
@@ -99,16 +97,10 @@ // Signals the handler that parsing is done | ||
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, " "); | ||
lastNode.data += data; | ||
if (this.options.withEndIndices) { | ||
lastNode.endIndex = this.parser.endIndex; | ||
} | ||
else { | ||
lastNode.data += data; | ||
} | ||
} | ||
else { | ||
if (normalizeWhitespace) { | ||
data = data.replace(reWhitespace, " "); | ||
} | ||
var node = new node_1.Text(data); | ||
@@ -115,0 +107,0 @@ this.addNode(node); |
@@ -38,2 +38,5 @@ import { ElementType } from "domelementtype"; | ||
} | ||
/** | ||
* A node that contains some data. | ||
*/ | ||
export declare class DataNode extends Node { | ||
@@ -49,8 +52,17 @@ data: string; | ||
} | ||
/** | ||
* Text within the document. | ||
*/ | ||
export declare class Text extends DataNode { | ||
constructor(data: string); | ||
} | ||
/** | ||
* Comments within the document. | ||
*/ | ||
export declare class Comment extends DataNode { | ||
constructor(data: string); | ||
} | ||
/** | ||
* Processing instructions, including doc types. | ||
*/ | ||
export declare class ProcessingInstruction extends DataNode { | ||
@@ -78,2 +90,5 @@ name: string; | ||
} | ||
/** | ||
* The root node of the document. | ||
*/ | ||
export declare class Document extends NodeWithChildren { | ||
@@ -83,2 +98,5 @@ constructor(children: Node[]); | ||
} | ||
/** | ||
* The description of an individual attribute. | ||
*/ | ||
interface Attribute { | ||
@@ -90,2 +108,5 @@ name: string; | ||
} | ||
/** | ||
* An element within the DOM. | ||
*/ | ||
export declare class Element extends NodeWithChildren { | ||
@@ -124,3 +145,3 @@ name: string; | ||
*/ | ||
export declare function isText(node: Node): node is DataNode; | ||
export declare function isText(node: Node): node is Text; | ||
/** | ||
@@ -127,0 +148,0 @@ * @param node Node to check. |
@@ -116,2 +116,5 @@ "use strict"; | ||
exports.Node = Node; | ||
/** | ||
* A node that contains some data. | ||
*/ | ||
var DataNode = /** @class */ (function (_super) { | ||
@@ -141,2 +144,5 @@ __extends(DataNode, _super); | ||
exports.DataNode = DataNode; | ||
/** | ||
* Text within the document. | ||
*/ | ||
var Text = /** @class */ (function (_super) { | ||
@@ -150,2 +156,5 @@ __extends(Text, _super); | ||
exports.Text = Text; | ||
/** | ||
* Comments within the document. | ||
*/ | ||
var Comment = /** @class */ (function (_super) { | ||
@@ -159,2 +168,5 @@ __extends(Comment, _super); | ||
exports.Comment = Comment; | ||
/** | ||
* Processing instructions, including doc types. | ||
*/ | ||
var ProcessingInstruction = /** @class */ (function (_super) { | ||
@@ -215,2 +227,5 @@ __extends(ProcessingInstruction, _super); | ||
exports.NodeWithChildren = NodeWithChildren; | ||
/** | ||
* The root node of the document. | ||
*/ | ||
var Document = /** @class */ (function (_super) { | ||
@@ -224,2 +239,5 @@ __extends(Document, _super); | ||
exports.Document = Document; | ||
/** | ||
* An element within the DOM. | ||
*/ | ||
var Element = /** @class */ (function (_super) { | ||
@@ -279,3 +297,3 @@ __extends(Element, _super); | ||
function isTag(node) { | ||
return domelementtype_1.isTag(node); | ||
return (0, domelementtype_1.isTag)(node); | ||
} | ||
@@ -282,0 +300,0 @@ exports.isTag = isTag; |
{ | ||
"name": "domhandler", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"description": "Handler for htmlparser2 that turns pages into a dom", | ||
@@ -17,4 +17,4 @@ "author": "Felix Boehm <me@feedic.com>", | ||
"scripts": { | ||
"test": "jest --coverage && npm run lint", | ||
"coverage": "cat coverage/lcov.info | coveralls", | ||
"test": "npm run test:jest && npm run lint", | ||
"test:jest": "jest", | ||
"lint": "eslint src", | ||
@@ -40,14 +40,13 @@ "format": "prettier --write '**/*.{ts,md,json}'", | ||
"devDependencies": { | ||
"@types/jest": "^26.0.0", | ||
"@types/node": "^14.0.9", | ||
"@typescript-eslint/eslint-plugin": "^4.1.0", | ||
"@typescript-eslint/parser": "^4.1.0", | ||
"coveralls": "^3.0.5", | ||
"eslint": "^7.9.0", | ||
"@types/jest": "^27.0.1", | ||
"@types/node": "^16.7.2", | ||
"@typescript-eslint/eslint-plugin": "^4.29.3", | ||
"@typescript-eslint/parser": "^4.29.3", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"htmlparser2": "^6.0.0", | ||
"jest": "^26.0.1", | ||
"htmlparser2": "^7.1.1", | ||
"jest": "^27.1.0", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^26.1.0", | ||
"typescript": "^4.0.2" | ||
"ts-jest": "^27.0.5", | ||
"typescript": "^4.4.2" | ||
}, | ||
@@ -54,0 +53,0 @@ "jest": { |
@@ -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
11
813
38029
93