The `html-parser` library provides a fault-tolerant parser for HTML and XML, designed to process even malformed input without 'explosions'. Its primary feature is robust sanitization capabilities, allowing developers to strip unwanted elements, attributes, and comments from untrusted HTML content. The library operates using a callback-based API, offering granular control over how various HTML tokens (elements, attributes, text, comments, CDATA, doctype) are handled during parsing. Currently at version 0.11.0 and last published over nine years ago, this package is no longer actively maintained. Its key differentiators historically were its resilience to invalid markup and its built-in, configurable sanitization features, making it suitable for preparing user-generated HTML for safe display, though its age raises concerns about modern security vulnerabilities.
npm install html-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates both the callback-based HTML parsing and the sanitization features of the library. It shows how to process an HTML string, logging events for various tokens, and how to remove malicious script tags, event attributes, and comments.
Migrate to a maintained HTML parsing and sanitization library like `htmlparser2` or `parse5` for parsing, and a dedicated sanitization library like `dompurify` for security.
Always use `const htmlParser = require('html-parser');` to import the library in Node.js environments.For secure HTML sanitization, use actively maintained and peer-reviewed libraries such as `dompurify`. Consider server-side sanitization as a primary defense.
It is strongly recommended to migrate to a modern, actively maintained HTML parsing and sanitization library.
Ensure you are using CommonJS `require` and accessing `parse` as a method of the default export: `const htmlParser = require('html-parser'); htmlParser.parse(...)`Add `const htmlParser = require('html-parser');` at the top of your file to ensure the module is loaded and accessible.Review the `sanitize` options carefully. `elements` and `attributes` arrays specify *what to remove*, or provide a callback function that returns `true` for items to be removed. Ensure `comments: true` is set if comments should be stripped.
No dependency data recorded yet.