use skyscraper::{html, xpath};
#[test]
fn parent_axis_should_select_parent_node() {
// arrange
let text = r###"
"###;
let document = html::parse(&text).unwrap();
let xpath_item_tree = xpath::XpathItemTree::from(&document);
let xpath = xpath::parse("//body/parent::html").unwrap();
// act
let nodes = xpath.apply(&xpath_item_tree).unwrap();
// assert
assert_eq!(nodes.len(), 1);
let mut nodes = nodes.into_iter();
// assert node
{
let tree_node = nodes.next().unwrap().extract_into_node();
let element = tree_node.extract_as_element_node();
assert_eq!(element.name, "html");
}
}
#[test]
fn parent_axis_should_select_parents_of_all_selected_nodes() {
// arrange
// `not-p` is not selected by the initial `//p` query, so its parent `div` should not be selected by the parent axis.
let text = r###"
3
"###;
let document = html::parse(&text).unwrap();
let xpath_item_tree = xpath::XpathItemTree::from(&document);
let xpath = xpath::parse("//p/parent::div").unwrap();
// act
let nodes = xpath.apply(&xpath_item_tree).unwrap();
// assert
assert_eq!(nodes.len(), 2);
let mut nodes = nodes.into_iter();
// assert node
{
let tree_node = nodes.next().unwrap().extract_into_node();
let element = tree_node.extract_as_element_node();
assert_eq!(element.name, "div");
assert_eq!(element.get_attribute(&xpath_item_tree, "id"), Some("1"));
}
// assert node
{
let tree_node = nodes.next().unwrap().extract_into_node();
let element = tree_node.extract_as_element_node();
assert_eq!(element.name, "div");
assert_eq!(element.get_attribute(&xpath_item_tree, "id"), Some("2"));
}
}
/// The node test (ex. `div`) is applied to the parent of the context node.
#[test]
fn parent_axis_should_respect_node_test() {
// arrange
// `not-div` does not match the node test of `div`, so it should not be selected.
let text = r###"
3
"###;
let document = html::parse(&text).unwrap();
let xpath_item_tree = xpath::XpathItemTree::from(&document);
let xpath = xpath::parse("//p/parent::div").unwrap();
// act
let nodes = xpath.apply(&xpath_item_tree).unwrap();
// assert
assert_eq!(nodes.len(), 2);
let mut nodes = nodes.into_iter();
// assert node
{
let tree_node = nodes.next().unwrap().extract_into_node();
let element = tree_node.extract_as_element_node();
assert_eq!(element.name, "div");
assert_eq!(element.get_attribute(&xpath_item_tree, "id"), Some("1"));
}
// assert node
{
let tree_node = nodes.next().unwrap().extract_into_node();
let element = tree_node.extract_as_element_node();
assert_eq!(element.name, "div");
assert_eq!(element.get_attribute(&xpath_item_tree, "id"), Some("2"));
}
}