/
index.ts
147 lines (131 loc) · 4.97 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Writable } from 'node:stream';
import { Parser, type ParserOptions, type TreeAdapterTypeMap, type DefaultTreeAdapterMap } from 'parse5';
/* eslint-disable unicorn/consistent-function-scoping -- The rule seems to be broken here. */
/**
* Streaming HTML parser with scripting support.
* A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
*
* @example
*
* ```js
* const ParserStream = require('parse5-parser-stream');
* const http = require('http');
* const { finished } = require('node:stream');
*
* // Fetch the page content and obtain it's <head> node
* http.get('http://inikulin.github.io/parse5/', res => {
* const parser = new ParserStream();
*
* finished(parser, () => {
* console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
* });
*
* res.pipe(parser);
* });
* ```
*
*/
export class ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> extends Writable {
static getFragmentStream<T extends TreeAdapterTypeMap>(
fragmentContext?: T['parentNode'] | null,
options?: ParserOptions<T>
): ParserStream<T> {
const parser = Parser.getFragmentParser(fragmentContext, options);
const stream = new ParserStream(options, parser);
return stream;
}
private lastChunkWritten = false;
private writeCallback: undefined | (() => void) = undefined;
private pendingHtmlInsertions: string[] = [];
/** The resulting document node. */
public get document(): T['document'] {
return this.parser.document;
}
public getFragment(): T['documentFragment'] {
return this.parser.getFragment();
}
/**
* @param options Parsing options.
*/
constructor(options?: ParserOptions<T>, public parser: Parser<T> = new Parser(options)) {
super({ decodeStrings: false });
const resume = (): void => {
for (let i = this.pendingHtmlInsertions.length - 1; i >= 0; i--) {
this.parser.tokenizer.insertHtmlAtCurrentPos(this.pendingHtmlInsertions[i]);
}
this.pendingHtmlInsertions.length = 0;
//NOTE: keep parsing if we don't wait for the next input chunk
this.parser.tokenizer.resume(this.writeCallback);
};
const documentWrite = (html: string): void => {
if (!this.parser.stopped) {
this.pendingHtmlInsertions.push(html);
}
};
const scriptHandler = (scriptElement: T['element']): void => {
if (this.listenerCount('script') > 0) {
this.parser.tokenizer.pause();
this.emit('script', scriptElement, documentWrite, resume);
}
};
this.parser.scriptHandler = scriptHandler;
}
//WritableStream implementation
override _write(chunk: string, _encoding: string, callback: () => void): void {
if (typeof chunk !== 'string') {
throw new TypeError('Parser can work only with string streams.');
}
this.writeCallback = callback;
this.parser.tokenizer.write(chunk, this.lastChunkWritten, this.writeCallback);
}
// TODO [engine:node@>=16]: Due to issues with Node < 16, we are overriding `end` instead of `_final`.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
override end(chunk?: any, encoding?: any, callback?: any): any {
this.lastChunkWritten = true;
super.end(chunk || '', encoding, callback);
}
}
export interface ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> {
/**
* Raised 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.
*
* @example
*
* ```js
* const ParserStream = require('parse5-parser-stream');
* const http = require('http');
*
* const parser = new ParserStream();
*
* parser.on('script', (scriptElement, documentWrite, resume) => {
* const src = scriptElement.attrs.find(({ name }) => name === 'src').value;
*
* http.get(src, res => {
* // Fetch the script content, execute it with DOM built around `parser.document` and
* // `document.write` implemented using `documentWrite`.
* ...
* // Then resume parsing.
* resume();
* });
* });
*
* parser.end('<script src="example.com/script.js"></script>');
* ```
*
* @param event Name of the event
* @param handler
*/
on(
event: 'script',
handler: (scriptElement: T['element'], documentWrite: (html: string) => void, resume: () => void) => void
): void;
/**
* Base event handler.
*
* @param event Name of the event
* @param handler Event handler
*/
on(event: string, handler: (...args: any[]) => void): this;
}