Skip to content

Latest commit

 

History

History
74 lines (56 loc) · 2.44 KB

File metadata and controls

74 lines (56 loc) · 2.44 KB

Getting started

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.

Install

npm install @ismail-elkorchi/html-parser
deno add jsr:@ismail-elkorchi/html-parser

Parse a document

import { 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.

Set limits for untrusted input

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.”

Continue by task