tail-fin-common 0.7.4

Shared infrastructure for tail-fin: error types, page_fetch, cookies, CDP helpers
Documentation
/// Strip HTML tags from a string, returning plain text.
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)
}

/// Remove CDATA wrappers from a string.
pub fn strip_cdata(s: &str) -> String {
    s.replace("<![CDATA[", "")
        .replace("]]>", "")
        .trim()
        .to_string()
}

/// Decode common HTML entities.
pub fn html_decode(s: &str) -> String {
    s.replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&#39;", "'")
        .replace("&nbsp;", " ")
}

#[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("&amp; &lt;test&gt;"), "& <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("&amp; &quot;hi&quot;"), "& \"hi\"");
    }
}