Crate html_escape

Source
Expand description

§HTML Escape

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

Statics§

NAMED_ENTITIES
The table of HTML named entities ordered by the names.

Functions§

decode_html_entities
Decode html entities in a given string.
decode_html_entities_to_string
Decode html entities in a given string to a mutable String reference and return the decoded string slice.
decode_html_entities_to_vec
Decode html entities in a given string to a mutable Vec<u8> reference and return the decoded data slice.
decode_html_entities_to_writer
Decode html entities in a given string to a writer.
decode_script
Decode text from the <script> element.
decode_script_double_quoted_text
Decode text from a double quoted text in the <script> element.
decode_script_double_quoted_text_to_string
Write text from a double quoted text in the <script> element to a mutable String reference and return the encoded string slice.
decode_script_double_quoted_text_to_vec
Write text from a double quoted text in the <script> element to a mutable Vec<u8> reference and return the encoded data slice.
decode_script_double_quoted_text_to_writer
Write text from a double quoted text in the <script> element to a writer.
decode_script_quoted_text
Decode text from a quoted text in the <script> element.
decode_script_quoted_text_to_string
Write text from a quoted text in the <script> element to a mutable String reference and return the encoded string slice.
decode_script_quoted_text_to_vec
Write text from a quoted text in the <script> element to a mutable Vec<u8> reference and return the encoded data slice.
decode_script_quoted_text_to_writer
Write text from a quoted text in the <script> element to a writer.
decode_script_single_quoted_text
Decode text from a single quoted text in the <script> element.
decode_script_single_quoted_text_to_string
Write text from a single quoted text in the <script> element to a mutable String reference and return the encoded string slice.
decode_script_single_quoted_text_to_vec
Write text from a single quoted text in the <script> element to a mutable Vec<u8> reference and return the encoded data slice.
decode_script_single_quoted_text_to_writer
Write text from a single quoted text in the <script> element to a writer.
decode_script_to_string
Write text from the <script> element to a mutable String reference and return the encoded string slice.
decode_script_to_vec
Write text from the <script> element to a mutable Vec<u8> reference and return the encoded data slice.
decode_script_to_writer
Write text from the <script> element to a writer.
decode_style
Decode text from the <style> element.
decode_style_double_quoted_text
Decode text from a double quoted text in the <style> element.
decode_style_double_quoted_text_to_string
Write text from a double quoted text in the <style> element to a mutable String reference and return the encoded string slice.
decode_style_double_quoted_text_to_vec
Write text from a double quoted text in the <style> element to a mutable Vec<u8> reference and return the encoded data slice.
decode_style_double_quoted_text_to_writer
Write text from a double quoted text in the <style> element to a writer.
decode_style_quoted_text
Decode text from a quoted text in the <style> element.
decode_style_quoted_text_to_string
Write text from a quoted text in the <style> element to a mutable String reference and return the encoded string slice.
decode_style_quoted_text_to_vec
Write text from a quoted text in the <style> element to a mutable Vec<u8> reference and return the encoded data slice.
decode_style_quoted_text_to_writer
Write text from a quoted text in the <style> element to a writer.
decode_style_single_quoted_text
Decode text from a single quoted text in the <style> element.
decode_style_single_quoted_text_to_string
Write text from a single quoted text in the <style> element to a mutable String reference and return the encoded string slice.
decode_style_single_quoted_text_to_vec
Write text from a single quoted text in the <style> element to a mutable Vec<u8> reference and return the encoded data slice.
decode_style_single_quoted_text_to_writer
Write text from a single quoted text in the <style> element to a writer.
decode_style_to_string
Write text from the <style> element to a mutable String reference and return the encoded string slice.
decode_style_to_vec
Write text from the <style> element to a mutable Vec<u8> reference and return the encoded data slice.
decode_style_to_writer
Write text from the <style> element to a writer.
encode_double_quoted_attribute
Encode text used in a double-quoted attribute.
encode_double_quoted_attribute_to_string
Write text used in a double-quoted attribute to a mutable String reference and return the encoded string slice.
encode_double_quoted_attribute_to_vec
Write text used in a double-quoted attribute to a mutable Vec<u8> reference and return the encoded data slice.
encode_double_quoted_attribute_to_writer
Write text used in a double-quoted attribute to a writer.
encode_quoted_attribute
Encode text used in a quoted attribute.
encode_quoted_attribute_to_string
Write text used in a quoted attribute to a mutable String reference and return the encoded string slice.
encode_quoted_attribute_to_vec
Write text used in a quoted attribute to a mutable Vec<u8> reference and return the encoded data slice.
encode_quoted_attribute_to_writer
Write text used in a quoted attribute to a writer.
encode_safe
Encode text to prevent special characters functioning.
encode_safe_to_string
Encode text to prevent special characters functioning and write it to a mutable String reference and return the encoded string slice.
encode_safe_to_vec
Encode text to prevent special characters functioning and write it to a mutable Vec<u8> reference and return the encoded data slice.
encode_safe_to_writer
Encode text to prevent special characters functioning and write it to a writer.
encode_script
Encode text used in the <script> element.
encode_script_double_quoted_text
Encode text used in a double quoted text in the <script> element.
encode_script_double_quoted_text_to_string
Write text used in a double quoted text in the <script> element to a mutable String reference and return the encoded string slice.
encode_script_double_quoted_text_to_vec
Write text used in a double quoted text in the <script> element to a mutable Vec<u8> reference and return the encoded data slice.
encode_script_double_quoted_text_to_writer
Write text used in a double quoted text in the <script> element to a writer.
encode_script_quoted_text
Encode text used in a quoted text in the <script> element.
encode_script_quoted_text_to_string
Write text used in a quoted text in the <script> element to a mutable String reference and return the encoded string slice.
encode_script_quoted_text_to_vec
Write text used in a quoted text in the <script> element to a mutable Vec<u8> reference and return the encoded data slice.
encode_script_quoted_text_to_writer
Write text used in a quoted text in the <script> element to a writer.
encode_script_single_quoted_text
Encode text used in a single quoted text in the <script> element.
encode_script_single_quoted_text_to_string
Write text used in a single quoted text in the <script> element to a mutable String reference and return the encoded string slice.
encode_script_single_quoted_text_to_vec
Write text used in a single quoted text in the <script> element to a mutable Vec<u8> reference and return the encoded data slice.
encode_script_single_quoted_text_to_writer
Write text used in a single quoted text in the <script> element to a writer.
encode_script_to_string
Write text used in the <script> element to a mutable String reference and return the encoded string slice.
encode_script_to_vec
Write text used in the <script> element to a mutable Vec<u8> reference and return the encoded data slice.
encode_script_to_writer
Write text used in the <script> element to a writer.
encode_single_quoted_attribute
Encode text used in a single-quoted attribute.
encode_single_quoted_attribute_to_string
Write text used in a single-quoted attribute to a mutable String reference and return the encoded string slice.
encode_single_quoted_attribute_to_vec
Write text used in a single-quoted attribute to a mutable Vec<u8> reference and return the encoded data slice.
encode_single_quoted_attribute_to_writer
Write text used in a single-quoted attribute to a writer.
encode_style
Encode text used in the <style> element.
encode_style_double_quoted_text
Encode text used in a double quoted text in the <style> element.
encode_style_double_quoted_text_to_string
Write text used in a double quoted text in the <style> element to a mutable String reference and return the encoded string slice.
encode_style_double_quoted_text_to_vec
Write text used in a double quoted text in the <style> element to a mutable Vec<u8> reference and return the encoded data slice.
encode_style_double_quoted_text_to_writer
Write text used in a double quoted text in the <style> element to a writer.
encode_style_quoted_text
Encode text used in a quoted text in the <style> element.
encode_style_quoted_text_to_string
Write text used in a quoted text in the <style> element to a mutable String reference and return the encoded string slice.
encode_style_quoted_text_to_vec
Write text used in a quoted text in the <style> element to a mutable Vec<u8> reference and return the encoded data slice.
encode_style_quoted_text_to_writer
Write text used in a quoted text in the <style> element to a writer.
encode_style_single_quoted_text
Encode text used in a single quoted text in the <style> element.
encode_style_single_quoted_text_to_string
Write text used in a single quoted text in the <style> element to a mutable String reference and return the encoded string slice.
encode_style_single_quoted_text_to_vec
Write text used in a single quoted text in the <style> element to a mutable Vec<u8> reference and return the encoded data slice.
encode_style_single_quoted_text_to_writer
Write text used in a single quoted text in the <style> element to a writer.
encode_style_to_string
Write text used in the <style> element to a mutable String reference and return the encoded string slice.
encode_style_to_vec
Write text used in the <style> element to a mutable Vec<u8> reference and return the encoded data slice.
encode_style_to_writer
Write text used in the <style> element to a writer.
encode_text
Encode text used as regular HTML text.
encode_text_minimal
Encode text used as regular HTML text.
encode_text_minimal_to_string
Write text used as regular HTML text to a mutable String reference and return the encoded string slice.
encode_text_minimal_to_vec
Write text used as regular HTML text to a mutable Vec<u8> reference and return the encoded data slice.
encode_text_minimal_to_writer
Write text used as regular HTML text to a writer.
encode_text_to_string
Write text used as regular HTML text to a mutable String reference and return the encoded string slice.
encode_text_to_vec
Write text used as regular HTML text to a mutable Vec<u8> reference and return the encoded data slice.
encode_text_to_writer
Write text used as regular HTML text to a writer.
encode_unquoted_attribute
Encode text used in an unquoted attribute. Except for alphanumeric characters, escape all characters which are less than 128.
encode_unquoted_attribute_to_string
Write text used in an unquoted attribute to a mutable String reference and return the encoded string slice. Except for alphanumeric characters, escape all characters which are less than 128.
encode_unquoted_attribute_to_vec
Write text used in an unquoted attribute to a mutable Vec<u8> reference and return the encoded data slice. Except for alphanumeric characters, escape all characters which are less than 128.
encode_unquoted_attribute_to_writer
Write text used in an unquoted attribute to a writer. Except for alphanumeric characters, escape all characters which are less than 128.