pub fn escape_xml(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
.replace('\'', "'")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_escapes_all_five_special_chars() {
assert_eq!(
escape_xml(r#"<a href="x" b='y'> & </a>"#),
"<a href="x" b='y'> & </a>"
);
}
#[test]
fn test_amp_first_to_avoid_double_escape() {
assert_eq!(escape_xml("<&>"), "<&>");
}
#[test]
fn test_passthrough_for_safe_chars() {
assert_eq!(escape_xml("hello world 123"), "hello world 123");
}
#[test]
fn test_empty_string() {
assert_eq!(escape_xml(""), "");
}
}