Skip to content
parsec-htmlPublic

About

The parsing foundation for the web, in Rust: HTML, CSS, JavaScript, XML, URLs and encodings, each its own crate, checked against each standard's own suite. From Rust, Python, JavaScript and C.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

4 stars

Watchers

0 watching

Forks

Parsec: the parsing foundation for the web

The parsing foundation for the web, in Rust.

crates.io PyPI npm docs.rs CI OpenSSF Best Practices OpenSSF Scorecard

Website · Get started · Docs · Benchmarks · Roadmap

Parsec reads HTML, CSS, JavaScript, XML, URLs and text encodings the way the standards say a browser should, and hands you the result. Each language is its own crate with its own tree, so if all you want is CSS you don't compile an HTML tokenizer, and if all you want is HTML you never build a JavaScript parser. It runs from Rust, Python, JavaScript and C.

It's built for the pages people actually send: broken markup, a <meta> that lies about the encoding, 30 MB tables, inputs written to make a parser crawl. Every part is checked against the suite its standard publishes, and every Monday it's run against a slice of the real web.

The parsers

What you get Crate Checked against
HTML The HTML Standard's tokenizer and tree builder, fragments, streaming, CSS selectors Level 4, declarative shadow DOM, editing and serializing parsec-web-core html5lib tree construction: 3,898 pass, none fail
CSS Syntax Level 3, a typed value for all 822 properties, media queries and the cascade through to computed values parsec-web-css web-platform-tests CSS parsing: 14,758 pass, 135 don't yet
JavaScript ECMAScript 2025 scripts and modules to a tree with spans, ESTree JSON, scopes, module records, and page state read without running anything parsec-js test262: 52,782 pass, none fail
XML XML 1.0 with namespaces, not validating, with a span on every node, and XPath 1.0 over it parsec-xml W3C XML conformance: 1,915 pass, none fail
URL The WHATWG URL Standard: parsing, setters, URLSearchParams and IDNA parsec-url web-platform-tests URL: 4,565 pass, none fail
Encoding A page's encoding found the way a browser finds it, then decoded, whole or as the bytes arrive parsec-web-encoding WPT legacy multi-byte: 62,352 pass, none fail

XPath 1.0 has its own crate too, parsec-xpath, and parsec-web is the one most people install: HTML, CSS, encodings, URLs and XPath together. Everything in the table is released, in 0.3.1. Those counts come from the 35 suites Parsec runs; the standards page has all of them, 679,730 passing tests in all, with what each one skips and why.

Try it

The same page in each language: three books, one sold, and the two that are left.

cargo add parsec-web
use parsec::css::SelectorList;

fn main() {
    let doc = parsec::parse(
        r#"<ul>
  <li class="book"><a href="/p/1">Dune</a></li>
  <li class="book sold"><a href="/p/2">Emma</a></li>
  <li class="book"><a href="/p/3">Ulysses</a></li>
</ul>"#,
        false,
    );
    let in_stock = SelectorList::parse("li.book:not(.sold) a").unwrap();
    for a in in_stock.select(&doc, doc.root()) {
        let href = doc.attrs(a).find(|&(_, name, _)| name == "href").map(|(_, _, value)| value);
        println!("{} {}", doc.text(a), href.unwrap_or(""));
    }
}

It prints Dune /p/1 and Ulysses /p/3. The package is parsec-web and the crate you use is parsec.

Python
pip install parsec-web
from parsec_html import HTMLParser

page = HTMLParser("""<ul>
  <li class="book"><a href="/p/1">Dune</a></li>
  <li class="book sold"><a href="/p/2">Emma</a></li>
  <li class="book"><a href="/p/3">Ulysses</a></li>
</ul>""")

for a in page.css("li.book:not(.sold) a"):
    print(a.text(), a.attributes["href"])

The API is selectolax's, so moving over is mostly a change of import. You install parsec-web and import parsec_html.

JavaScript, in Node or a browser
npm install parsec-web
import { parse } from "parsec-web";

const doc = parse(`<ul>
  <li class="book"><a href="/p/1">Dune</a></li>
  <li class="book sold"><a href="/p/2">Emma</a></li>
  <li class="book"><a href="/p/3">Ulysses</a></li>
</ul>`);

for (const a of doc.select("li.book:not(.sold) a")) {
  console.log(a.textContent, a.getAttribute("href"));
}

On Linux, macOS and Windows, Node gets a native build; anywhere else it falls back to WebAssembly.

C

Each release has a package per platform with the shared and static library, parsec.h, and pkg-config and CMake files.

#include <stdio.h>
#include <string.h>
#include <parsec.h>

static int print_text(parsec_node a, void *doc) {
    char *text = parsec_text(doc, a, NULL);
    puts(text);
    parsec_string_free(text);
    return 0;
}

int main(void) {
    const char *html = "<ul><li class=book><a href=/p/1>Dune</a>"
                       "<li class='book sold'><a href=/p/2>Emma</a>"
                       "<li class=book><a href=/p/3>Ulysses</a></ul>";
    const char *css = "li.book:not(.sold) a";
    parsec_document *doc;
    parsec_selector *sel;
    if (parsec_parse(html, strlen(html), 0, &doc) != PARSEC_OK) return 1;
    if (parsec_selector_compile(css, strlen(css), &sel, NULL) != PARSEC_OK) return 1;
    parsec_select(doc, sel, parsec_root(), print_text, doc);
    parsec_selector_free(sel);
    parsec_document_free(doc);
    return 0;
}

Getting started goes from there, and Install has every platform and feature flag.

How fast

On 13 real pages, 24 MB of Wikipedia, MDN, GitHub, the BBC, the HTML spec and others, Parsec parses 362 MB a second. Lexbor's C manages 242 and html5ever 83, and Parsec is ahead of both on every page. Across 1,746 production pages fetched on 29 November 2026, 563 MB, it's 1,007 MB a second to Lexbor's 543, with all three building the same number of elements on every page. Its selectors are 2.9 times as fast as Lexbor's over the same pages.

Those are one machine's numbers, an Apple M2 Max, taken on 29 November 2026 and committed with the command that made them in bench/results. Parsec doesn't win everywhere: oxc parses the smallest inline scripts on those pages faster, and the benchmarks say by how much. Every figure comes with the command that made it, so you can run them on yours; the method takes about five minutes to try.

Tested against the web, not only the specs

Spec suites tell you a parser is right by the standard. They can't tell you it survives the pages a crawler meets. That's what Netweir is for: it's the crawling and extraction framework the HTML parser was first written for, and since Netweir 0.12.0 it builds on Parsec's crates from crates.io like anyone else. Every Monday, Netweir's CI fetches 300 of a pinned list of 2,709 production URLs, a different slice each week, and parses each page with Parsec and with html5ever. If Parsec panics, hangs, or builds a tree html5ever disagrees with, the run fails, and the page that broke it is cut down to a test here. Netweir and Parsec has the whole loop.

Safe with input you didn't write

Parsec is Rust, with one unsafe block in the parser and its reasoning written beside it; the C API's unsafe is all at the boundary. Outside that, the compiler rules out use-after-free and buffer overflows. It can't rule out a page that takes forever, so you set the limits: a time budget per parse, a cap on input size, and a maximum tree depth. The XML parser stops billion laughs with an error and never fetches anything. Eight fuzz targets run every night across the HTML, CSS, JavaScript and XML parsers. Security has the threat model, and SECURITY.md says how to report a problem privately.

Coming from Lexbor

If you use Lexbor today, through selectolax, PHP's Dom\HTMLDocument, nokolexbor or lazy_html, the HTML parser was built with you in mind: positions on every node, parse errors with the spec's names, XPath, documents you can share across threads, and a node that reads as nothing after it's removed rather than a dangling pointer. Parsec and Lexbor compares the two fairly, including where Lexbor is ahead, and Moving from Lexbor maps its C API call by call.

What's next

0.4.0 is the next release that can change the API, and the main thing waiting for it is a faster parsec-js lexer for small scripts (#190). ROADMAP.md has every milestone, and the roadmap page shows where each one stands. CHANGELOG.md has what changed in each release.

Working on it

git clone --recurse-submodules https://github.com/parsec-html/parsec
cd parsec
cargo test

CONTRIBUTING.md explains how a change gets in. The most useful thing you can send is a page Parsec gets wrong. Questions go to Discussions, bugs to Issues, and the documentation lives at docs.parsec.tools, where every page has a link to edit it.

Licence

Apache-2.0. See LICENSE and NOTICE.

About

The parsing foundation for the web, in Rust: HTML, CSS, JavaScript, XML, URLs and encodings, each its own crate, checked against each standard's own suite. From Rust, Python, JavaScript and C.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages