docgen_core/util.rs
1//! Small shared helpers used by the AST passes.
2
3/// Escape the five HTML-significant characters so untrusted text (math source,
4/// diagram source) can be embedded inside generated markup safely.
5pub fn escape_html(s: &str) -> String {
6 let mut o = String::with_capacity(s.len());
7 for c in s.chars() {
8 match c {
9 '&' => o.push_str("&"),
10 '<' => o.push_str("<"),
11 '>' => o.push_str(">"),
12 '"' => o.push_str("""),
13 '\'' => o.push_str("'"),
14 _ => o.push(c),
15 }
16 }
17 o
18}
19
20#[cfg(test)]
21mod tests {
22 use super::*;
23
24 #[test]
25 fn escapes_angle_brackets_and_amp() {
26 assert_eq!(escape_html("a<b>&\"c\""), "a<b>&"c"");
27 }
28}