MCPcopy
hub / github.com/inikulin/parse5 / SAXParser

Class SAXParser

packages/parse5-sax-parser/lib/index.ts:42–237  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40 * ```
41 */
42export class SAXParser extends Transform implements TokenHandler {
43 protected options: SAXParserOptions;
44 /** @internal */
45 protected parserFeedbackSimulator: ParserFeedbackSimulator;
46 private pendingText: Text | null = null;
47 private lastChunkWritten = false;
48 private stopped = false;
49 protected tokenizer: Tokenizer;
50
51 /**
52 * @param options Parsing options.
53 */
54 constructor(options: SAXParserOptions = {}) {
55 super({ encoding: 'utf8', decodeStrings: false });
56
57 this.options = {
58 sourceCodeLocationInfo: false,
59 ...options,
60 };
61
62 this.parserFeedbackSimulator = new ParserFeedbackSimulator(this.options, this);
63 this.tokenizer = this.parserFeedbackSimulator.tokenizer;
64
65 // NOTE: always pipe the stream to the /dev/null stream to avoid
66 // the `highWaterMark` to be hit even if we don't have consumers.
67 // (see: https://github.com/inikulin/parse5/issues/97#issuecomment-171940774)
68 this.pipe(new DevNullStream());
69 }
70
71 //`Transform` implementation
72 override _transform(
73 chunk: string,
74 _encoding: string,
75 callback: (error?: Error | null, data?: string) => void,
76 ): void {
77 if (typeof chunk !== 'string') {
78 throw new TypeError('Parser can work only with string streams.');
79 }
80
81 callback(null, this._transformChunk(chunk));
82 }
83
84 override _final(callback: (error?: Error | null, data?: string) => void): void {
85 this.lastChunkWritten = true;
86 callback(null, this._transformChunk(''));
87 }
88
89 /**
90 * Stops parsing. Useful if you want the parser to stop consuming CPU time
91 * once you've obtained the desired info from the input stream. Doesn't
92 * prevent piping, so that data will flow through the parser as usual.
93 *
94 * @example
95 *
96 * ```js
97 * const SAXParser = require('parse5-sax-parser');
98 * const http = require('http');
99 * const fs = require('fs');

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…