• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class: SAXParser
2
3Streaming [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML parser. A [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform) (which means you can pipe _through_ it, see example).
4
5*__example__*:
6
7```js
8    const SAXParser = require('parse5-sax-parser');
9    const http = require('http');
10    const fs = require('fs');
11
12    const file = fs.createWriteStream('/home/google.com.html');
13    const parser = new SAXParser();
14
15    parser.on('text', text => {
16       // Handle page text content
17       ...
18    });
19
20    http.get('http://google.com', res => {
21       // SAXParser is the Transform stream, which means you can pipe
22       // through it. So, you can analyze page content and, e.g., save it
23       // to the file at the same time:
24       res.pipe(parser).pipe(file);
25    });
26```
27
28### Constructors
29
30* [constructor](#constructor)
31
32### Methods
33
34* [stop](#stop)
35
36See also: [transform stream API](https://nodejs.org/api/stream.html#stream_class_stream_transform).
37
38### Events
39
40* [on("startTag")](#on_startag)
41* [on("endTag")](#on_startag)
42* [on("comment")](#on_comment)
43* [on("text")](#on_text)
44* [on("doctype")](#on_doctype)
45
46See also: [transform stream API](https://nodejs.org/api/stream.html#stream_class_stream_transform).
47
48---
49
50## Constructors
51
52<a id="constructor"></a>
53
54###  constructor
55
56⊕ **new SAXParser**(options?: *[SAXParserOptions](sax-parser-options.md)*): [SAXParser]()
57
58**Parameters:**
59
60| Param | Type | Description |
61| ------ | ------ | ------ |
62| `Optional` options | [SAXParserOptions](sax-parser-options.md) |  Parsing options. |
63
64**Returns:** [SAXParser]()
65
66___
67
68
69## Methods
70
71
72<a id="stop"></a>
73
74###  stop
75
76▸ **stop**(): `void`
77
78Stops parsing. Useful if you want the parser to stop consuming CPU time once you've obtained the desired info from the input stream. Doesn't prevent piping, so that data will flow through the parser as usual.
79
80*__example__*:
81
82```js
83const SAXParser = require('parse5-sax-parser');
84const http = require('http');
85const fs = require('fs');
86
87const file = fs.createWriteStream('google.com.html');
88const parser = new SAXParser();
89
90parser.on('doctype', ({ name, publicId, systemId }) => {
91    // Process doctype info and stop parsing
92    ...
93    parser.stop();
94});
95
96http.get('http://google.com', res => {
97    // Despite the fact that parser.stop() was called whole
98    // content of the page will be written to the file
99    res.pipe(parser).pipe(file);
100});
101```
102
103**Returns:** `void`
104
105___
106
107## Events
108
109<a id="on_starttag"></a>
110
111###  on("startTag")
112
113▸ **on**(event: *"startTag"*, listener: *`function`*): `this`
114
115Raised when the parser encounters a start tag.
116
117**Parameters:**
118
119| Param | Type |
120| ------ | ------ |
121| event | "startTag" |
122| listener | function (startTag: *[StartTagToken](./tokens/start-tag.md)*) |
123
124**Returns:** `this`
125
126___
127<a id="on_endtag"></a>
128
129###  on("endTag")
130
131▸ **on**(event: *"endTag"*, listener: *`function`*): `this`
132
133Raised when parser encounters an end tag.
134
135**Parameters:**
136
137| Param | Type |
138| ------ | ------ |
139| event | "endTag" |
140| listener | function (endTag: *[EndTagToken](./tokens/end-tag.md)*) |
141
142**Returns:** `this`
143
144___
145<a id="on_comment"></a>
146
147###  on("comment")
148
149▸ **on**(event: *"comment"*, listener: *`function`*): `this`
150
151Raised when parser encounters a comment.
152
153**Parameters:**
154
155| Param | Type |
156| ------ | ------ |
157| event | "comment" |
158| listener | function (comment: *[CommentToken](./tokens/comment.md)*) |
159
160**Returns:** `this`
161
162___
163<a id="on_text"></a>
164
165###  on("text")
166
167▸ **on**(event: *"text"*, listener: *`function`*): `this`
168
169Raised when parser encounters text content.
170
171**Parameters:**
172
173| Param | Type |
174| ------ | ------ |
175| event | "text" |
176| listener | function (text: *[TextToken](./tokens/text.md)*)|
177
178**Returns:** `this`
179
180___
181<a id="on_doctype"></a>
182
183###  on("doctype")
184
185▸ **on**(event: *"doctype"*, listener: *`function`*): `this`
186
187Raised when parser encounters a [document type declaration](https://en.wikipedia.org/wiki/Document_type_declaration).
188
189**Parameters:**
190
191| Param | Type |
192| ------ | ------ |
193| event | "doctype" |
194| listener | function (doctype: *[DoctypeToken](./tokens/doctype.md)*) |
195
196**Returns:** `this`
197
198___
199