2
0
mirror of https://github.com/Y2Z/monolith synced 2024-11-03 09:40:21 +00:00
monolith/tests/html/get_node_name.rs

54 lines
2.5 KiB
Rust
Raw Normal View History

2020-03-23 02:08:41 +00:00
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
#[cfg(test)]
mod passing {
use html5ever::rcdom::{Handle, NodeData};
2020-03-23 02:08:41 +00:00
use monolith::html;
#[test]
fn parent_node_names() {
let html = "<!doctype html><html><HEAD></HEAD><body><div><P></P></div></body></html>";
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
let mut count = 0;
2020-03-23 02:08:41 +00:00
fn test_walk(node: &Handle, i: &mut i8) {
*i += 1;
match &node.data {
NodeData::Document => {
for child in node.children.borrow().iter() {
test_walk(child, &mut *i);
}
2020-03-23 02:08:41 +00:00
}
NodeData::Element { ref name, .. } => {
let node_name = name.local.as_ref().to_string();
let parent = html::get_parent_node(node);
let parent_node_name = html::get_node_name(&parent);
if node_name == "head" || node_name == "body" {
assert_eq!(parent_node_name, Some("html"));
} else if node_name == "div" {
assert_eq!(parent_node_name, Some("body"));
} else if node_name == "p" {
assert_eq!(parent_node_name, Some("div"));
}
2020-03-23 02:08:41 +00:00
for child in node.children.borrow().iter() {
test_walk(child, &mut *i);
}
2020-03-23 02:08:41 +00:00
}
_ => (),
};
}
2020-03-23 02:08:41 +00:00
test_walk(&dom.document, &mut count);
2020-03-23 02:08:41 +00:00
assert_eq!(count, 7);
}
2020-03-23 02:08:41 +00:00
}