use super::{Lexer, Limits, Options, Token}; /// As a mail reader sets it up. const MAIL: Options<'static> = Options { limits: Limits { max_attrs: 32, max_tag_name: 24 }, drop: &["script", "style", "title", "iframe"], raw_text: &["textarea", "xmp"] }; fn show<'a>(lexer: impl Iterator>) -> Vec { lexer .map(|t| match t { Token::Text(t) => format!("T:{t}"), Token::Start(tag) => { let attrs: Vec = tag.attrs.iter().map(|(n, v)| format!("{n}={v}")).collect(); if attrs.is_empty() { format!("<{}>", tag.name) } else { format!("<{} {}>", tag.name, attrs.join(" ")) } } Token::End(n) => format!(""), }) .collect() } fn tokens(html: &str) -> Vec { show(Lexer::with(html, MAIL)) } #[test] fn text_and_tags() { assert_eq!(tokens("Hi there!"), ["T:Hi ", "", "T:there", "", "T:!"]); assert_eq!(tokens(""), Vec::::new()); assert_eq!(tokens("plain"), ["T:plain"]); assert_eq!(tokens("

"), ["
", "
"]); } #[test] fn attributes_in_every_quoting_style() { assert_eq!(tokens(r#""#), [r#""#]); assert_eq!(tokens("t"), ["", "T:t"]); assert_eq!(tokens(""), [""]); assert_eq!(tokens("

"), ["

"]); // A `>` inside a quoted value does not end the tag. assert_eq!(tokens(r#"t"#), ["b>", "T:t", ""]); } #[test] fn attribute_values_are_decoded_on_demand() { let mut t = Lexer::new(r#""#); let Some(Token::Start(tag)) = t.next() else { panic!("no tag") }; assert_eq!(tag.attr("href").as_deref(), Some("?a=1&b=2©=3")); assert_eq!(tag.raw_attr("href"), Some("?a=1&b=2©=3")); assert_eq!(tag.attr("missing"), None); assert!(tag.has("checked") && !tag.has("check")); assert_eq!(tag.attributes().map(|(n, _)| n).collect::>(), ["HREF", "href", "Checked"]); } #[test] fn a_tag_that_closes_itself_says_so() { let closes = |html: &str| match Lexer::new(html).next() { Some(Token::Start(tag)) => tag.self_close, other => panic!("{other:?}"), }; for yes in ["
", "
", "", "", ""] { assert!(closes(yes), "{yes}"); } // A `/` that ends an unquoted value, or stands anywhere but before the `>`, is not it. for no in ["
", "", "
", "", "
d"), ["T:a < b and c > d"]); assert_eq!(tokens("1<2<3"), ["T:1<2<3"]); assert_eq!(tokens("<"), ["T:<"]); assert_eq!(tokens("x<"), ["T:x<"]); assert_eq!(tokens("<3 <- b"), ["T:a", "T:b"]); } #[test] fn comments_and_declarations_vanish() { assert_eq!(tokens("ab"), ["T:a", "T:b"]); assert_eq!(tokens("

x"), ["

", "T:x"]); assert_eq!(tokens("x"), ["T:x"]); assert_eq!(tokens("abc"), ["T:a", "T:b", "T:c"]); assert_eq!(tokens("shown"), ["T:shown"]); assert_eq!(tokens("shown"), ["T:shown"]); // An unfinished comment swallows the rest. assert_eq!(tokens("a