BarrierLab

html5-parser

WHATWG HTML parsing in pure Rust: tokenizer and tree construction with per-node source positions and parse errors.

Turns an HTML string into a tree, following the WHATWG HTML parsing specification (tokenizer and tree construction, §13.2). It reports every parse error the specification names and gives each node its position in the source. It evaluates nothing: no accessibility, no conformance, no CSS. Norm: WHATWG HTML, Parsing.

Status

On crates.io, no dependencies, no unsafe. Used by html-conform (conformance check), a11y-static (static audit) and, behind the optional feature html5-parser, a11y-dom (an arena built from parsed HTML). Tests of a11y-rules, accname and style-cascade parse their fixtures with it.

Measured against the vendored html5lib-tests tree-construction corpus: all 1,726 applicable cases pass. The tokenizer corpus matches on token streams. The parse errors are compared with the corpus too, but only partly: presence of an error agrees in 1,581 of 1,602 cases, the WHATWG codes in 1,560; the 51 differing cases are listed with reasons in tests/html5lib_errors_known_failures.txt. Counts and gates: README.md.

Structure

state switch,

foreign-content flag

HTML text

Tokenizer

states §13.2.5

Tree construction

insertion modes §13.2.6

Document

nodes + positions

ParseError list

The two stages run token by token and feed back into each other, as the specification requires (RCDATA, RAWTEXT, script data and PLAINTEXT switches; CDATA in foreign content). Both contribute parse errors; parse merges them in document order.

Public surface

EntryPurpose
parsetext → ParseResult; never fails
ParseResultdocument (always complete) and errors
Documentread-only arena: root, node, parent, children
Node, NodeIdkind, position, end_position (end of an element’s start tag)
NodeKindDocument, Element (name, namespace, attributes), Text, Comment, ProcessingInstruction, Doctype, DocumentFragment
ParseError, ParseErrorKind, Positiona WHATWG parse error with line, column (one-based) and byte offset

Tokenizer and TreeBuilder are not public. ParseErrorKind is #[non_exhaustive] and holds only errors the crate detects.

Dependencies

Downward: none. Upward: html-conform (L1), a11y-static (L2) and the optional a11y-dom feature (L1). html5-parser itself is L0 and knows no accessibility term.

Limits

  • No fragment parsing and no scripting. parse always parses a whole document with scripting off; the html5lib cases for fragments and #script-on are counted by the test harness and not run.
  • <template>: the content-fragment model is implemented; declarative shadow DOM (shadowrootmode) and content patching (for) are not.
  • <selectedcontent>: option mirroring is implemented for ordinary parse-time use with simplifications (no multiple <select>, a practical rather than complete walk over <optgroup> nesting).
  • Parse errors are a diagnosis, not a verdict. They say where the input leaves the strict grammar. Whether a document is conformant is a question for html-conform.
  • Read-only tree. No serialization and no editing API; the tree is for walking.

Edit this page on GitHub