This project is pre-1.0. Minor releases may contain intentional breaking changes, so use the documentation and TypeScript declarations shipped with the version you install.
npm install @ismail-elkorchi/html-parserdeno add jsr:@ismail-elkorchi/html-parserimport { parse, serialize } from "@ismail-elkorchi/html-parser";
const document = parse("<!doctype html><title>Hello</title><main>World</main>");
console.log(document.tree.kind); // "document"
console.log(document.tree.errors); // non-fatal HTML parse diagnostics
console.log(document.metadata.resourceUsage.nodes);
console.log(serialize(document.tree));parse(), parseBytes(), and parseStream() return a frozen
ParsedDocument. Its tree is the parsed document, metadata describes the
exact parse, and sourceText is null unless source retention was requested.
parseFragment() returns a frozen ParsedFragment: its tree is the fragment
and its metadata records successful resource use. It requires the namespace
and local name of the context element. See fragments.
HTML recovery diagnostics do not normally throw. Invalid configuration, exceeded budgets, cancellation, stream failures, and invalid patch operations throw typed operational errors. See limits, errors, and safety.
Parser limits are disabled unless you supply them. Choose limits from the request or job budget rather than copying arbitrary global values.
import { parse } from "@ismail-elkorchi/html-parser";
const document = parse("<article><p>bounded</p></article>", {
budgets: {
maxInputBytes: 64 * 1_024,
maxDecodedUtf8Bytes: 64 * 1_024,
maxNodes: 4_096,
maxDepth: 128,
maxParseErrors: 256,
maxAttributesPerElement: 128,
maxAttributeBytes: 16 * 1_024,
maxTimeMs: 250
}
});
console.log(document.tree.children.length);Every limit is inclusive. Missing limits are not enforced; zero is a valid limit and does not mean “unlimited.”
- Use parsing for text, byte, and fragment entry points.
- Use streams and encoding for response bodies.
- Use querying and text to consume the tree.
- Use modifying HTML for source-aware edits.
- Use the API guide for entry points and type groups.