2 unstable releases

✓ Uses Rust 2018 edition

0.2.0 Mar 6, 2020
0.1.0 Nov 26, 2019

#155 in Parser implementations

Download history 5/week @ 2020-03-16 8/week @ 2020-03-23 12/week @ 2020-03-30 32/week @ 2020-04-06 54/week @ 2020-04-13 71/week @ 2020-04-20 21/week @ 2020-04-27 350/week @ 2020-05-04 582/week @ 2020-05-11 530/week @ 2020-05-18 515/week @ 2020-05-25 783/week @ 2020-06-01 760/week @ 2020-06-08 691/week @ 2020-06-15 1008/week @ 2020-06-22 1491/week @ 2020-06-29

66 downloads per month
Used in 4 crates (2 directly)

BSD-3-Clause and MPL-2.0 licenses

680KB
11K SLoC

LOL HTML

The logo is generated from https://openmoji.org/data/color/svg/1F602.svg by Emily Jäger which is licensed under CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)

Low Output Latency streaming HTML rewriter/parser with CSS-selector based API.

It is designed to modify HTML on the fly with minimal buffering. It can quickly handle very large documents, and operate in environments with limited memory resources. More details can be found in the blog post.

The crate serves as a back-end for the HTML rewriting functionality of Cloudflare Workers, but can be used as a standalone library with a convenient API for a wide variety of HTML rewriting/analysis tasks.

Documentation

https://docs.rs/lol_html/

Bindings for other programming languages

Example

Rewrite insecure hyperlinks:

use lol_html::{element, HtmlRewriter, Settings};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut output = vec![];

    let mut rewriter = HtmlRewriter::try_new(
        Settings {
            element_content_handlers: vec![
                element!("a[href]", |el| {
                    let href = el
                        .get_attribute("href")
                        .expect("href was required")
                        .replace("http:", "https:");

                    el.set_attribute("href", &href)?;

                    Ok(())
                })
            ],
            ..Settings::default()
        },
        |c: &[u8]| output.extend_from_slice(c)
    )?;

    rewriter.write(b"<div><a href=")?;
    rewriter.write(b"http://example.com>")?;
    rewriter.write(b"</a></div>")?;
    rewriter.end()?;

    assert_eq!(
        String::from_utf8(output)?,
        r#"<div><a href="https://example.com"></a></div>"#
    );
    Ok(())
}

License

BSD licensed. See the LICENSE file for details.

Dependencies

~5.5MB
~171K SLoC