hescape

Crates.iohescape
lib.rshescape
version0.1.0
created_at2026-01-17 08:14:35.483088+00
updated_at2026-01-17 08:14:35.483088+00
descriptionA fast and lightweight HTML escape/unescape library for Rust.
homepagehttps://github.com/devashishdxt/hescape
repositoryhttps://github.com/devashishdxt/hescape
max_upload_size
id2050131
size148,197
Devashish Dixit (devashishdxt)

documentation

README

hescape

A fast and lightweight HTML escape/unescape library for Rust.

This crate provides functions to escape and unescape HTML special characters, which is essential for preventing XSS (Cross-Site Scripting) attacks and correctly rendering user-provided content in HTML documents.

Escaping

The escape function converts the following characters to their HTML entity equivalents:

Character Entity
& &
< &lt;
> &gt;
" &quot;
' &#39;

Example

use hescape::escape;

let input = "<script>alert(\"xss\")</script>";
let escaped = escape(input);
assert_eq!(escaped, "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;");

Unescaping

The unescape function converts HTML entities back to their original characters. It supports:

  • Named references: &amp;, &lt;, &gt;, &quot;, &apos;, and many more.
  • Decimal numeric references: &#39;, &#60;, etc.
  • Hexadecimal numeric references: &#x27;, &#x3C;, etc.

Example

use hescape::unescape;

let input = "&lt;div&gt;Hello &amp; welcome!&lt;/div&gt;";
let unescaped = unescape(input);
assert_eq!(unescaped, "<div>Hello & welcome!</div>");

Writing to a buffer

For performance-sensitive applications, you can use escape_to and unescape_to to write directly to any type implementing core::fmt::Write:

use hescape::escape_to;

let mut buffer = String::new();
escape_to(&mut buffer, "Hello <world>").unwrap();
assert_eq!(buffer, "Hello &lt;world&gt;");

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 4

cargo fmt