domhandler
Advanced tools
Comparing version 4.2.1 to 4.2.2
@@ -21,2 +21,11 @@ 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. | ||
@@ -23,0 +32,0 @@ * |
@@ -17,4 +17,6 @@ "use strict"; | ||
__exportStar(require("./node"), exports); | ||
var reWhitespace = /\s+/g; | ||
// Default options | ||
var defaultOpts = { | ||
normalizeWhitespace: false, | ||
withStartIndices: false, | ||
@@ -95,5 +97,11 @@ withEndIndices: false, | ||
DomHandler.prototype.ontext = function (data) { | ||
var normalizeWhitespace = this.options.normalizeWhitespace; | ||
var lastNode = this.lastNode; | ||
if (lastNode && lastNode.type === domelementtype_1.ElementType.Text) { | ||
lastNode.data += data; | ||
if (normalizeWhitespace) { | ||
lastNode.data = (lastNode.data + data).replace(reWhitespace, " "); | ||
} | ||
else { | ||
lastNode.data += data; | ||
} | ||
if (this.options.withEndIndices) { | ||
@@ -104,2 +112,5 @@ lastNode.endIndex = this.parser.endIndex; | ||
else { | ||
if (normalizeWhitespace) { | ||
data = data.replace(reWhitespace, " "); | ||
} | ||
var node = new node_1.Text(data); | ||
@@ -106,0 +117,0 @@ this.addNode(node); |
{ | ||
"name": "domhandler", | ||
"version": "4.2.1", | ||
"version": "4.2.2", | ||
"description": "Handler for htmlparser2 that turns pages into a dom", | ||
@@ -5,0 +5,0 @@ "author": "Felix Boehm <me@feedic.com>", |
@@ -79,2 +79,73 @@ # 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", | ||
}, | ||
], | ||
}, | ||
]; | ||
``` | ||
--- | ||
@@ -81,0 +152,0 @@ |
Sorry, the diff of this file is not supported yet
40025
833
164