#escaping #html #encode #html-text #encode-decode #decode #generate-html

no-std html-escape

This library is for encoding/escaping special characters in HTML and decoding/unescaping HTML entities as well

15 releases

0.2.13 Dec 23, 2022
0.2.12 Nov 2, 2022
0.2.11 Mar 17, 2022
0.2.9 Apr 21, 2021
0.1.0 Jun 26, 2020

#22 in Encoding

Download history 61638/week @ 2024-07-17 60100/week @ 2024-07-24 67211/week @ 2024-07-31 68440/week @ 2024-08-07 68753/week @ 2024-08-14 70539/week @ 2024-08-21 66294/week @ 2024-08-28 72110/week @ 2024-09-04 64020/week @ 2024-09-11 65732/week @ 2024-09-18 76940/week @ 2024-09-25 81180/week @ 2024-10-02 76105/week @ 2024-10-09 78427/week @ 2024-10-16 81399/week @ 2024-10-23 92377/week @ 2024-10-30

344,172 downloads per month
Used in 649 crates (182 directly)

MIT license

140KB
4K SLoC

HTML Escape

CI

This library is for encoding/escaping special characters in HTML and decoding/unescaping HTML entities as well.

Usage

Encoding

This crate provides some encode_* functions to encode HTML text in different situations.

For example, to put a text between a start tag <foo> and an end tag </foo>, use the encode_text function to escape every &, <, and > in the text.

assert_eq!("a &gt; b &amp;&amp; a &lt; c", html_escape::encode_text("a > b && a < c"));

The functions suffixed with _to_writer, _to_vec or _to_string are useful to generate HTML.

let mut html = String::from("<input value=");
assert_eq!("Hello&#x20;world&#x21;", html_escape::encode_unquoted_attribute_to_string("Hello world!", &mut html));
html.push_str(" placeholder=\"");
assert_eq!("The default value is &quot;Hello world!&quot;.", html_escape::encode_double_quoted_attribute_to_string("The default value is \"Hello world!\".", &mut html));
html.push_str("\"/><script>alert('");
assert_eq!(r"<script>\'s end tag is <\/script>", html_escape::encode_script_single_quoted_text_to_string("<script>'s end tag is </script>", &mut html));
html.push_str("');</script>");

assert_eq!("<input value=Hello&#x20;world&#x21; placeholder=\"The default value is &quot;Hello world!&quot;.\"/><script>alert(\'<script>\\\'s end tag is <\\/script>\');</script>", html);

Decoding

assert_eq!("Hello world!", html_escape::decode_html_entities("Hello&#x20;world&#x21;"));
assert_eq!("alert('<script></script>);'", html_escape::decode_script(r"alert('<script><\/script>);'"));

No Std

Disable the default features to compile this crate without std.

[dependencies.html-escape]
version = "*"
default-features = false

Benchmark

cargo bench

Crates.io

https://crates.io/crates/html-escape

Documentation

https://docs.rs/html-escape

License

MIT

Dependencies