Optionaloptions: ParserOptions<T>Calling the writable.end() method signals that no more data will be written
to the Writable. The optional chunk and encoding arguments allow one
final additional chunk of data to be written immediately before closing the
stream.
Calling the write method after calling end will raise an error.
// Write 'hello, ' and then end with 'world!'.
import fs from 'node:fs';
const file = fs.createWriteStream('example.txt');
file.write('hello, ');
file.end('world!');
// Writing more now is not allowed!
Optionalchunk: any
Optional data to write. For streams not operating in object mode, chunk must be a {string}, {Buffer},
{TypedArray} or {DataView}. For object mode streams, chunk may be any JavaScript value other than null.
Optionalencoding: any
The encoding if chunk is a string
Optionalcallback: any
Callback for when the stream is finished.
Synchronously delivers buffered character tokens to the tree builder without ending the input stream. Incomplete tags and character references are retained for subsequent writes. This does not resume a suspended script or process queued writes, and must not be called from a parser callback.
Flushing splits character-token runs. Their source locations do not extend through subsequently
ignored input, such as CDATA delimiters or the leading newline in a pre element, so text-node
source ranges can differ from parsing without flushing.
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.
Name of the event
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>');
Base event handler.
Name of the event
Event handler
StaticgetOptionalfragmentContext: T["parentNode"] | nullOptionaloptions: ParserOptions<T>
Converts plain text files into HTML document as required by HTML specification. A writable stream.
Example