README.md
1# domhandler [![Build Status](https://travis-ci.org/fb55/domhandler.svg?branch=master)](https://travis-ci.org/fb55/domhandler)
2
3The DOM handler creates a tree containing all nodes of a page.
4The tree may be manipulated using the [domutils](https://github.com/fb55/domutils) or [cheerio](https://github.com/cheeriojs/cheerio) libraries.
5
6## Usage
7
8```javascript
9const handler = new DomHandler([ <func> callback(err, dom), ] [ <obj> options ]);
10// const parser = new Parser(handler[, options]);
11```
12
13Available options are described below.
14
15## Example
16
17```javascript
18const { Parser } = require("htmlparser2");
19const { DomHandler } = require("domhandler");
20const rawHtml =
21 "Xyz <script language= javascript>var foo = '<<bar>>';< / script><!--<!-- Waah! -- -->";
22const handler = new htmlparser.DomHandler(function(error, dom) {
23 if (error) {
24 // Handle error
25 } else {
26 // Parsing completed, do something
27 console.log(dom);
28 }
29});
30const parser = new Parser(handler);
31parser.write(rawHtml);
32parser.end();
33```
34
35Output:
36
37```javascript
38[
39 {
40 data: "Xyz ",
41 type: "text"
42 },
43 {
44 type: "script",
45 name: "script",
46 attribs: {
47 language: "javascript"
48 },
49 children: [
50 {
51 data: "const foo = '<bar>';<",
52 type: "text"
53 }
54 ]
55 },
56 {
57 data: "<!-- Waah! -- ",
58 type: "comment"
59 }
60];
61```
62
63## Option: normalizeWhitespace
64
65Indicates whether the whitespace in text nodes should be normalized (= all whitespace should be replaced with single spaces).
66The default value is `false`.
67
68For the following examples, this HTML will be used:
69
70```html
71<font> <br />this is the text <font></font></font>
72```
73
74### Example: `normalizeWhitespace: true`
75
76```javascript
77[
78 {
79 type: "tag",
80 name: "font",
81 children: [
82 {
83 data: " ",
84 type: "text"
85 },
86 {
87 type: "tag",
88 name: "br"
89 },
90 {
91 data: "this is the text ",
92 type: "text"
93 },
94 {
95 type: "tag",
96 name: "font"
97 }
98 ]
99 }
100];
101```
102
103### Example: `normalizeWhitespace: false`
104
105```javascript
106[
107 {
108 type: "tag",
109 name: "font",
110 children: [
111 {
112 data: "\n\t",
113 type: "text"
114 },
115 {
116 type: "tag",
117 name: "br"
118 },
119 {
120 data: "this is the text\n",
121 type: "text"
122 },
123 {
124 type: "tag",
125 name: "font"
126 }
127 ]
128 }
129];
130```
131
132## Option: withStartIndices
133
134Indicates whether a `startIndex` property will be added to nodes.
135When the parser is used in a non-streaming fashion, `startIndex` is an integer indicating the position of the start of the node in the document.
136The default value is `false`.
137
138## Option: withEndIndices
139
140Indicates whether a `endIndex` property will be added to nodes.
141When 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.
142The default value is `false`.
143
144---
145
146License: BSD-2-Clause
147
148[Get supported domhandler with the Tidelift Subscription](https://tidelift.com/subscription/pkg/npm-domhandler?utm_source=npm-domhandler&utm_medium=referral&utm_campaign=readme)
149
150## Security contact information
151
152To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
153Tidelift will coordinate the fix and disclosure.
154