Function fhtml::escape

source ·
pub fn escape(input: &str) -> String
Expand description

Escapes special HTML characters in a string to prevent XSS attacks or unintended HTML rendering. This function converts:

  • & to &
  • < to &lt;
  • > to &gt;
  • " to &quot;
  • ' to &#39;

§Arguments

  • input - A string slice that may contain special HTML characters.

§Returns

A String with all special HTML characters replaced by their respective HTML entities.

§Examples

let raw_html = "5 < 7 & 5 > 3";
let safe_html = fhtml::escape(raw_html);
assert_eq!(safe_html, "5 &lt; 7 &amp; 5 &gt; 3");