Skip to content

Commit a841df3

Browse files
committed
Revert "refactor: Remove deprecated normalizeWhitespace option (#614)"
This reverts commit 018ddf9.
1 parent bd435d9 commit a841df3

3 files changed

Lines changed: 144 additions & 1 deletion

File tree

readme.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,77 @@ When the parser is used in a non-streaming fashion, `endIndex` is an integer
7676
indicating the position of the end of the node in the document.
7777
The default value is `false`.
7878

79+
## Option: `normalizeWhitespace` _(deprecated)_
80+
81+
Replace all whitespace with single spaces.
82+
The default value is `false`.
83+
84+
**Note:** Enabling this might break your markup.
85+
86+
For the following examples, this HTML will be used:
87+
88+
```html
89+
<font> <br />this is the text <font></font></font>
90+
```
91+
92+
### Example: `normalizeWhitespace: true`
93+
94+
```javascript
95+
[
96+
{
97+
type: "tag",
98+
name: "font",
99+
children: [
100+
{
101+
data: " ",
102+
type: "text",
103+
},
104+
{
105+
type: "tag",
106+
name: "br",
107+
},
108+
{
109+
data: "this is the text ",
110+
type: "text",
111+
},
112+
{
113+
type: "tag",
114+
name: "font",
115+
},
116+
],
117+
},
118+
];
119+
```
120+
121+
### Example: `normalizeWhitespace: false`
122+
123+
```javascript
124+
[
125+
{
126+
type: "tag",
127+
name: "font",
128+
children: [
129+
{
130+
data: "\n\t",
131+
type: "text",
132+
},
133+
{
134+
type: "tag",
135+
name: "br",
136+
},
137+
{
138+
data: "this is the text\n",
139+
type: "text",
140+
},
141+
{
142+
type: "tag",
143+
name: "font",
144+
},
145+
],
146+
},
147+
];
148+
```
149+
79150
---
80151

81152
License: BSD-2-Clause
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "Normalize whitespace",
3+
"options": {
4+
"normalizeWhitespace": true
5+
},
6+
"html": "Line one\n<br>\t \r\n\f <br>\nline two<font><br> x </font>",
7+
"expected": [
8+
{
9+
"data": "Line one ",
10+
"type": "text"
11+
},
12+
{
13+
"type": "tag",
14+
"name": "br",
15+
"attribs": {}
16+
},
17+
{
18+
"data": " ",
19+
"type": "text"
20+
},
21+
{
22+
"type": "tag",
23+
"name": "br",
24+
"attribs": {}
25+
},
26+
{
27+
"data": " line two",
28+
"type": "text"
29+
},
30+
{
31+
"type": "tag",
32+
"name": "font",
33+
"attribs": {},
34+
"children": [
35+
{
36+
"type": "tag",
37+
"name": "br",
38+
"attribs": {}
39+
},
40+
{
41+
"data": " x ",
42+
"type": "text"
43+
}
44+
]
45+
}
46+
]
47+
}

src/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212

1313
export * from "./node";
1414

15+
const reWhitespace = /\s+/g;
16+
1517
export interface DomHandlerOptions {
1618
/**
1719
* Add a `startIndex` property to nodes.
@@ -31,6 +33,16 @@ export interface DomHandlerOptions {
3133
*/
3234
withEndIndices?: boolean;
3335

36+
/**
37+
* Replace all whitespace with single spaces.
38+
*
39+
* **Note:** Enabling this might break your markup.
40+
*
41+
* @default false
42+
* @deprecated
43+
*/
44+
normalizeWhitespace?: boolean;
45+
3446
/**
3547
* Treat the markup as XML.
3648
*
@@ -41,6 +53,7 @@ export interface DomHandlerOptions {
4153

4254
// Default options
4355
const defaultOpts: DomHandlerOptions = {
56+
normalizeWhitespace: false,
4457
withStartIndices: false,
4558
withEndIndices: false,
4659
xmlMode: false,
@@ -153,14 +166,26 @@ export class DomHandler {
153166
}
154167

155168
public ontext(data: string): void {
169+
const { normalizeWhitespace } = this.options;
156170
const { lastNode } = this;
157171

158172
if (lastNode && lastNode.type === ElementType.Text) {
159-
lastNode.data += data;
173+
if (normalizeWhitespace) {
174+
lastNode.data = (lastNode.data + data).replace(
175+
reWhitespace,
176+
" "
177+
);
178+
} else {
179+
lastNode.data += data;
180+
}
160181
if (this.options.withEndIndices) {
161182
lastNode.endIndex = this.parser!.endIndex;
162183
}
163184
} else {
185+
if (normalizeWhitespace) {
186+
data = data.replace(reWhitespace, " ");
187+
}
188+
164189
const node = new Text(data);
165190
this.addNode(node);
166191
this.lastNode = node;

0 commit comments

Comments
 (0)