htmlparser2 parses HTML and XML quickly with a permissive tokenizer geared toward real-world markup. It offers streaming callbacks, DOM construction, and feed parsing helpers.
Overview
htmlparser2 is a JavaScript parser for HTML and XML marketed as fast and tolerant of messy documents. It prioritizes speed over strict HTML5 compliance; teams who need spec-perfect parsing can use parse5 instead. The core API fires fine-grained events for tags, attributes, text, comments, CDATA, and processing instructions with optional implied open and close handling in HTML mode.
For lower allocation usage, handlers consume events through a Parser instance or a WritableStream adapter when reading files. The parseDocument helper builds a DOM tree using the domhandler integration, with options for indices and XML mode for feeds. DomUtils exposes traversal, text extraction, and in-tree edits, while sibling projects css-select and cheerio add selector queries and jQuery-like ergonomics.
parseFeed recognizes RSS, RDF, and Atom documents and returns structured title, link, and item fields when xmlMode is enabled. Parser flags control entity decoding, lowercasing, self-closing recognition, and CDATA treatment. Published benchmarks compare htmlparser2 against other Node parsers on real-world pages.
Installation is through npm. A live AST Explorer demo is linked for interactive inspection. Security issues can be reported through the Tidelift coordinated disclosure channel.
Key features
- Streaming HTML and XML tokenization
- DOM tree via parseDocument
- RSS Atom and RDF feed helper
- Configurable HTML versus XML mode
- WritableStream file parsing
- DomUtils search and mutation
- Entity decoding options
- Ecosystem ties to cheerio and css-select
Use cases
- Scraping imperfect HTML pages
- Building server-side DOM snapshots
- Parsing syndication feeds
- Transforming markup in Node pipelines