parse5-parser-stream 7.1.2 → 8.1.0

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 CHANGED
@@ -16,7 +16,7 @@
16
16
  <br>
17
17
 
18
18
  <p align="center">
19
- 📖 <a href="https://parse5.js.org/modules/parse5_parser_stream.html"><b>Documentation</b></a> 📖
19
+ 📖 <a href="https://parse5.js.org/modules/parse5-parser-stream.html"><b>Documentation</b></a> 📖
20
20
  </p>
21
21
 
22
22
  ---
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { Writable } from 'node:stream';
3
2
  import { Parser, type ParserOptions, type TreeAdapterTypeMap, type DefaultTreeAdapterMap } from 'parse5';
4
3
  /**
@@ -34,6 +33,26 @@ export declare class ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdap
34
33
  /** The resulting document node. */
35
34
  get document(): T['document'];
36
35
  getFragment(): T['documentFragment'];
36
+ /**
37
+ * Synchronously delivers buffered character tokens to the tree builder without ending the input stream.
38
+ * Incomplete tags and character references are retained for subsequent writes. This does not resume a
39
+ * suspended script or process queued writes, and must not be called from a parser callback.
40
+ *
41
+ * Flushing splits character-token runs. Their source locations do not extend through subsequently
42
+ * ignored input, such as CDATA delimiters or the leading newline in a `pre` element, so text-node
43
+ * source ranges can differ from parsing without flushing.
44
+ *
45
+ * @example
46
+ *
47
+ * ```js
48
+ * const stream = new ParserStream();
49
+ * stream.write('<p>hello');
50
+ * stream.flushCharacters();
51
+ * // The paragraph's text is now available in `stream.document`.
52
+ * stream.end(' world</p>');
53
+ * ```
54
+ */
55
+ flushCharacters(): void;
37
56
  /**
38
57
  * @param options Parsing options.
39
58
  */
@@ -82,4 +101,3 @@ export interface ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterM
82
101
  */
83
102
  on(event: string, handler: (...args: any[]) => void): this;
84
103
  }
85
- //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Writable } from 'node:stream';
2
2
  import { Parser } from 'parse5';
3
- /* eslint-disable unicorn/consistent-function-scoping -- The rule seems to be broken here. */
4
3
  /**
5
4
  * Streaming HTML parser with scripting support.
6
5
  * A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
@@ -38,6 +37,28 @@ export class ParserStream extends Writable {
38
37
  getFragment() {
39
38
  return this.parser.getFragment();
40
39
  }
40
+ /**
41
+ * Synchronously delivers buffered character tokens to the tree builder without ending the input stream.
42
+ * Incomplete tags and character references are retained for subsequent writes. This does not resume a
43
+ * suspended script or process queued writes, and must not be called from a parser callback.
44
+ *
45
+ * Flushing splits character-token runs. Their source locations do not extend through subsequently
46
+ * ignored input, such as CDATA delimiters or the leading newline in a `pre` element, so text-node
47
+ * source ranges can differ from parsing without flushing.
48
+ *
49
+ * @example
50
+ *
51
+ * ```js
52
+ * const stream = new ParserStream();
53
+ * stream.write('<p>hello');
54
+ * stream.flushCharacters();
55
+ * // The paragraph's text is now available in `stream.document`.
56
+ * stream.end(' world</p>');
57
+ * ```
58
+ */
59
+ flushCharacters() {
60
+ this.parser.tokenizer.flushCharacters();
61
+ }
41
62
  /**
42
63
  * @param options Parsing options.
43
64
  */
@@ -83,4 +104,3 @@ export class ParserStream extends Writable {
83
104
  super.end(chunk || '', encoding, callback);
84
105
  }
85
106
  }
86
- //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -2,10 +2,15 @@
2
2
  "name": "parse5-parser-stream",
3
3
  "type": "module",
4
4
  "description": "Streaming HTML parser with scripting support.",
5
- "version": "7.1.2",
5
+ "version": "8.1.0",
6
6
  "author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
7
- "contributors": "https://github.com/inikulin/parse5/graphs/contributors",
8
- "homepage": "https://github.com/inikulin/parse5",
7
+ "contributors": [
8
+ "James Garbutt (https://github.com/43081j)",
9
+ "Felix Boehm (https://github.com/fb55)",
10
+ "Ivan Nikulin (https://github.com/inikulin)",
11
+ "Titus (https://github.com/wooorm)"
12
+ ],
13
+ "homepage": "https://parse5.js.org",
9
14
  "funding": "https://github.com/inikulin/parse5?sponsor=1",
10
15
  "keywords": [
11
16
  "parse5",
@@ -14,26 +19,27 @@
14
19
  "streaming"
15
20
  ],
16
21
  "license": "MIT",
17
- "main": "dist/cjs/index.js",
22
+ "main": "dist/index.js",
18
23
  "module": "dist/index.js",
19
24
  "types": "dist/index.d.ts",
20
25
  "exports": {
21
- "import": "./dist/index.js",
22
- "require": "./dist/cjs/index.js"
26
+ ".": {
27
+ "default": "./dist/index.js"
28
+ }
23
29
  },
24
30
  "dependencies": {
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"
31
+ "parse5": "^8.0.0"
29
32
  },
33
+ "scripts": {},
30
34
  "repository": {
31
35
  "type": "git",
32
36
  "url": "git://github.com/inikulin/parse5.git"
33
37
  },
34
38
  "files": [
35
- "dist/cjs/package.json",
36
39
  "dist/**/*.js",
37
40
  "dist/**/*.d.ts"
38
- ]
41
+ ],
42
+ "devDependencies": {
43
+ "parse5-test-utils": "1.0.0"
44
+ }
39
45
  }
@@ -1,85 +0,0 @@
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/cjs/index.js DELETED
@@ -1,90 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"type":"commonjs"}