Skip to content

escape_html

Escapes the HTML special characters &, <, >, " and '.

Returns the input borrowed, without allocating, when there is nothing to escape. Use it to embed untrusted text in HTML text nodes or quoted attribute values.

use helpers4::string::escape_html;

Cargo feature string (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features string

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.5", default-features = false, features = ["string"] }
pub fn escape_html(s: &str) -> Cow<'_, str>
ParameterTypeDescription
s&strThe text to escape.

Cow<'_, str>

use helpers4::string::escape_html;

assert_eq!(
    escape_html("<script>alert(\"xss\")</script>"),
    "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;"
);
assert_eq!(escape_html("It's a <test> & more"), "It&#39;s a &lt;test&gt; &amp; more");
assert_eq!(escape_html("plain"), "plain");

src/string/escape_html.rs