pub fn strip_html(s: &str) -> String {
let mut result = String::with_capacity(s.len());
let mut in_tag = false;
for ch in s.chars() {
match ch {
'<' => in_tag = true,
'>' => in_tag = false,
_ if !in_tag => result.push(ch),
_ => {}
}
}
html_decode(&result)
}
pub fn strip_cdata(s: &str) -> String {
s.replace("<![CDATA[", "")
.replace("]]>", "")
.trim()
.to_string()
}
pub fn html_decode(s: &str) -> String {
s.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace(""", "\"")
.replace("'", "'")
.replace(" ", " ")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_strip_html_basic() {
assert_eq!(strip_html("<p>Hello <b>world</b></p>"), "Hello world");
}
#[test]
fn test_strip_html_entities() {
assert_eq!(strip_html("& <test>"), "& <test>");
}
#[test]
fn test_strip_html_no_tags() {
assert_eq!(strip_html("plain text"), "plain text");
}
#[test]
fn test_strip_cdata() {
assert_eq!(strip_cdata("<![CDATA[Hello]]>"), "Hello");
}
#[test]
fn test_html_decode() {
assert_eq!(html_decode("& "hi""), "& \"hi\"");
}
}