lol-html
A low-latency streaming HTML parser and rewriter for Rust with a CSS-selector-based API.
Repository Health
Technical Analysis
lol-html (Low Output Latency HTML) is a streaming HTML rewriter and parser for Rust that modifies HTML on the fly with minimal buffering. It processes documents as a stream of chunks, so it can transform very large pages quickly and run in memory-constrained environments.
You describe transformations declaratively by attaching content handlers to CSS selectors, and the rewriter invokes them as matching elements and text pass through. Built by Cloudflare as the engine behind the HTMLRewriter API in Cloudflare Workers, it also ships C and JavaScript bindings and is usable as a standalone library for a wide range of HTML rewriting and analysis tasks.
What You Get
- A streaming
HtmlRewriterthat transforms HTML chunk by chunk - A declarative CSS-selector-based handler API via
element!andtext!macros - Element, text, comment, and document-level content handlers
- Very low memory usage with minimal buffering for large documents
- C and JavaScript bindings plus community bindings for other languages
Common Use Cases
- Injecting, removing, or rewriting elements and attributes in an HTML stream
- Rewriting links or resource URLs (e.g. HTTP to HTTPS) at the edge
- Sanitizing or transforming HTML responses in a proxy or CDN
- Extracting or analyzing content from large HTML documents efficiently
Under The Hood
Architecture - The rewriter is fed input via write()/end() and runs a streaming tokenizer that emits SAX-like events. A selector-matching engine maps CSS selectors from the Settings to registered content handlers; when a token matches, the corresponding element!/text! handler runs and can mutate, replace, insert, or remove content. Because it never materializes a full DOM, only the minimal state needed to satisfy active selectors is buffered. The core lives in src/, with c-api/ and js-api/ exposing the same engine to other runtimes.
Tech Stack - Pure Rust with a strong performance focus: it includes benches/, a fuzz/ harness, and extensive tests/. The C API enables embedding in non-Rust hosts, and the JS API backs Cloudflare Workers’ HTMLRewriter.
Code Quality - The project is very actively maintained by Cloudflare, with fuzzing, benchmarking, a detailed CHANGELOG, and a DEVELOPING guide. Its use in production at Cloudflare’s edge and its extensive test/fuzz coverage signal high reliability.
API Design - The handler-plus-selector model is ergonomic and hard to misuse: you register element!("a[href]", |el| { ... })-style closures and the library handles streaming mechanics. This declarative surface keeps common rewriting tasks concise while supporting complex transformations.