RepoCloudflare (Workers AI)Cloudflare (Workers AI)published Sep 9, 2019seen 5d

cloudflare/lol-html

Rust

Open original ↗

Captured source

source ↗
published Sep 9, 2019seen 5dcaptured 8hhttp 200method plain

cloudflare/lol-html

Description: Low output latency streaming HTML parser/rewriter with CSS selector-based API

Language: Rust

License: BSD-3-Clause

Stars: 2007

Forks: 98

Open issues: 31

Created: 2019-09-09T02:20:32Z

Pushed: 2026-06-03T10:50:03Z

Default branch: main

Fork: no

Archived: no

README:

LOL HTML

*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> {
let mut output = vec![];

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

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

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

rewriter.write(b"")?;
rewriter.write(b"
")?;
rewriter.end()?;

assert_eq!(
String::from_utf8(output)?,
r#"
"#
);
Ok(())
}

License

BSD licensed. See the [LICENSE](LICENSE) file for details.