• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class: ParserStream
2
3Streaming HTML parser with scripting support. A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
4
5*__example__*:
6
7```js
8const ParserStream = require('parse5-parser-stream');
9const http = require('http');
10
11// Fetch the page content and obtain it's <head> node
12http.get('http://inikulin.github.io/parse5/', res => {
13    const parser = new ParserStream();
14
15    parser.once('finish', () => {
16        console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
17    });
18
19    res.pipe(parser);
20});
21```
22
23### Constructors
24
25* [constructor](#constructor)
26
27### Properties
28
29* [document](#document)
30
31### Methods
32
33See: [writable stream API](https://nodejs.org/api/stream.html#stream_class_stream_writable).
34
35### Events
36
37* [on("script")](#on_script)
38
39Also see: [writable stream API](https://nodejs.org/api/stream.html#stream_class_stream_writable).
40
41---
42
43## Constructors
44
45<a id="constructor"></a>
46
47###  constructor
48
49⊕ **new ParserStream**(options?: *[ParserOptions](../../parse5/docs/options/parser-options.md)*): [ParserStream]()
50
51**Parameters:**
52
53| Param | Type | Description |
54| ------ | ------ | ------ |
55| `Optional` options | [ParserOptions](../../parse5/docs/options/parser-options.md) |  Parsing options. |
56
57**Returns:** [ParserStream]()
58
59___
60
61## Properties
62
63<a id="document"></a>
64
65###  document
66
67**● document**: *Document*
68
69The resulting document node.
70___
71
72## Events
73
74<a id="on_script"></a>
75
76###  on("script")
77
78▸ **on**(event: *"script"*, listener: *`function`*): `this`
79
80Raised when parser encounters a `<script>` element. If this event has listeners, parsing will be suspended once it is emitted. So, if `<script>` has the `src` attribute, you can fetch it, execute and then resume parsing just like browsers do.
81
82*__example__*:
83
84```js
85const ParserStream = require('parse5-parser-stream');
86const http = require('http');
87
88const parser = new ParserStream();
89
90parser.on('script', (scriptElement, documentWrite, resume) => {
91    const src = scriptElement.attrs.find(({ name }) => name === 'src').value;
92
93    http.get(src, res => {
94        // Fetch the script content, execute it with DOM built around `parser.document` and
95        // `document.write` implemented using `documentWrite`.
96        ...
97        // Then resume parsing.
98        resume();
99    });
100});
101
102parser.end('<script src="example.com/script.js"></script>');
103```
104
105**Parameters:**
106
107| Param | Type |
108| ------ | ------ |
109| event | "script" |
110| listener | `function` (see below) |
111
112**Returns:** `this`
113
114**listener:** (scriptElement: *Element*, documentWrite: *`function`*, resume: *`function`*): *`void`*
115
116| Param | Type | Description |
117| ------ | ------ | ------ |
118| scriptElement | Element |  The script element that caused the event. |
119| documentWrite | `function (html: string): void` |  Write additional html at the current parsing position. Suitable for implementing the DOM document.write and document.writeln methods. |
120| resume | `function` | Resumes parsing.
121
122___
123