html-escape
Context-aware HTML encoding and entity decoding for Rust, with no-std support.
Repository Health
Technical Analysis
html-escape is a small, dependency-free Rust crate for encoding special characters in HTML and decoding HTML entities back to plain text. It goes beyond a single escape function by offering context-specific encoders for element text, single- and double-quoted attributes, unquoted attributes, and script/style blocks, so output is escaped exactly as much as each HTML context requires.
The crate is no_std-friendly, allocation-optional via _to_writer/_to_vec/_to_string variants, and pairs its encoders with matching decoders that resolve named and numeric HTML entities. This makes it a practical building block for templating, server-side rendering, and any code that emits user-controlled text into HTML.
What You Get
- Context-specific encode functions for element text, attributes, and script/style blocks
- Matching decode functions that resolve named and numeric HTML entities
- Zero-allocation _to_writer and buffer-reusing _to_string/_to_vec variants
- no_std support by disabling the default std feature
- A dependency-free build with a stable, minimal public API
Common Use Cases
- Escaping user-supplied text before inserting it into an HTML template
- Safely emitting values into HTML attributes in a server-rendered page
- Decoding HTML entities from scraped or stored markup back to plain text
Under The Hood
Architecture - The crate is organized into encode and decode modules (src/encode, src/decode), each split into element, html_entity, and shared helpers, with a thin functions.rs and lib.rs re-exporting the public API. Encoders map characters to their escaped forms per HTML context, while decoders walk input resolving named and numeric entities. Tech Stack - Pure Rust targeting edition 2021 (MSRV 1.58) with zero runtime dependencies; bencher is used only for benchmarks. A std feature (default) gates allocation and writer helpers, enabling a no_std build when disabled. Code Quality - The repo ships integration tests under tests/ (element.rs, html_entity.rs) plus doctests embedded in the README/lib docs, and benches under benches/. Naming is consistent and descriptive across the encode_/decode_ families. API Design - The public surface is a flat set of clearly named free functions with predictable _to_writer/_to_vec/_to_string suffixes, making the ergonomics obvious and boilerplate near zero for the common case.