use html5ever::tree_builder::QuirksMode; use html5ever::QualName; use std::path::Path; use tempfile::TempDir; use crate::parser::{parse_html, parse_fragment}; use crate::select::*; use crate::traits::*; #[test] fn text_nodes() { let html = r"
Content contains Important data
"; let document = parse_html().one(html); let paragraph = document.select("p").unwrap().collect::Content"; let document = parse_html().one(html); assert_eq!( document.as_document().unwrap().quirks_mode(), QuirksMode::NoQuirks ); assert_eq!( document.to_string(), r"
Content
" ); } #[test] fn parse_and_serialize_fragment() { let html = r"Foo
"; let document = parse_html().from_utf8().from_file(&path).unwrap(); assert_eq!(document.to_string(), html); } #[test] fn serialize_and_read_file() { let tempdir = TempDir::new().unwrap(); let mut path = tempdir.path().to_path_buf(); path.push("temp.html"); let html = r"Foo
Bar
Foo
";
let document = parse_html().one(html);
let matching = document.select("p.foo").unwrap().collect:: Foo
Bar
Baz
";
let document = parse_html().one(html);
let matching = document.select_first("p.foo").unwrap();
let child = matching.as_node().first_child().unwrap();
assert_eq!(&**child.as_text().unwrap().borrow(), "Foo\n");
assert_eq!(matching.attributes.borrow().get("class"), Some("foo"));
assert_eq!(
matching.attributes.borrow().get(local_name!("class")),
Some("foo")
);
assert!(document.select_first("p.bar").is_err());
}
#[test]
fn to_string() {
let html = r"
Foo
";
let document = parse_html().one(html);
assert_eq!(
document
.inclusive_descendants()
.nth(11)
.unwrap()
.to_string(),
" Foo\n \n