57 releases

0.27.0 Mar 22, 2024
0.26.0 Mar 27, 2022
0.25.2 Mar 24, 2022
0.25.1 Oct 25, 2019
0.2.1 Jul 31, 2015

#23 in Parser implementations

Download history 68037/week @ 2023-12-23 91258/week @ 2023-12-30 105122/week @ 2024-01-06 105979/week @ 2024-01-13 109996/week @ 2024-01-20 116232/week @ 2024-01-27 107895/week @ 2024-02-03 116336/week @ 2024-02-10 115567/week @ 2024-02-17 132931/week @ 2024-02-24 132228/week @ 2024-03-02 132538/week @ 2024-03-09 133351/week @ 2024-03-16 141696/week @ 2024-03-23 149645/week @ 2024-03-30 95400/week @ 2024-04-06

538,292 downloads per month
Used in 1,086 crates (139 directly)

MIT/Apache

360KB
8K SLoC

html5ever

Build Status crates.io

API Documentation

html5ever is an HTML parser developed as part of the Servo project.

It can parse and serialize HTML according to the WHATWG specs (aka "HTML5"). However, there are some differences in the actual behavior currently, most of which are documented in the bug tracker. html5ever passes all tokenizer tests from html5lib-tests, with most tree builder tests outside of the unimplemented features. The goal is to pass all html5lib tests, while also providing all hooks needed by a production web browser, e.g. document.write.

Note that the HTML syntax is very similar to XML. For correct parsing of XHTML, use an XML parser (That said, many XHTML documents in the wild are serialized in an HTML-compatible form).

html5ever is written in Rust, therefore it avoids the notorious security problems that come along with using C. Being built with Rust also makes the library come with the high-grade performance you would expect from an HTML parser written in C. html5ever is basically a C HTML parser, but without needing a garbage collector or other heavy runtime processes.

Getting started in Rust

Add html5ever as a dependency in your Cargo.toml file:

[dependencies]
html5ever = "0.26"

You should also take a look at examples/html2html.rs, examples/print-rcdom.rs, and the API documentation.

Getting started in other languages

Bindings for Python and other languages are much desired.

Working on html5ever

To fetch the test suite, you need to run

git submodule update --init

Run cargo doc in the repository root to build local documentation under target/doc/.

Details

html5ever uses callbacks to manipulate the DOM, therefore it does not provide any DOM tree representation.

html5ever exclusively uses UTF-8 to represent strings. In the future it will support other document encodings (and UCS-2 document.write) by converting input.

The code is cross-referenced with the WHATWG syntax spec, and eventually we will have a way to present code and spec side-by-side.

html5ever builds against the official stable releases of Rust, though some optimizations are only supported on nightly releases.

Dependencies

~0.8–7MB
~23K SLoC