Skip to content

What it does

  • The HTML Standard’s tokenizer and tree builder, for whole documents and fragments, with the scripting flag on or off.
  • Input whole, in chunks, or as bytes with the encoding sniffed. Any split builds the same tree.
  • Parse errors by the spec’s own names, each with a byte offset, line and column.
  • A span on every node and attribute, so you can point back at the source.
  • CSS selectors, Level 4, matched as a browser would on a static page, :scope and the form pseudo-classes included.
  • XPath 1.0 over the same tree.
  • Edits: create, insert, move, detach and remove nodes, change attributes, then write it back as innerHTML or outerHTML.
  • Declarative shadow DOM, as the parser attaches it.
  • A time budget, a size cap and a depth limit for input you don’t trust.

Add it

In Rust, add parsec-web (0.3.1 on crates.io); the library inside isparsec. Parsing, selectors, XPath, positions and parse errors all work from Python (pip install parsec-web), JavaScript (npm install parsec-web) and C.

parsec-web 0.3.1, from its documentation
use parsec::css::SelectorList;

let doc = parsec::parse("<ul><li class=hit>one<li>two</ul>", false);
let hits = SelectorList::parse("li.hit").unwrap().select(&doc, doc.root());
assert_eq!(doc.text(hits[0]), "one");

That's the first example in the crate's own documentation at the v0.3.1 tag, unchanged.cargo test runs it on every change, so it compiles and passes against the release you'd install. The API reference on docs.rs has the rest, and the getting started guide goes step by step.

Checked against the standards

These counts are the conformance baseline on Parsec's main branch, read when this page was built. Each release has to match or beat them: a suite that passes fewer cases, or fails more, stops the release.

SuitePassFailSkipped
html5lib tokenizer7,04204
html5lib tree construction3,89806
html5lib tree construction, error counts1,8021656
html5lib serializer22901
web-platform-tests DOM selectors1,71908
web-platform-tests CSS selectors4760653
web-platform-tests declarative shadow DOM7,59000

Standards has every suite, where its data comes from, and why the failures and skips are what they are.

Where it stops

  • It parses. It doesn’t run scripts or lay anything out, so nothing a script would add to the page is in the tree.
  • The DOM is narrower than a browser’s: there’s no textContent setter, no cloning or importing between documents, no lookup by id other than a selector, and no mutation callbacks or events.

The HTML page in the docs goes through the API, the limits and the conformance details in full.