#html #escape #unescape #encode #decode

no-std html-escape

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

11 releases

0.2.9 Apr 21, 2021
0.2.8 Apr 20, 2021
0.2.7 Mar 10, 2021
0.2.6 Jul 29, 2020
0.1.0 Jun 26, 2020

#51 in Encoding

Download history 8100/week @ 2021-05-28 8746/week @ 2021-06-04 8851/week @ 2021-06-11 10897/week @ 2021-06-18 10006/week @ 2021-06-25 10161/week @ 2021-07-02 9374/week @ 2021-07-09 10607/week @ 2021-07-16 11080/week @ 2021-07-23 11962/week @ 2021-07-30 11135/week @ 2021-08-06 10061/week @ 2021-08-13 11853/week @ 2021-08-20 10929/week @ 2021-08-27 8316/week @ 2021-09-03 9306/week @ 2021-09-10

18,305 downloads per month
Used in 49 crates (26 directly)

MIT license

165KB
3K 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.

extern crate html_escape;

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.

extern crate html_escape;

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

extern crate html_escape;

assert_eq!("Hello world!", html_escape::decode_html_entities("Hello&#x20;world&#x21;"));
extern crate html_escape;

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