use uuid::Uuid;
pub fn escape_html(s: &str) -> String {
let mut buf = String::with_capacity(s.len());
escape_html_to(s, &mut buf);
buf
}
pub fn escape_html_to(s: &str, buf: &mut String) {
for c in s.chars() {
match c {
'&' => buf.push_str("&"),
'<' => buf.push_str("<"),
'>' => buf.push_str(">"),
'"' => buf.push_str("""),
'\'' => buf.push_str("'"),
_ => buf.push(c),
}
}
}
pub fn escape_html_to_with_indent(s: &str, buf: &mut String, indent: usize) {
if indent == 0 {
escape_html_to(s, buf);
} else {
let pad = " ".repeat(indent);
s.lines().for_each(|o| {
buf.push_str(&pad);
escape_html_to(o, buf);
buf.push('\n');
});
buf.pop();
}
}
pub fn random_id(prefix: &str) -> String {
let uuid = Uuid::new_v4();
format!("{}-{}", prefix, uuid.as_hyphenated())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn escape_html_works() {
let res = escape_html("<a>\"hello\"</a>");
insta::assert_snapshot!(res, @"<a>"hello"</a>");
let res = escape_html("<a>'he \\& llo'</a>");
insta::assert_snapshot!(res, @r"<a>'he \& llo'</a>");
}
}