parse5-html-rewriting-stream 6.0.1 → 7.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 +2 -2
- package/dist/index.d.ts +89 -0
- package/dist/index.js +127 -0
- package/package.json +14 -8
- package/lib/index.js +0 -92
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
<br>
|
|
17
17
|
|
|
18
18
|
<p align="center">
|
|
19
|
-
📖 <a href="https://
|
|
19
|
+
📖 <a href="https://parse5.js.org/modules/parse5-html-rewriting-stream.html"><b>Documentation</b></a> 📖
|
|
20
20
|
</p>
|
|
21
21
|
|
|
22
22
|
---
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
</p>
|
|
31
31
|
|
|
32
32
|
<p align="center">
|
|
33
|
-
<a href="https://github.com/inikulin/parse5/
|
|
33
|
+
<a href="https://github.com/inikulin/parse5/releases">Changelog</a>
|
|
34
34
|
</p>
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { SAXParser, type EndTag, type StartTag, type Doctype, type Text, type Comment, type SaxToken } from 'parse5-sax-parser';
|
|
2
|
+
/**
|
|
3
|
+
* Streaming [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML rewriter.
|
|
4
|
+
* A [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform) (which means you can pipe _through_ it, see example).
|
|
5
|
+
*
|
|
6
|
+
* The rewriter uses the raw source representation of tokens if they are not modified by the user. Therefore, the resulting
|
|
7
|
+
* HTML is not affected by parser error-recovery mechanisms as in a classical parsing-serialization roundtrip.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* const RewritingStream = require('parse5-html-rewriting-stream');
|
|
13
|
+
* const http = require('http');
|
|
14
|
+
* const fs = require('fs');
|
|
15
|
+
*
|
|
16
|
+
* const file = fs.createWriteStream('/home/google.com.html');
|
|
17
|
+
* const rewriter = new RewritingStream();
|
|
18
|
+
*
|
|
19
|
+
* // Replace divs with spans
|
|
20
|
+
* rewriter.on('startTag', startTag => {
|
|
21
|
+
* if (startTag.tagName === 'span') {
|
|
22
|
+
* startTag.tagName = 'div';
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* rewriter.emitStartTag(startTag);
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* rewriter.on('endTag', endTag => {
|
|
29
|
+
* if (endTag.tagName === 'span') {
|
|
30
|
+
* endTag.tagName = 'div';
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* rewriter.emitEndTag(endTag);
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Wrap all text nodes with an <i> tag
|
|
37
|
+
* rewriter.on('text', (_, raw) => {
|
|
38
|
+
* // Use the raw representation of text without HTML entities decoding
|
|
39
|
+
* rewriter.emitRaw(`<i>${raw}</i>`);
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* http.get('http://google.com', res => {
|
|
43
|
+
* // Assumes response is UTF-8.
|
|
44
|
+
* res.setEncoding('utf8');
|
|
45
|
+
* // `RewritingStream` is a `Transform` stream, which means you can pipe
|
|
46
|
+
* // through it.
|
|
47
|
+
* res.pipe(rewriter).pipe(file);
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare class RewritingStream extends SAXParser {
|
|
52
|
+
/** Note: `sourceCodeLocationInfo` is always enabled. */
|
|
53
|
+
constructor();
|
|
54
|
+
_transformChunk(chunk: string): string;
|
|
55
|
+
private _getRawHtml;
|
|
56
|
+
protected emitIfListenerExists(eventName: string, token: SaxToken): boolean;
|
|
57
|
+
protected _emitToken(eventName: string, token: SaxToken): void;
|
|
58
|
+
/** Emits a serialized document type token into the output stream. */
|
|
59
|
+
emitDoctype(token: Doctype): void;
|
|
60
|
+
/** Emits a serialized start tag token into the output stream. */
|
|
61
|
+
emitStartTag(token: StartTag): void;
|
|
62
|
+
/** Emits a serialized end tag token into the output stream. */
|
|
63
|
+
emitEndTag(token: EndTag): void;
|
|
64
|
+
/** Emits a serialized text token into the output stream. */
|
|
65
|
+
emitText({ text }: Text): void;
|
|
66
|
+
/** Emits a serialized comment token into the output stream. */
|
|
67
|
+
emitComment(token: Comment): void;
|
|
68
|
+
/** Emits a raw HTML string into the output stream. */
|
|
69
|
+
emitRaw(html: string): void;
|
|
70
|
+
}
|
|
71
|
+
export interface RewritingStream {
|
|
72
|
+
/** Raised when the rewriter encounters a start tag. */
|
|
73
|
+
on(event: 'startTag', listener: (startTag: StartTag, rawHtml: string) => void): this;
|
|
74
|
+
/** Raised when rewriter encounters an end tag. */
|
|
75
|
+
on(event: 'endTag', listener: (endTag: EndTag, rawHtml: string) => void): this;
|
|
76
|
+
/** Raised when rewriter encounters a comment. */
|
|
77
|
+
on(event: 'comment', listener: (comment: Comment, rawHtml: string) => void): this;
|
|
78
|
+
/** Raised when rewriter encounters text content. */
|
|
79
|
+
on(event: 'text', listener: (text: Text, rawHtml: string) => void): this;
|
|
80
|
+
/** Raised when rewriter encounters a [document type declaration](https://en.wikipedia.org/wiki/Document_type_declaration). */
|
|
81
|
+
on(event: 'doctype', listener: (doctype: Doctype, rawHtml: string) => void): this;
|
|
82
|
+
/**
|
|
83
|
+
* Base event handler.
|
|
84
|
+
*
|
|
85
|
+
* @param event Name of the event
|
|
86
|
+
* @param handler Event handler
|
|
87
|
+
*/
|
|
88
|
+
on(event: string, handler: (...args: any[]) => void): this;
|
|
89
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { html } from 'parse5';
|
|
2
|
+
import { SAXParser, } from 'parse5-sax-parser';
|
|
3
|
+
import { escapeText, escapeAttribute } from 'entities/escape';
|
|
4
|
+
/**
|
|
5
|
+
* Streaming [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML rewriter.
|
|
6
|
+
* A [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform) (which means you can pipe _through_ it, see example).
|
|
7
|
+
*
|
|
8
|
+
* The rewriter uses the raw source representation of tokens if they are not modified by the user. Therefore, the resulting
|
|
9
|
+
* HTML is not affected by parser error-recovery mechanisms as in a classical parsing-serialization roundtrip.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```js
|
|
14
|
+
* const RewritingStream = require('parse5-html-rewriting-stream');
|
|
15
|
+
* const http = require('http');
|
|
16
|
+
* const fs = require('fs');
|
|
17
|
+
*
|
|
18
|
+
* const file = fs.createWriteStream('/home/google.com.html');
|
|
19
|
+
* const rewriter = new RewritingStream();
|
|
20
|
+
*
|
|
21
|
+
* // Replace divs with spans
|
|
22
|
+
* rewriter.on('startTag', startTag => {
|
|
23
|
+
* if (startTag.tagName === 'span') {
|
|
24
|
+
* startTag.tagName = 'div';
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* rewriter.emitStartTag(startTag);
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* rewriter.on('endTag', endTag => {
|
|
31
|
+
* if (endTag.tagName === 'span') {
|
|
32
|
+
* endTag.tagName = 'div';
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* rewriter.emitEndTag(endTag);
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* // Wrap all text nodes with an <i> tag
|
|
39
|
+
* rewriter.on('text', (_, raw) => {
|
|
40
|
+
* // Use the raw representation of text without HTML entities decoding
|
|
41
|
+
* rewriter.emitRaw(`<i>${raw}</i>`);
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* http.get('http://google.com', res => {
|
|
45
|
+
* // Assumes response is UTF-8.
|
|
46
|
+
* res.setEncoding('utf8');
|
|
47
|
+
* // `RewritingStream` is a `Transform` stream, which means you can pipe
|
|
48
|
+
* // through it.
|
|
49
|
+
* res.pipe(rewriter).pipe(file);
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export class RewritingStream extends SAXParser {
|
|
54
|
+
/** Note: `sourceCodeLocationInfo` is always enabled. */
|
|
55
|
+
constructor() {
|
|
56
|
+
super({ sourceCodeLocationInfo: true });
|
|
57
|
+
}
|
|
58
|
+
_transformChunk(chunk) {
|
|
59
|
+
// NOTE: ignore upstream return values as we want to push to
|
|
60
|
+
// the `Writable` part of the `Transform` stream ourselves.
|
|
61
|
+
super._transformChunk(chunk);
|
|
62
|
+
return '';
|
|
63
|
+
}
|
|
64
|
+
_getRawHtml(location) {
|
|
65
|
+
const { droppedBufferSize, html } = this.tokenizer.preprocessor;
|
|
66
|
+
const start = location.startOffset - droppedBufferSize;
|
|
67
|
+
const end = location.endOffset - droppedBufferSize;
|
|
68
|
+
return html.slice(start, end);
|
|
69
|
+
}
|
|
70
|
+
// Events
|
|
71
|
+
emitIfListenerExists(eventName, token) {
|
|
72
|
+
if (!super.emitIfListenerExists(eventName, token)) {
|
|
73
|
+
this.emitRaw(this._getRawHtml(token.sourceCodeLocation));
|
|
74
|
+
}
|
|
75
|
+
// NOTE: don't skip new lines after `<pre>` and other tags,
|
|
76
|
+
// otherwise we'll have incorrect raw data.
|
|
77
|
+
this.parserFeedbackSimulator.skipNextNewLine = false;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
// Emitter API
|
|
81
|
+
_emitToken(eventName, token) {
|
|
82
|
+
this.emit(eventName, token, this._getRawHtml(token.sourceCodeLocation));
|
|
83
|
+
}
|
|
84
|
+
/** Emits a serialized document type token into the output stream. */
|
|
85
|
+
emitDoctype(token) {
|
|
86
|
+
let res = `<!DOCTYPE ${token.name}`;
|
|
87
|
+
if (token.publicId !== null) {
|
|
88
|
+
res += ` PUBLIC "${token.publicId}"`;
|
|
89
|
+
}
|
|
90
|
+
else if (token.systemId !== null) {
|
|
91
|
+
res += ' SYSTEM';
|
|
92
|
+
}
|
|
93
|
+
if (token.systemId !== null) {
|
|
94
|
+
res += ` "${token.systemId}"`;
|
|
95
|
+
}
|
|
96
|
+
res += '>';
|
|
97
|
+
this.push(res);
|
|
98
|
+
}
|
|
99
|
+
/** Emits a serialized start tag token into the output stream. */
|
|
100
|
+
emitStartTag(token) {
|
|
101
|
+
let res = `<${token.tagName}`;
|
|
102
|
+
for (const attr of token.attrs) {
|
|
103
|
+
res += ` ${attr.name}="${escapeAttribute(attr.value)}"`;
|
|
104
|
+
}
|
|
105
|
+
res += token.selfClosing ? '/>' : '>';
|
|
106
|
+
this.push(res);
|
|
107
|
+
}
|
|
108
|
+
/** Emits a serialized end tag token into the output stream. */
|
|
109
|
+
emitEndTag(token) {
|
|
110
|
+
this.push(`</${token.tagName}>`);
|
|
111
|
+
}
|
|
112
|
+
/** Emits a serialized text token into the output stream. */
|
|
113
|
+
emitText({ text }) {
|
|
114
|
+
this.push(!this.parserFeedbackSimulator.inForeignContent &&
|
|
115
|
+
html.hasUnescapedText(this.tokenizer.lastStartTagName, true)
|
|
116
|
+
? text
|
|
117
|
+
: escapeText(text));
|
|
118
|
+
}
|
|
119
|
+
/** Emits a serialized comment token into the output stream. */
|
|
120
|
+
emitComment(token) {
|
|
121
|
+
this.push(`<!--${token.text}-->`);
|
|
122
|
+
}
|
|
123
|
+
/** Emits a raw HTML string into the output stream. */
|
|
124
|
+
emitRaw(html) {
|
|
125
|
+
this.push(html);
|
|
126
|
+
}
|
|
127
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "parse5-html-rewriting-stream",
|
|
3
|
+
"type": "module",
|
|
3
4
|
"description": "Streaming HTML rewriter.",
|
|
4
|
-
"version": "
|
|
5
|
+
"version": "7.1.0",
|
|
5
6
|
"author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
|
|
6
7
|
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
|
|
7
|
-
"homepage": "https://
|
|
8
|
+
"homepage": "https://parse5.js.org",
|
|
9
|
+
"funding": "https://github.com/inikulin/parse5?sponsor=1",
|
|
8
10
|
"keywords": [
|
|
9
11
|
"parse5",
|
|
10
12
|
"parser",
|
|
@@ -15,17 +17,21 @@
|
|
|
15
17
|
"HTML"
|
|
16
18
|
],
|
|
17
19
|
"license": "MIT",
|
|
18
|
-
"main": "
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"module": "dist/index.js",
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"exports": "./dist/index.js",
|
|
19
24
|
"dependencies": {
|
|
20
|
-
"
|
|
21
|
-
"parse5
|
|
25
|
+
"entities": "^6.0.0",
|
|
26
|
+
"parse5": "^7.0.0",
|
|
27
|
+
"parse5-sax-parser": "^7.0.0"
|
|
22
28
|
},
|
|
23
29
|
"repository": {
|
|
24
30
|
"type": "git",
|
|
25
31
|
"url": "git://github.com/inikulin/parse5.git"
|
|
26
32
|
},
|
|
27
33
|
"files": [
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
"dist/**/*.js",
|
|
35
|
+
"dist/**/*.d.ts"
|
|
36
|
+
]
|
|
31
37
|
}
|
package/lib/index.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const SAXParser = require('parse5-sax-parser');
|
|
4
|
-
const { escapeString } = require('parse5/lib/serializer');
|
|
5
|
-
|
|
6
|
-
class RewritingStream extends SAXParser {
|
|
7
|
-
constructor() {
|
|
8
|
-
super({ sourceCodeLocationInfo: true });
|
|
9
|
-
|
|
10
|
-
this.posTracker = this.locInfoMixin.posTracker;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
_transformChunk(chunk) {
|
|
14
|
-
// NOTE: ignore upstream return value as we want to push to
|
|
15
|
-
// the Writable part of Transform stream ourselves.
|
|
16
|
-
super._transformChunk(chunk);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
_getRawHtml(location) {
|
|
20
|
-
const droppedBufferSize = this.posTracker.droppedBufferSize;
|
|
21
|
-
const start = location.startOffset - droppedBufferSize;
|
|
22
|
-
const end = location.endOffset - droppedBufferSize;
|
|
23
|
-
|
|
24
|
-
return this.tokenizer.preprocessor.html.slice(start, end);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Events
|
|
28
|
-
_handleToken(token) {
|
|
29
|
-
if (!super._handleToken(token)) {
|
|
30
|
-
this.emitRaw(this._getRawHtml(token.location));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// NOTE: don't skip new lines after <pre> and other tags,
|
|
34
|
-
// otherwise we'll have incorrect raw data.
|
|
35
|
-
this.parserFeedbackSimulator.skipNextNewLine = false;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Emitter API
|
|
39
|
-
_emitToken(eventName, token) {
|
|
40
|
-
this.emit(eventName, token, this._getRawHtml(token.sourceCodeLocation));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
emitDoctype(token) {
|
|
44
|
-
let res = `<!DOCTYPE ${token.name}`;
|
|
45
|
-
|
|
46
|
-
if (token.publicId !== null) {
|
|
47
|
-
res += ` PUBLIC "${token.publicId}"`;
|
|
48
|
-
} else if (token.systemId !== null) {
|
|
49
|
-
res += ' SYSTEM';
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (token.systemId !== null) {
|
|
53
|
-
res += ` "${token.systemId}"`;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
res += '>';
|
|
57
|
-
|
|
58
|
-
this.push(res);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
emitStartTag(token) {
|
|
62
|
-
let res = `<${token.tagName}`;
|
|
63
|
-
|
|
64
|
-
const attrs = token.attrs;
|
|
65
|
-
|
|
66
|
-
for (let i = 0; i < attrs.length; i++) {
|
|
67
|
-
res += ` ${attrs[i].name}="${escapeString(attrs[i].value, true)}"`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
res += token.selfClosing ? '/>' : '>';
|
|
71
|
-
|
|
72
|
-
this.push(res);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
emitEndTag(token) {
|
|
76
|
-
this.push(`</${token.tagName}>`);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
emitText({ text }) {
|
|
80
|
-
this.push(escapeString(text, false));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
emitComment(token) {
|
|
84
|
-
this.push(`<!--${token.text}-->`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
emitRaw(html) {
|
|
88
|
-
this.push(html);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
module.exports = RewritingStream;
|