parse5-parser-stream 6.0.1 → 7.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -3
- package/dist/cjs/index.d.ts +85 -0
- package/dist/cjs/index.js +90 -0
- package/dist/cjs/package.json +1 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +86 -0
- package/package.json +18 -6
- package/lib/index.js +0 -83
package/README.md
CHANGED
|
@@ -15,9 +15,8 @@
|
|
|
15
15
|
</div>
|
|
16
16
|
<br>
|
|
17
17
|
|
|
18
|
-
|
|
19
18
|
<p align="center">
|
|
20
|
-
📖 <a href="https://
|
|
19
|
+
📖 <a href="https://parse5.js.org/modules/parse5_parser_stream.html"><b>Documentation</b></a> 📖
|
|
21
20
|
</p>
|
|
22
21
|
|
|
23
22
|
---
|
|
@@ -31,5 +30,5 @@
|
|
|
31
30
|
</p>
|
|
32
31
|
|
|
33
32
|
<p align="center">
|
|
34
|
-
<a href="https://github.com/inikulin/parse5/
|
|
33
|
+
<a href="https://github.com/inikulin/parse5/releases">Changelog</a>
|
|
35
34
|
</p>
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Writable } from 'node:stream';
|
|
3
|
+
import { Parser, type ParserOptions, type TreeAdapterTypeMap, type DefaultTreeAdapterMap } from 'parse5';
|
|
4
|
+
/**
|
|
5
|
+
* Streaming HTML parser with scripting support.
|
|
6
|
+
* A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```js
|
|
11
|
+
* const ParserStream = require('parse5-parser-stream');
|
|
12
|
+
* const http = require('http');
|
|
13
|
+
* const { finished } = require('node:stream');
|
|
14
|
+
*
|
|
15
|
+
* // Fetch the page content and obtain it's <head> node
|
|
16
|
+
* http.get('http://inikulin.github.io/parse5/', res => {
|
|
17
|
+
* const parser = new ParserStream();
|
|
18
|
+
*
|
|
19
|
+
* finished(parser, () => {
|
|
20
|
+
* console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* res.pipe(parser);
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
export declare class ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> extends Writable {
|
|
29
|
+
parser: Parser<T>;
|
|
30
|
+
static getFragmentStream<T extends TreeAdapterTypeMap>(fragmentContext?: T['parentNode'] | null, options?: ParserOptions<T>): ParserStream<T>;
|
|
31
|
+
private lastChunkWritten;
|
|
32
|
+
private writeCallback;
|
|
33
|
+
private pendingHtmlInsertions;
|
|
34
|
+
/** The resulting document node. */
|
|
35
|
+
get document(): T['document'];
|
|
36
|
+
getFragment(): T['documentFragment'];
|
|
37
|
+
/**
|
|
38
|
+
* @param options Parsing options.
|
|
39
|
+
*/
|
|
40
|
+
constructor(options?: ParserOptions<T>, parser?: Parser<T>);
|
|
41
|
+
_write(chunk: string, _encoding: string, callback: () => void): void;
|
|
42
|
+
end(chunk?: any, encoding?: any, callback?: any): any;
|
|
43
|
+
}
|
|
44
|
+
export interface ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> {
|
|
45
|
+
/**
|
|
46
|
+
* Raised when parser encounters a `<script>` element. If this event has listeners, parsing will be suspended once
|
|
47
|
+
* it is emitted. So, if `<script>` has the `src` attribute, you can fetch it, execute and then resume parsing just
|
|
48
|
+
* like browsers do.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
*
|
|
52
|
+
* ```js
|
|
53
|
+
* const ParserStream = require('parse5-parser-stream');
|
|
54
|
+
* const http = require('http');
|
|
55
|
+
*
|
|
56
|
+
* const parser = new ParserStream();
|
|
57
|
+
*
|
|
58
|
+
* parser.on('script', (scriptElement, documentWrite, resume) => {
|
|
59
|
+
* const src = scriptElement.attrs.find(({ name }) => name === 'src').value;
|
|
60
|
+
*
|
|
61
|
+
* http.get(src, res => {
|
|
62
|
+
* // Fetch the script content, execute it with DOM built around `parser.document` and
|
|
63
|
+
* // `document.write` implemented using `documentWrite`.
|
|
64
|
+
* ...
|
|
65
|
+
* // Then resume parsing.
|
|
66
|
+
* resume();
|
|
67
|
+
* });
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* parser.end('<script src="example.com/script.js"></script>');
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @param event Name of the event
|
|
74
|
+
* @param handler
|
|
75
|
+
*/
|
|
76
|
+
on(event: 'script', handler: (scriptElement: T['element'], documentWrite: (html: string) => void, resume: () => void) => void): void;
|
|
77
|
+
/**
|
|
78
|
+
* Base event handler.
|
|
79
|
+
*
|
|
80
|
+
* @param event Name of the event
|
|
81
|
+
* @param handler Event handler
|
|
82
|
+
*/
|
|
83
|
+
on(event: string, handler: (...args: any[]) => void): this;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ParserStream = void 0;
|
|
4
|
+
const node_stream_1 = require("node:stream");
|
|
5
|
+
const parse5_1 = require("parse5");
|
|
6
|
+
/* eslint-disable unicorn/consistent-function-scoping -- The rule seems to be broken here. */
|
|
7
|
+
/**
|
|
8
|
+
* Streaming HTML parser with scripting support.
|
|
9
|
+
* A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```js
|
|
14
|
+
* const ParserStream = require('parse5-parser-stream');
|
|
15
|
+
* const http = require('http');
|
|
16
|
+
* const { finished } = require('node:stream');
|
|
17
|
+
*
|
|
18
|
+
* // Fetch the page content and obtain it's <head> node
|
|
19
|
+
* http.get('http://inikulin.github.io/parse5/', res => {
|
|
20
|
+
* const parser = new ParserStream();
|
|
21
|
+
*
|
|
22
|
+
* finished(parser, () => {
|
|
23
|
+
* console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* res.pipe(parser);
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
class ParserStream extends node_stream_1.Writable {
|
|
32
|
+
static getFragmentStream(fragmentContext, options) {
|
|
33
|
+
const parser = parse5_1.Parser.getFragmentParser(fragmentContext, options);
|
|
34
|
+
const stream = new ParserStream(options, parser);
|
|
35
|
+
return stream;
|
|
36
|
+
}
|
|
37
|
+
/** The resulting document node. */
|
|
38
|
+
get document() {
|
|
39
|
+
return this.parser.document;
|
|
40
|
+
}
|
|
41
|
+
getFragment() {
|
|
42
|
+
return this.parser.getFragment();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* @param options Parsing options.
|
|
46
|
+
*/
|
|
47
|
+
constructor(options, parser = new parse5_1.Parser(options)) {
|
|
48
|
+
super({ decodeStrings: false });
|
|
49
|
+
this.parser = parser;
|
|
50
|
+
this.lastChunkWritten = false;
|
|
51
|
+
this.writeCallback = undefined;
|
|
52
|
+
this.pendingHtmlInsertions = [];
|
|
53
|
+
const resume = () => {
|
|
54
|
+
for (let i = this.pendingHtmlInsertions.length - 1; i >= 0; i--) {
|
|
55
|
+
this.parser.tokenizer.insertHtmlAtCurrentPos(this.pendingHtmlInsertions[i]);
|
|
56
|
+
}
|
|
57
|
+
this.pendingHtmlInsertions.length = 0;
|
|
58
|
+
//NOTE: keep parsing if we don't wait for the next input chunk
|
|
59
|
+
this.parser.tokenizer.resume(this.writeCallback);
|
|
60
|
+
};
|
|
61
|
+
const documentWrite = (html) => {
|
|
62
|
+
if (!this.parser.stopped) {
|
|
63
|
+
this.pendingHtmlInsertions.push(html);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const scriptHandler = (scriptElement) => {
|
|
67
|
+
if (this.listenerCount('script') > 0) {
|
|
68
|
+
this.parser.tokenizer.pause();
|
|
69
|
+
this.emit('script', scriptElement, documentWrite, resume);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
this.parser.scriptHandler = scriptHandler;
|
|
73
|
+
}
|
|
74
|
+
//WritableStream implementation
|
|
75
|
+
_write(chunk, _encoding, callback) {
|
|
76
|
+
if (typeof chunk !== 'string') {
|
|
77
|
+
throw new TypeError('Parser can work only with string streams.');
|
|
78
|
+
}
|
|
79
|
+
this.writeCallback = callback;
|
|
80
|
+
this.parser.tokenizer.write(chunk, this.lastChunkWritten, this.writeCallback);
|
|
81
|
+
}
|
|
82
|
+
// TODO [engine:node@>=16]: Due to issues with Node < 16, we are overriding `end` instead of `_final`.
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
end(chunk, encoding, callback) {
|
|
85
|
+
this.lastChunkWritten = true;
|
|
86
|
+
super.end(chunk || '', encoding, callback);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.ParserStream = ParserStream;
|
|
90
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Writable } from 'node:stream';
|
|
3
|
+
import { Parser, type ParserOptions, type TreeAdapterTypeMap, type DefaultTreeAdapterMap } from 'parse5';
|
|
4
|
+
/**
|
|
5
|
+
* Streaming HTML parser with scripting support.
|
|
6
|
+
* A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```js
|
|
11
|
+
* const ParserStream = require('parse5-parser-stream');
|
|
12
|
+
* const http = require('http');
|
|
13
|
+
* const { finished } = require('node:stream');
|
|
14
|
+
*
|
|
15
|
+
* // Fetch the page content and obtain it's <head> node
|
|
16
|
+
* http.get('http://inikulin.github.io/parse5/', res => {
|
|
17
|
+
* const parser = new ParserStream();
|
|
18
|
+
*
|
|
19
|
+
* finished(parser, () => {
|
|
20
|
+
* console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* res.pipe(parser);
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
export declare class ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> extends Writable {
|
|
29
|
+
parser: Parser<T>;
|
|
30
|
+
static getFragmentStream<T extends TreeAdapterTypeMap>(fragmentContext?: T['parentNode'] | null, options?: ParserOptions<T>): ParserStream<T>;
|
|
31
|
+
private lastChunkWritten;
|
|
32
|
+
private writeCallback;
|
|
33
|
+
private pendingHtmlInsertions;
|
|
34
|
+
/** The resulting document node. */
|
|
35
|
+
get document(): T['document'];
|
|
36
|
+
getFragment(): T['documentFragment'];
|
|
37
|
+
/**
|
|
38
|
+
* @param options Parsing options.
|
|
39
|
+
*/
|
|
40
|
+
constructor(options?: ParserOptions<T>, parser?: Parser<T>);
|
|
41
|
+
_write(chunk: string, _encoding: string, callback: () => void): void;
|
|
42
|
+
end(chunk?: any, encoding?: any, callback?: any): any;
|
|
43
|
+
}
|
|
44
|
+
export interface ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> {
|
|
45
|
+
/**
|
|
46
|
+
* Raised when parser encounters a `<script>` element. If this event has listeners, parsing will be suspended once
|
|
47
|
+
* it is emitted. So, if `<script>` has the `src` attribute, you can fetch it, execute and then resume parsing just
|
|
48
|
+
* like browsers do.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
*
|
|
52
|
+
* ```js
|
|
53
|
+
* const ParserStream = require('parse5-parser-stream');
|
|
54
|
+
* const http = require('http');
|
|
55
|
+
*
|
|
56
|
+
* const parser = new ParserStream();
|
|
57
|
+
*
|
|
58
|
+
* parser.on('script', (scriptElement, documentWrite, resume) => {
|
|
59
|
+
* const src = scriptElement.attrs.find(({ name }) => name === 'src').value;
|
|
60
|
+
*
|
|
61
|
+
* http.get(src, res => {
|
|
62
|
+
* // Fetch the script content, execute it with DOM built around `parser.document` and
|
|
63
|
+
* // `document.write` implemented using `documentWrite`.
|
|
64
|
+
* ...
|
|
65
|
+
* // Then resume parsing.
|
|
66
|
+
* resume();
|
|
67
|
+
* });
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* parser.end('<script src="example.com/script.js"></script>');
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @param event Name of the event
|
|
74
|
+
* @param handler
|
|
75
|
+
*/
|
|
76
|
+
on(event: 'script', handler: (scriptElement: T['element'], documentWrite: (html: string) => void, resume: () => void) => void): void;
|
|
77
|
+
/**
|
|
78
|
+
* Base event handler.
|
|
79
|
+
*
|
|
80
|
+
* @param event Name of the event
|
|
81
|
+
* @param handler Event handler
|
|
82
|
+
*/
|
|
83
|
+
on(event: string, handler: (...args: any[]) => void): this;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Writable } from 'node:stream';
|
|
2
|
+
import { Parser } from 'parse5';
|
|
3
|
+
/* eslint-disable unicorn/consistent-function-scoping -- The rule seems to be broken here. */
|
|
4
|
+
/**
|
|
5
|
+
* Streaming HTML parser with scripting support.
|
|
6
|
+
* A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```js
|
|
11
|
+
* const ParserStream = require('parse5-parser-stream');
|
|
12
|
+
* const http = require('http');
|
|
13
|
+
* const { finished } = require('node:stream');
|
|
14
|
+
*
|
|
15
|
+
* // Fetch the page content and obtain it's <head> node
|
|
16
|
+
* http.get('http://inikulin.github.io/parse5/', res => {
|
|
17
|
+
* const parser = new ParserStream();
|
|
18
|
+
*
|
|
19
|
+
* finished(parser, () => {
|
|
20
|
+
* console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* res.pipe(parser);
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
export class ParserStream extends Writable {
|
|
29
|
+
static getFragmentStream(fragmentContext, options) {
|
|
30
|
+
const parser = Parser.getFragmentParser(fragmentContext, options);
|
|
31
|
+
const stream = new ParserStream(options, parser);
|
|
32
|
+
return stream;
|
|
33
|
+
}
|
|
34
|
+
/** The resulting document node. */
|
|
35
|
+
get document() {
|
|
36
|
+
return this.parser.document;
|
|
37
|
+
}
|
|
38
|
+
getFragment() {
|
|
39
|
+
return this.parser.getFragment();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @param options Parsing options.
|
|
43
|
+
*/
|
|
44
|
+
constructor(options, parser = new Parser(options)) {
|
|
45
|
+
super({ decodeStrings: false });
|
|
46
|
+
this.parser = parser;
|
|
47
|
+
this.lastChunkWritten = false;
|
|
48
|
+
this.writeCallback = undefined;
|
|
49
|
+
this.pendingHtmlInsertions = [];
|
|
50
|
+
const resume = () => {
|
|
51
|
+
for (let i = this.pendingHtmlInsertions.length - 1; i >= 0; i--) {
|
|
52
|
+
this.parser.tokenizer.insertHtmlAtCurrentPos(this.pendingHtmlInsertions[i]);
|
|
53
|
+
}
|
|
54
|
+
this.pendingHtmlInsertions.length = 0;
|
|
55
|
+
//NOTE: keep parsing if we don't wait for the next input chunk
|
|
56
|
+
this.parser.tokenizer.resume(this.writeCallback);
|
|
57
|
+
};
|
|
58
|
+
const documentWrite = (html) => {
|
|
59
|
+
if (!this.parser.stopped) {
|
|
60
|
+
this.pendingHtmlInsertions.push(html);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const scriptHandler = (scriptElement) => {
|
|
64
|
+
if (this.listenerCount('script') > 0) {
|
|
65
|
+
this.parser.tokenizer.pause();
|
|
66
|
+
this.emit('script', scriptElement, documentWrite, resume);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
this.parser.scriptHandler = scriptHandler;
|
|
70
|
+
}
|
|
71
|
+
//WritableStream implementation
|
|
72
|
+
_write(chunk, _encoding, callback) {
|
|
73
|
+
if (typeof chunk !== 'string') {
|
|
74
|
+
throw new TypeError('Parser can work only with string streams.');
|
|
75
|
+
}
|
|
76
|
+
this.writeCallback = callback;
|
|
77
|
+
this.parser.tokenizer.write(chunk, this.lastChunkWritten, this.writeCallback);
|
|
78
|
+
}
|
|
79
|
+
// TODO [engine:node@>=16]: Due to issues with Node < 16, we are overriding `end` instead of `_final`.
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
|
+
end(chunk, encoding, callback) {
|
|
82
|
+
this.lastChunkWritten = true;
|
|
83
|
+
super.end(chunk || '', encoding, callback);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "parse5-parser-stream",
|
|
3
|
+
"type": "module",
|
|
3
4
|
"description": "Streaming HTML parser with scripting support.",
|
|
4
|
-
"version": "
|
|
5
|
+
"version": "7.1.2",
|
|
5
6
|
"author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
|
|
6
7
|
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
|
|
7
8
|
"homepage": "https://github.com/inikulin/parse5",
|
|
9
|
+
"funding": "https://github.com/inikulin/parse5?sponsor=1",
|
|
8
10
|
"keywords": [
|
|
9
11
|
"parse5",
|
|
10
12
|
"parser",
|
|
@@ -12,16 +14,26 @@
|
|
|
12
14
|
"streaming"
|
|
13
15
|
],
|
|
14
16
|
"license": "MIT",
|
|
15
|
-
"main": "
|
|
17
|
+
"main": "dist/cjs/index.js",
|
|
18
|
+
"module": "dist/index.js",
|
|
19
|
+
"types": "dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"require": "./dist/cjs/index.js"
|
|
23
|
+
},
|
|
16
24
|
"dependencies": {
|
|
17
|
-
"parse5": "^
|
|
25
|
+
"parse5": "^7.0.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build:cjs": "tsc --module CommonJS --target ES6 --outDir dist/cjs && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json"
|
|
18
29
|
},
|
|
19
30
|
"repository": {
|
|
20
31
|
"type": "git",
|
|
21
32
|
"url": "git://github.com/inikulin/parse5.git"
|
|
22
33
|
},
|
|
23
34
|
"files": [
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
35
|
+
"dist/cjs/package.json",
|
|
36
|
+
"dist/**/*.js",
|
|
37
|
+
"dist/**/*.d.ts"
|
|
38
|
+
]
|
|
27
39
|
}
|
package/lib/index.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { Writable } = require('stream');
|
|
4
|
-
const Parser = require('parse5/lib/parser');
|
|
5
|
-
|
|
6
|
-
class ParserStream extends Writable {
|
|
7
|
-
constructor(options) {
|
|
8
|
-
super({ decodeStrings: false });
|
|
9
|
-
|
|
10
|
-
this.parser = new Parser(options);
|
|
11
|
-
|
|
12
|
-
this.lastChunkWritten = false;
|
|
13
|
-
this.writeCallback = null;
|
|
14
|
-
this.pausedByScript = false;
|
|
15
|
-
|
|
16
|
-
this.document = this.parser.treeAdapter.createDocument();
|
|
17
|
-
|
|
18
|
-
this.pendingHtmlInsertions = [];
|
|
19
|
-
|
|
20
|
-
this._resume = this._resume.bind(this);
|
|
21
|
-
this._documentWrite = this._documentWrite.bind(this);
|
|
22
|
-
this._scriptHandler = this._scriptHandler.bind(this);
|
|
23
|
-
|
|
24
|
-
this.parser._bootstrap(this.document, null);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
//WritableStream implementation
|
|
28
|
-
_write(chunk, encoding, callback) {
|
|
29
|
-
if (typeof chunk !== 'string') {
|
|
30
|
-
throw new TypeError('Parser can work only with string streams.');
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
this.writeCallback = callback;
|
|
34
|
-
this.parser.tokenizer.write(chunk, this.lastChunkWritten);
|
|
35
|
-
this._runParsingLoop();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
end(chunk, encoding, callback) {
|
|
39
|
-
this.lastChunkWritten = true;
|
|
40
|
-
super.end(chunk || '', encoding, callback);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
//Scriptable parser implementation
|
|
44
|
-
_runParsingLoop() {
|
|
45
|
-
this.parser.runParsingLoopForCurrentChunk(this.writeCallback, this._scriptHandler);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
_resume() {
|
|
49
|
-
if (!this.pausedByScript) {
|
|
50
|
-
throw new Error('Parser was already resumed');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
while (this.pendingHtmlInsertions.length) {
|
|
54
|
-
const html = this.pendingHtmlInsertions.pop();
|
|
55
|
-
|
|
56
|
-
this.parser.tokenizer.insertHtmlAtCurrentPos(html);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
this.pausedByScript = false;
|
|
60
|
-
|
|
61
|
-
//NOTE: keep parsing if we don't wait for the next input chunk
|
|
62
|
-
if (this.parser.tokenizer.active) {
|
|
63
|
-
this._runParsingLoop();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
_documentWrite(html) {
|
|
68
|
-
if (!this.parser.stopped) {
|
|
69
|
-
this.pendingHtmlInsertions.push(html);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
_scriptHandler(scriptElement) {
|
|
74
|
-
if (this.listenerCount('script') > 0) {
|
|
75
|
-
this.pausedByScript = true;
|
|
76
|
-
this.emit('script', scriptElement, this._documentWrite, this._resume);
|
|
77
|
-
} else {
|
|
78
|
-
this._runParsingLoop();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
module.exports = ParserStream;
|