use skyscraper::{
html::{self, trim_internal_whitespace},
xpath,
};
#[test]
fn contains_should_select_text() {
let text = r###"
<html>
<div>
hello world
</div>
<div>
select me
</div>
</html>"###;
let document = html::parse(&text).unwrap();
let xpath = xpath::parse("//div[contains(text(),'select')]").unwrap();
let nodes = xpath.apply(&document).unwrap();
assert_eq!(nodes.len(), 1);
let mut nodes = nodes.into_iter();
{
let tree_node = nodes.next().unwrap().extract_into_node();
let element = tree_node.extract_as_element_node();
assert_eq!(element.name, "div");
assert_eq!(
trim_internal_whitespace(&tree_node.text(&document).unwrap()),
"select me"
);
}
}
#[test]
fn contains_should_select_attribute() {
let text = r###"
<html>
<div class="nah">
hello world
</div>
<div class="select me">
select me
</div>
</html>"###;
let document = html::parse(&text).unwrap();
let xpath = xpath::parse("//div[contains(@class,'select')]").unwrap();
let nodes = xpath.apply(&document).unwrap();
assert_eq!(nodes.len(), 1);
let mut nodes = nodes.into_iter();
{
let tree_node = nodes.next().unwrap().extract_into_node();
let element = tree_node.extract_as_element_node();
assert_eq!(element.name, "div");
assert_eq!(
trim_internal_whitespace(&tree_node.text(&document).unwrap()),
"select me"
);
}
}
#[test]
fn contains_should_select_on_expression() {
let text = r###"
<html>
<div>
other
</div>
<div>
hello world
</div>
<div class="hello" id="select"></div>
</html>"###;
let document = html::parse(&text).unwrap();
let xpath = xpath::parse("//div[contains(text(),//div[@id='select']/@class)]").unwrap();
let nodes = xpath.apply(&document).unwrap();
assert_eq!(nodes.len(), 1);
let mut nodes = nodes.into_iter();
{
let tree_node = nodes.next().unwrap().extract_into_node();
let element = tree_node.extract_as_element_node();
assert_eq!(element.name, "div");
assert_eq!(
trim_internal_whitespace(&tree_node.text(&document).unwrap()),
"hello world"
);
}
}