use std::result::Result as StdResult;
use visdom::types::BoxDynError;
use visdom::Vis;
type Result = StdResult<(), BoxDynError>;
#[test]
fn test_document_trait() -> Result {
let page_title = "Vis<dom>";
let html = format!(
r#"
<!doctype html>
<html>
<head>
<title>{page_title}</title>
</head>
<body>
Visdom!
</body>
</html>
"#
);
let root = Vis::load(&html)?;
let doc = root.document();
assert!(doc.is_some());
let doc = doc.unwrap();
let title = doc.title();
assert!(title.is_some() && title.unwrap() == page_title);
let head = doc.head();
assert!(head.is_some() && head.unwrap().children().filter("title").text() == page_title);
let body = doc.body();
assert!(body.is_some() && body.as_ref().unwrap().previous_element_sibling().is_some());
assert_eq!(
body
.as_ref()
.unwrap()
.previous_element_sibling()
.unwrap()
.tag_name(),
"HEAD"
);
assert_eq!(doc.source_code(), html);
let doc_element = doc.document_element();
assert!(doc_element.is_some());
assert_eq!(doc_element.unwrap().tag_name(), "HTML");
let html = r##"<div>just a document fragement</div>"##;
let root = Vis::load(html)?;
let doc = root.document();
assert!(doc.is_some());
let doc = doc.unwrap();
assert!(doc.title().is_none());
assert!(doc.head().is_none());
assert!(doc.document_element().is_none());
assert!(doc.body().is_none());
Ok(())
}
#[cfg(feature = "text")]
#[test]
fn test_text_trait() -> Result {
let html = r#"
<!doctype html>
<html>
<head>
<title>test text trait</title>
</head>
<body>
<div id="content">Vis<span>dom</span></div>
</body>
</html>
"#;
let root = Vis::load(html)?;
let id_content = root.find("#content");
let mut texts = id_content.texts(0);
assert_eq!(texts.length(), 2);
assert!(texts.get_ref().first().unwrap().text() == "Vis");
assert!(texts.get_ref().get(1).unwrap().text() == "dom");
texts.for_each(|_, node| {
node.prepend_text("^");
node.append_text("$");
true
});
assert!(texts.get_ref().first().unwrap().text() == "^Vis$");
assert!(texts.get_ref().get(1).unwrap().text() == "^dom$");
texts.remove();
let texts = id_content.texts(0);
assert_eq!(texts.length(), 0);
let root = Vis::load("<script></script>")?;
let mut script_text = root.find("script").texts(1);
script_text.for_each(|_, text_node| {
assert_eq!(text_node.text(), "");
text_node.prepend_text("var a;");
text_node.append_text("var b;");
assert_eq!(text_node.text(), "var a;var b;");
true
});
let root = Vis::load("<style></style>")?;
let mut style_text = root.find("style").texts(1);
style_text.for_each(|_, text_node| {
assert_eq!(text_node.text(), "");
text_node.append_text("{}");
text_node.prepend_text("body");
assert_eq!(text_node.text(), "body{}");
true
});
let root = Vis::load(r#"<a><span>&</a>"#)?;
let a_link = root.find("a");
let mut texts = a_link.texts(1);
texts.for_each(|_, ele| {
assert_eq!(ele.text(), "<span>&");
assert_eq!(
ele.text_chars().iter().collect::<String>(),
"<span>&"
);
true
});
Ok(())
}
#[test]
fn test_node_trait() -> Result {
let html = r#"
<!doctype html>
<html>
<head>
<title>test text trait</title>
</head>
<body>
<div id="content">Vis<span>dom</span></div>
</body>
</html>
"#;
let root = Vis::load(html)?;
let root_element = root.get(0).unwrap().root_element().unwrap();
assert!(root_element.is(&root_element.root_element().unwrap()));
Ok(())
}
#[test]
#[cfg(feature = "text")]
fn test_node_text_trait() -> Result {
let html = r#"
<!doctype html>
<html>
<head>
<title>test text trait</title>
</head>
<body>
<div id="content">Vis<span>dom</span></div>
</body>
</html>
"#;
let root = Vis::load(html)?;
let content = root.find("#content");
assert!(content
.get(0)
.unwrap()
.clone_node()
.typed()
.into_text()
.is_none());
let mut texts = content.texts(1);
texts.for_each(|_, text_node| {
assert!(text_node.clone_node().typed().into_element().is_none());
true
});
Ok(())
}