#[cfg(test)] mod parser_tests { use html2md_rs::{ parser::{safe_parse_html, MalformedTagError, ParseHTMLError}, structs::{AttributeValues, Attributes, Node, NodeType::*}, }; #[test] fn parse_simple_div_with_text() { let input = "
hello
".to_string(); let expected = Node { tag_name: Some(Div), value: None, attributes: None, within_special_tag: None, children: vec![Node { tag_name: Some(Text), value: Some("hello".to_string()), attributes: None, within_special_tag: None, children: vec![], }], }; assert_eq!(safe_parse_html(input).unwrap(), expected); } #[test] fn parse_multiple_headers() { let input = "

hello

world

".to_string(); let expected = Node { tag_name: None, value: None, attributes: None, within_special_tag: None, children: vec![ Node { tag_name: Some(H1), value: None, attributes: None, within_special_tag: None, children: vec![Node { tag_name: Some(Text), value: Some("hello".to_string()), attributes: None, within_special_tag: None, children: vec![], }], }, Node { tag_name: Some(H2), value: None, attributes: None, within_special_tag: None, children: vec![Node { tag_name: Some(Text), value: Some("world".to_string()), attributes: None, within_special_tag: None, children: vec![], }], }, ], }; assert_eq!(safe_parse_html(input).unwrap(), expected); } #[test] fn parse_unordered_list() { let input = "".to_string(); let expected = Node { tag_name: Some(Ul), children: vec![ Node { tag_name: Some(Li), within_special_tag: Some(vec![Ul]), children: vec![Node { tag_name: Some(Text), within_special_tag: Some(vec![Ul]), value: Some("hello".to_string()), ..Default::default() }], ..Default::default() }, Node { tag_name: Some(Li), within_special_tag: Some(vec![Ul]), children: vec![Node { tag_name: Some(Text), within_special_tag: Some(vec![Ul]), value: Some("world".to_string()), ..Default::default() }], ..Default::default() }, ], ..Default::default() }; assert_eq!(safe_parse_html(input).unwrap(), expected); } #[test] fn parse_ordered_list() { let input = "
  1. hello
  2. world
".to_string(); let expected = Node { tag_name: Some(Ol), children: vec![ Node { tag_name: Some(Li), within_special_tag: Some(vec![Ol]), children: vec![Node { tag_name: Some(Text), value: Some("hello".to_string()), within_special_tag: Some(vec![Ol]), ..Default::default() }], ..Default::default() }, Node { tag_name: Some(Li), within_special_tag: Some(vec![Ol]), children: vec![Node { tag_name: Some(Text), value: Some("world".to_string()), within_special_tag: Some(vec![Ol]), ..Default::default() }], ..Default::default() }, ], ..Default::default() }; assert_eq!(safe_parse_html(input).unwrap(), expected); } #[test] fn self_closing_div() { let input = "
".to_string(); let expected = Node { tag_name: Some(Div), value: None, attributes: None, within_special_tag: None, children: vec![], }; assert_eq!(safe_parse_html(input).unwrap(), expected); } #[test] fn with_self_closing_div() { let input = "
hello
" .to_string(); let expected = Node { tag_name: None, value: None, attributes: None, within_special_tag: None, children: vec![ Node { tag_name: Some(Div), value: None, attributes: None, within_special_tag: None, children: vec![Node { tag_name: Some(Text), value: Some("hello".to_string()), attributes: None, within_special_tag: None, children: vec![], }], }, Node { tag_name: Some(Div), value: None, attributes: None, within_special_tag: None, children: vec![], }, ], }; assert_eq!(safe_parse_html(input).unwrap(), expected); } #[test] fn missing_closing_bracket() { let input = "
hello
".to_string(); let mut attributes = Attributes::new(); attributes.insert("class".to_string(), AttributeValues::from("hello=world")); let expected = Node { tag_name: Some(Div), attributes: Some(attributes), ..Default::default() }; assert_eq!(safe_parse_html(input).unwrap(), expected); } // https://github.com/izyuumi/html2md-rs/issues/21 #[test] fn issue_21() { let input = "".to_string(); let mut attributes = Attributes::new(); attributes.insert( "http-equiv".to_string(), AttributeValues::from("content-type"), ); attributes.insert( "content".to_string(), AttributeValues::from("text/html; charset=utf-8"), ); let expected = Node { tag_name: Some(Meta), attributes: Some(attributes), ..Default::default() }; assert_eq!(safe_parse_html(input).unwrap(), expected); } // https://github.com/izyuumi/html2md-rs/issues/23 #[test] fn issue_23() { let input = "
".to_string(); let mut attributes = Attributes::new(); attributes.insert("id".to_string(), AttributeValues::from("search")); attributes.insert("role".to_string(), AttributeValues::from("search")); attributes.insert("action".to_string(), AttributeValues::from("/search")); let expected = Node { tag_name: Some(Unknown("form".to_string())), attributes: Some(attributes), ..Default::default() }; assert_eq!(safe_parse_html(input).unwrap(), expected); } }