pub fn escape_xml(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
.replace('\'', "'")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_escape_xml_ampersand() {
assert_eq!(escape_xml("A & B"), "A & B");
}
#[test]
fn test_escape_xml_angle_brackets() {
assert_eq!(escape_xml("<tag>"), "<tag>");
}
#[test]
fn test_escape_xml_quotes() {
assert_eq!(escape_xml(r#"say "hello""#), "say "hello"");
}
#[test]
fn test_escape_xml_apostrophe() {
assert_eq!(escape_xml("it's"), "it's");
}
#[test]
fn test_escape_xml_no_special_chars() {
assert_eq!(escape_xml("hello world"), "hello world");
}
#[test]
fn test_escape_xml_all_special() {
assert_eq!(
escape_xml(r#"<a & 'b' "c">"#),
"<a & 'b' "c">"
);
}
}