$ show fb55/htmlparser2
htmlparser2 is a fast, forgiving HTML/XML parser for Node.js and browsers, written in TypeScript. It provides a low-level streaming tokenizer (Tokenizer) that feeds events to a higher-level Parser, which handles tag matching, implicit closing, foreign content (SVG/MathML), and entity decoding. The library also includes Node.js WritableStream and Web Streams API WritableStream adapters for piping data directly into the parser. It is the foundation for cheerio and other DOM manipulation libraries. The codebase is well-structured, thoroughly tested, and actively maintained, with a focus on performance and spec compliance for real-world HTML.
The LLM identified 3 files worth highlighting for reuse:
A low-level, state-machine-based tokenizer that breaks HTML/XML input into tokens (tags, attributes, text, comments, CDATA, entities). It handles special tags (script, style, title, textarea, etc.), foreign content contexts, and entity decoding via the `entities` library. The tokenizer is highly optimized with fast-forwarding and minimal allocations. It is reusable as a standalone tokenizer for any HTML/XML processing pipeline.
An incremental, event-driven parser that consumes tokens from the Tokenizer and emits high-level events (onopentag, ontext, onclosetag, etc.). It implements HTML5 implicit closing rules, void elements, SVG/MathML foreign content handling, and SVG tag name adjustments. The parser maintains a stack of open elements and a foreign context stack. It is designed for streaming and supports pause/resume. The architecture cleanly separates tokenization from parsing logic.
A test helper that uses a JavaScript Proxy to create a handler that collects all parser events into an array, with start/end indices. It automatically merges consecutive text events and validates index consistency. This pattern is reusable for testing any event-driven parser or stream processor, providing a simple way to capture and snapshot events.