use html_editor::{operation::*, Element}; use html_editor::{parse, Node}; const HTML: &str = r#"
Hello
"#; const INSERTED_HTML: &str = r#"Hello
"#; const REMOVED_HTML: &str = r#"Hello
"#; const REPLACED_HTML: &str = r#"Hello World!
"#; #[test] fn insert() { let body_selector = Selector::from("body"); let script = Node::new_element( "script", Vec::new(), vec![Node::Text(r#"console.log("Hello World")"#.to_string())], ); let html = parse(HTML) .unwrap() .insert_to(&body_selector, script) .html(); assert_eq!(html, INSERTED_HTML); } #[test] fn remove() { let meta_selector = Selector::from("meta"); let html = parse(HTML).unwrap().remove_by(&meta_selector).html(); assert_eq!(html, REMOVED_HTML); } #[test] fn replace() { let p_selector = Selector::from("p"); let html = parse(HTML) .unwrap() .replace_with(&p_selector, |p| { let new_text = format!("{} World!", p.children[0].html()); Node::Element(Element { name: "p".to_string(), attrs: vec![], children: vec![Node::Text(new_text)], }) }) .html(); assert_eq!(html, REPLACED_HTML); }