Skip to content

LOL HTML

Active
cloudflare/lol-html

Low-latency streaming HTML rewriter and parser with a CSS-selector API — the engine behind HTMLRewriter in Cloudflare Workers.

View on GitHub
Stars
2.1k
Forks
115
Watchers
2.1k
Open issues
40

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] or div.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.

Similar open-source projects, ranked by shared categories and tags.

All Development Tools projects →