Overview
LOL HTML (Low Output Latency HTML) is a streaming HTML rewriter and parser with a CSS-selector-based API. It modifies HTML on the fly with minimal buffering, so it can process very large documents quickly and work in memory-constrained environments.
It is the engine behind the HTMLRewriter API in Cloudflare Workers, and it is also available as a standalone Rust crate for any HTML rewriting or analysis task.
Key Features
- Streaming by design: emits output as input arrives instead of building a full DOM
- CSS selectors to target elements, e.g.
a[href]ordiv.content > p - Element, text, comment, and document handlers for rewriting or extracting content
- Low memory footprint suited to proxies and edge environments
- Bindings for C (official), plus community bindings for Lua, Go, and Ruby
Example
Upgrade insecure links to HTTPS while the document streams through:
use lol_html::{element, HtmlRewriter, Settings};
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").unwrap().replace("http:", "https:");
el.set_attribute("href", &href)?;
Ok(())
})),
|chunk: &[u8]| output.extend_from_slice(chunk),
);
Write chunks to the rewriter as they arrive and call end() when the input is complete.
Getting Started
cargo add lol_html
API documentation is published on docs.rs. In Cloudflare Workers you get the same engine through the built-in HTMLRewriter class — no Rust required.
Use Cases
- Injecting scripts, banners, or analytics into proxied pages
- Rewriting links and asset URLs in reverse proxies
- Extracting metadata or text from large HTML documents without loading them fully
License
BSD 3-Clause License.