html_generator::seo

Function escape_html

Source
pub fn escape_html(s: &str) -> Cow<'_, str>
Expand description

Escapes HTML special characters in a string.

This function replaces special characters with their HTML entity equivalents:

  • & becomes &amp;
  • < becomes &lt;
  • > becomes &gt;
  • " becomes &quot;
  • ' becomes &#x27;

§Arguments

  • s - The string to escape

§Returns

Returns a Cow<str> containing either the original string if no escaping was needed, or a new string with escaped characters.

§Examples

use html_generator::seo::escape_html;

let input = r#"<script>alert("Hello & goodbye")</script>"#;
let escaped = escape_html(input);
assert_eq!(
    escaped,
    r#"&lt;script&gt;alert(&quot;Hello &amp; goodbye&quot;)&lt;/script&gt;"#
);