pub fn escape_text(s: &str) -> String {
quick_xml::escape::escape(s).into_owned()
}
pub fn escape_attr(s: &str) -> String {
quick_xml::escape::escape(s).into_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn escape_text_all_special_chars() {
let input = r#"& < > " '"#;
let out = escape_text(input);
assert_eq!(out, "& < > " '");
}
#[test]
fn escape_attr_all_special_chars() {
let input = r#"& < > " '"#;
let out = escape_attr(input);
assert_eq!(out, "& < > " '");
}
#[test]
fn escape_text_plain_string_unchanged() {
let s = "hello world";
assert_eq!(escape_text(s), s);
}
#[test]
fn escape_attr_plain_string_unchanged() {
let s = "hello world";
assert_eq!(escape_attr(s), s);
}
#[test]
fn escape_text_ampersand_only() {
assert_eq!(escape_text("Acme & Sons"), "Acme & Sons");
}
#[test]
fn escape_text_angle_brackets() {
assert_eq!(escape_text("<tag>"), "<tag>");
}
#[test]
fn escape_attr_double_quote() {
assert_eq!(escape_attr(r#"say "hi""#), "say "hi"");
}
#[test]
fn escape_attr_single_quote() {
assert_eq!(escape_attr("it's"), "it's");
}
}