parse5-html-rewriting-stream 8.0.1 → 8.1.1
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 +20 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +15 -5
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -19,6 +19,26 @@
|
|
|
19
19
|
📖 <a href="https://parse5.js.org/modules/parse5-html-rewriting-stream.html"><b>Documentation</b></a> 📖
|
|
20
20
|
</p>
|
|
21
21
|
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Modify HTML tokens while streaming the result. Tokens without a handler are
|
|
25
|
+
passed through unchanged; handlers must emit the replacement tokens.
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { Readable } from 'node:stream';
|
|
29
|
+
import { pipeline } from 'node:stream/promises';
|
|
30
|
+
import { RewritingStream } from 'parse5-html-rewriting-stream';
|
|
31
|
+
|
|
32
|
+
const rewriter = new RewritingStream();
|
|
33
|
+
rewriter.on('text', (token) => rewriter.emitText({ text: token.text.toUpperCase() }));
|
|
34
|
+
let output = '';
|
|
35
|
+
rewriter.on('data', (chunk) => {
|
|
36
|
+
output += chunk;
|
|
37
|
+
});
|
|
38
|
+
await pipeline(Readable.from(['<p>Hello, world!</p>']), rewriter);
|
|
39
|
+
console.log(output); // <p>HELLO, WORLD!</p>
|
|
40
|
+
```
|
|
41
|
+
|
|
22
42
|
---
|
|
23
43
|
|
|
24
44
|
<p align="center">
|
package/dist/index.d.ts
CHANGED
|
@@ -49,6 +49,8 @@ import { SAXParser, type EndTag, type StartTag, type Doctype, type Text, type Co
|
|
|
49
49
|
* ```
|
|
50
50
|
*/
|
|
51
51
|
export declare class RewritingStream extends SAXParser {
|
|
52
|
+
/** Whether text is currently tokenized without decoding character references. */
|
|
53
|
+
private inRawText;
|
|
52
54
|
/** Note: `sourceCodeLocationInfo` is always enabled. */
|
|
53
55
|
constructor();
|
|
54
56
|
_transformChunk(chunk: string): string;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TokenizerMode } from 'parse5';
|
|
2
2
|
import { SAXParser, } from 'parse5-sax-parser';
|
|
3
3
|
import { escapeText, escapeAttribute } from 'entities/escape';
|
|
4
4
|
/**
|
|
@@ -54,6 +54,8 @@ export class RewritingStream extends SAXParser {
|
|
|
54
54
|
/** Note: `sourceCodeLocationInfo` is always enabled. */
|
|
55
55
|
constructor() {
|
|
56
56
|
super({ sourceCodeLocationInfo: true });
|
|
57
|
+
/** Whether text is currently tokenized without decoding character references. */
|
|
58
|
+
this.inRawText = false;
|
|
57
59
|
}
|
|
58
60
|
_transformChunk(chunk) {
|
|
59
61
|
// NOTE: ignore upstream return values as we want to push to
|
|
@@ -69,9 +71,20 @@ export class RewritingStream extends SAXParser {
|
|
|
69
71
|
}
|
|
70
72
|
// Events
|
|
71
73
|
emitIfListenerExists(eventName, token) {
|
|
74
|
+
if (eventName === 'startTag') {
|
|
75
|
+
// The tokenizer has already been switched to the state for this element's content.
|
|
76
|
+
const { state } = this.tokenizer;
|
|
77
|
+
this.inRawText =
|
|
78
|
+
state === TokenizerMode.RAWTEXT ||
|
|
79
|
+
state === TokenizerMode.SCRIPT_DATA ||
|
|
80
|
+
state === TokenizerMode.PLAINTEXT;
|
|
81
|
+
}
|
|
72
82
|
if (!super.emitIfListenerExists(eventName, token)) {
|
|
73
83
|
this.emitRaw(this._getRawHtml(token.sourceCodeLocation));
|
|
74
84
|
}
|
|
85
|
+
if (eventName === 'endTag') {
|
|
86
|
+
this.inRawText = false;
|
|
87
|
+
}
|
|
75
88
|
// NOTE: don't skip new lines after `<pre>` and other tags,
|
|
76
89
|
// otherwise we'll have incorrect raw data.
|
|
77
90
|
this.parserFeedbackSimulator.skipNextNewLine = false;
|
|
@@ -111,10 +124,7 @@ export class RewritingStream extends SAXParser {
|
|
|
111
124
|
}
|
|
112
125
|
/** Emits a serialized text token into the output stream. */
|
|
113
126
|
emitText({ text }) {
|
|
114
|
-
this.push(
|
|
115
|
-
html.hasUnescapedText(this.tokenizer.lastStartTagName, true)
|
|
116
|
-
? text
|
|
117
|
-
: escapeText(text));
|
|
127
|
+
this.push(this.inRawText ? text : escapeText(text));
|
|
118
128
|
}
|
|
119
129
|
/** Emits a serialized comment token into the output stream. */
|
|
120
130
|
emitComment(token) {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "parse5-html-rewriting-stream",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"description": "Streaming HTML rewriter.",
|
|
5
|
-
"version": "8.
|
|
5
|
+
"version": "8.1.1",
|
|
6
6
|
"author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
|
|
7
7
|
"contributors": [
|
|
8
8
|
"James Garbutt (https://github.com/43081j)",
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"entities": "^
|
|
35
|
-
"parse5": "^8.
|
|
36
|
-
"parse5-sax-parser": "^8.0.
|
|
34
|
+
"entities": "^9.0.0",
|
|
35
|
+
"parse5": "^8.1.1",
|
|
36
|
+
"parse5-sax-parser": "^8.0.1"
|
|
37
37
|
},
|
|
38
38
|
"repository": {
|
|
39
39
|
"type": "git",
|
|
@@ -42,5 +42,8 @@
|
|
|
42
42
|
"files": [
|
|
43
43
|
"dist/**/*.js",
|
|
44
44
|
"dist/**/*.d.ts"
|
|
45
|
-
]
|
|
45
|
+
],
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"parse5-test-utils": "1.0.0"
|
|
48
|
+
}
|
|
46
49
|
}
|