@@ -12,6 +12,8 @@ import {
1212
1313export * from "./node" ;
1414
15+ const reWhitespace = / \s + / g;
16+
1517export 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
4355const 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