Skip to content

Commit

Permalink
Revert "refactor: Remove deprecated normalizeWhitespace option (#614)"
Browse files Browse the repository at this point in the history
This reverts commit 018ddf9.
  • Loading branch information
fb55 committed Aug 29, 2021
1 parent bd435d9 commit a841df3
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
71 changes: 71 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,77 @@ When the parser is used in a non-streaming fashion, `endIndex` is an integer
indicating the position of the end of the node in the document.
The default value is `false`.

## 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",
},
],
},
];
```

---

License: BSD-2-Clause
Expand Down
47 changes: 47 additions & 0 deletions src/__fixtures__/16-normalize_whitespace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "Normalize whitespace",
"options": {
"normalizeWhitespace": true
},
"html": "Line one\n<br>\t \r\n\f <br>\nline two<font><br> x </font>",
"expected": [
{
"data": "Line one ",
"type": "text"
},
{
"type": "tag",
"name": "br",
"attribs": {}
},
{
"data": " ",
"type": "text"
},
{
"type": "tag",
"name": "br",
"attribs": {}
},
{
"data": " line two",
"type": "text"
},
{
"type": "tag",
"name": "font",
"attribs": {},
"children": [
{
"type": "tag",
"name": "br",
"attribs": {}
},
{
"data": " x ",
"type": "text"
}
]
}
]
}
27 changes: 26 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {

export * from "./node";

const reWhitespace = /\s+/g;

export interface DomHandlerOptions {
/**
* Add a `startIndex` property to nodes.
Expand All @@ -31,6 +33,16 @@ export interface DomHandlerOptions {
*/
withEndIndices?: boolean;

/**
* Replace all whitespace with single spaces.
*
* **Note:** Enabling this might break your markup.
*
* @default false
* @deprecated
*/
normalizeWhitespace?: boolean;

/**
* Treat the markup as XML.
*
Expand All @@ -41,6 +53,7 @@ export interface DomHandlerOptions {

// Default options
const defaultOpts: DomHandlerOptions = {
normalizeWhitespace: false,
withStartIndices: false,
withEndIndices: false,
xmlMode: false,
Expand Down Expand Up @@ -153,14 +166,26 @@ export class DomHandler {
}

public ontext(data: string): void {
const { normalizeWhitespace } = this.options;
const { lastNode } = this;

if (lastNode && lastNode.type === ElementType.Text) {
lastNode.data += data;
if (normalizeWhitespace) {
lastNode.data = (lastNode.data + data).replace(
reWhitespace,
" "
);
} else {
lastNode.data += data;
}
if (this.options.withEndIndices) {
lastNode.endIndex = this.parser!.endIndex;
}
} else {
if (normalizeWhitespace) {
data = data.replace(reWhitespace, " ");
}

const node = new Text(data);
this.addNode(node);
this.lastNode = node;
Expand Down

0 comments on commit a841df3

Please sign in to comment.