• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class: RewritingStream
2
3Streaming [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML rewriter. A [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform) (which means you can pipe _through_ it, see example).
4Rewriter uses raw source representation of tokens if they are not modified by the user, therefore resulting
5HTML is not affected by parser error-recovery mechanisms as in the classical parsing-serialization roundtrip.
6
7*__example__*:
8
9```js
10    const RewritingStream = require('parse5-html-rewriting-stream');
11    const http = require('http');
12    const fs = require('fs');
13
14    const file = fs.createWriteStream('/home/google.com.html');
15    const rewriter = new RewritingStream();
16
17    // Replace divs with spans
18    rewriter.on('startTag', startTag => {
19        if (startTag.tagName === 'span') {
20            startTag.tagName = 'div';
21        }
22
23        rewriter.emitStartTag(startTag);
24    });
25
26    rewriter.on('endTag', endTag => {
27        if (endTag.tagName === 'span') {
28            endTag.tagName = 'div';
29        }
30
31        rewriter.emitEndTag(endTag);
32    });
33
34    // Wrap all text nodes with <i> tag
35    rewriter.on('text', (_, raw) => {
36        // Use raw representation of text without HTML entities decoding
37        rewriter.emitRaw(`<i>${raw}</i>`);
38    });
39
40    http.get('http://google.com', res => {
41       // Assumes response is UTF-8.
42       res.setEncoding('utf8');
43       // RewritingStream is the Transform stream, which means you can pipe
44       // through it.
45       res.pipe(rewriter).pipe(file);
46    });
47```
48
49### Constructors
50
51* [constructor](#constructor)
52
53### Methods
54
55* [emitStartTag](#emit_start_tag)
56* [emitEndTag](#emit_end_tag)
57* [emitText](#emit_text)
58* [emitComment](#emit_comment)
59* [emitDoctype](#emit_doctype)
60* [emitRaw](#emit_raw)
61
62See also: [transform stream API](https://nodejs.org/api/stream.html#stream_class_stream_transform).
63
64### Events
65
66* [on("startTag")](#on_startag)
67* [on("endTag")](#on_startag)
68* [on("comment")](#on_comment)
69* [on("text")](#on_text)
70* [on("doctype")](#on_doctype)
71
72See also: [transform stream API](https://nodejs.org/api/stream.html#stream_class_stream_transform).
73
74---
75
76## Constructors
77
78<a id="constructor"></a>
79
80###  constructor
81
82⊕ **new RewritingStream**(): [RewritingStream]()
83
84**Note:** [sourceCodeLocationInfo](../../parse5-sax-parser/docs/sax-parser-options.md#locationinfo) option is
85always enabled for the [RewritingStream]().
86
87**Returns:** [RewritingStream]()
88
89___
90
91
92## Methods
93
94<a id="emit_start_tag"></a>
95
96###  emitStartTag
97
98▸ **emitStartTag**(startTag: *[StartTagToken](../../parse5-sax-parser/docs/tokens/start-tag.md)*): `void`
99
100Emits serialized start tag token into the output stream.
101
102**Returns:** `void`
103
104___
105<a id="emit_end_tag"></a>
106
107###  emitEndTag
108
109▸ **emitEndTag**(endTag: *[EndTagToken](../../parse5-sax-parser/docs/tokens/end-tag.md)*): `void`
110
111Emits serialized end tag token into the output stream.
112
113**Returns:** `void`
114
115___
116<a id="emit_text"></a>
117
118###  emitText
119
120▸ **emitText**(text: *[TextToken](../../parse5-sax-parser/docs/tokens/text.md)*): `void`
121
122Emits serialized text token into the output stream.
123
124**Returns:** `void`
125
126___
127<a id="emit_comment"></a>
128
129###  emitComment
130
131▸ **emitComment**(comment: *[CommentToken](../../parse5-sax-parser/docs/tokens/comment.md)*): `void`
132
133Emits serialized comment token into the output stream.
134
135**Returns:** `void`
136
137___
138<a id="emit_doctype"></a>
139
140###  emitDoctype
141
142▸ **emitDoctype**(doctype: *[DoctypeToken](../../parse5-sax-parser/docs/tokens/doctype.md)*): `void`
143
144Emits serialized document type token into the output stream.
145
146**Returns:** `void`
147
148___
149<a id="emit_raw"></a>
150
151###  emitRaw
152
153▸ **emitRaw**(html: *String*): `void`
154
155Emits raw HTML string into the output stream.
156
157**Returns:** `void`
158
159___
160
161## Events
162
163**Note:** Each event listener receives raw HTML string representation of a token as its second
164argument, which later can be emitted with the [emitRaw](#emit_raw) method. Thus, token will not be
165re-serialized and will have the same markup as in the source HTML.
166
167**Note:** If an event has an event handler attached, you need to emit token manually, otherwise
168it will not get into the output stream.
169
170<a id="on_starttag"></a>
171
172###  on("startTag")
173
174▸ **on**(event: *"startTag"*, listener: *`function`*): `this`
175
176Raised when the rewriter encounters a start tag.
177
178**Parameters:**
179
180| Param | Type |
181| ------ | ------ |
182| event | "startTag" |
183| listener | function (startTag: *[StartTagToken](../../parse5-sax-parser/docs/tokens/start-tag.md)*, rawHtml: *String*) |
184
185**Returns:** `this`
186
187___
188<a id="on_endtag"></a>
189
190###  on("endTag")
191
192▸ **on**(event: *"endTag"*, listener: *`function`*): `this`
193
194Raised when rewriter encounters an end tag.
195
196**Parameters:**
197
198| Param | Type |
199| ------ | ------ |
200| event | "endTag" |
201| listener | function (endTag: *[EndTagToken](../../parse5-sax-parser/docs/tokens/end-tag.md)*, rawHtml: *String*) |
202
203**Returns:** `this`
204
205___
206<a id="on_comment"></a>
207
208###  on("comment")
209
210▸ **on**(event: *"comment"*, listener: *`function`*): `this`
211
212Raised when rewriter encounters a comment.
213
214**Parameters:**
215
216| Param | Type |
217| ------ | ------ |
218| event | "comment" |
219| listener | function (comment: *[CommentToken](../../parse5-sax-parser/docs/tokens/comment.md)*, rawHtml: *String*) |
220
221**Returns:** `this`
222
223___
224<a id="on_text"></a>
225
226###  on("text")
227
228▸ **on**(event: *"text"*, listener: *`function`*): `this`
229
230Raised when rewriter encounters text content.
231
232**Parameters:**
233
234| Param | Type |
235| ------ | ------ |
236| event | "text" |
237| listener | function (text: *[TextToken](../../parse5-sax-parser/docs/tokens/text.md)*, rawHtml: *String*)|
238
239**Returns:** `this`
240
241___
242<a id="on_doctype"></a>
243
244###  on("doctype")
245
246▸ **on**(event: *"doctype"*, listener: *`function`*): `this`
247
248Raised when rewriter encounters a [document type declaration](https://en.wikipedia.org/wiki/Document_type_declaration).
249
250**Parameters:**
251
252| Param | Type |
253| ------ | ------ |
254| event | "doctype" |
255| listener | function (doctype: *[DoctypeToken](../../parse5-sax-parser/docs/tokens/doctype.md)*, rawHtml: *String*) |
256
257**Returns:** `this`
258
259___
260