UNPKG

4.1 kBMarkdownView Raw
1# domhandler [![Build Status](https://travis-ci.com/fb55/domhandler.svg?branch=master)](https://travis-ci.com/fb55/domhandler)
2
3The DOM handler creates a tree containing all nodes of a page.
4The tree can be manipulated using the [domutils](https://github.com/fb55/domutils)
5or [cheerio](https://github.com/cheeriojs/cheerio) libraries and
6rendered using [dom-serializer](https://github.com/cheeriojs/dom-serializer) .
7
8## Usage
9
10```javascript
11const handler = new DomHandler([ <func> callback(err, dom), ] [ <obj> options ]);
12// const parser = new Parser(handler[, options]);
13```
14
15Available options are described below.
16
17## Example
18
19```javascript
20const { Parser } = require("htmlparser2");
21const { DomHandler } = require("domhandler");
22const rawHtml =
23 "Xyz <script language= javascript>var foo = '<<bar>>';</script><!--<!-- Waah! -- -->";
24const handler = new DomHandler((error, dom) => {
25 if (error) {
26 // Handle error
27 } else {
28 // Parsing completed, do something
29 console.log(dom);
30 }
31});
32const parser = new Parser(handler);
33parser.write(rawHtml);
34parser.end();
35```
36
37Output:
38
39```javascript
40[
41 {
42 data: "Xyz ",
43 type: "text",
44 },
45 {
46 type: "script",
47 name: "script",
48 attribs: {
49 language: "javascript",
50 },
51 children: [
52 {
53 data: "var foo = '<bar>';<",
54 type: "text",
55 },
56 ],
57 },
58 {
59 data: "<!-- Waah! -- ",
60 type: "comment",
61 },
62];
63```
64
65## Option: `withStartIndices`
66
67Add a `startIndex` property to nodes.
68When the parser is used in a non-streaming fashion, `startIndex` is an integer
69indicating the position of the start of the node in the document.
70The default value is `false`.
71
72## Option: `withEndIndices`
73
74Add an `endIndex` property to nodes.
75When the parser is used in a non-streaming fashion, `endIndex` is an integer
76indicating the position of the end of the node in the document.
77The default value is `false`.
78
79## Option: `normalizeWhitespace` _(deprecated)_
80
81Replace all whitespace with single spaces.
82The default value is `false`.
83
84**Note:** Enabling this might break your markup.
85
86For 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
150---
151
152License: BSD-2-Clause
153
154## Security contact information
155
156To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
157Tidelift will coordinate the fix and disclosure.
158
159## `domhandler` for enterprise
160
161Available as part of the Tidelift Subscription
162
163The maintainers of `domhandler` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-domhandler?utm_source=npm-domhandler&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)