15 releases (8 stable)

2.1.0 Dec 4, 2024
2.0.0 Sep 25, 2024
1.2.1 Mar 11, 2024
1.2.0 Sep 13, 2023
0.1.0 Nov 26, 2019

#146 in Parser implementations

Download history 3738/week @ 2024-08-23 4014/week @ 2024-08-30 4240/week @ 2024-09-06 3868/week @ 2024-09-13 4508/week @ 2024-09-20 5774/week @ 2024-09-27 6269/week @ 2024-10-04 5749/week @ 2024-10-11 6571/week @ 2024-10-18 6433/week @ 2024-10-25 7912/week @ 2024-11-01 7375/week @ 2024-11-08 7327/week @ 2024-11-15 7822/week @ 2024-11-22 7624/week @ 2024-11-29 6840/week @ 2024-12-06

31,054 downloads per month
Used in 60 crates (39 directly)

BSD-3-Clause

570KB
13K 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

  • C
  • Lua
  • Go (unofficial, not coming from Cloudflare)
  • Ruby (unofficial, not coming from Cloudflare)

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::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::new()
        },
        |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

~7.5MB
~194K SLoC