use node_html_parser::{parse_with_options, Node, Options};
#[test]
fn issue_171_append_child_moves_node() {
let html = "<a><b><d></d></b><c></c></a>";
let mut root = parse_with_options(html, &Options::default());
let a = root.first_element_child_mut().unwrap();
let b = a.first_element_child_mut().unwrap();
let mut d_opt = None;
for child in &mut b.children {
if let Node::Element(e) = child {
if e.name() == "d" {
d_opt = Some(e.clone());
break;
}
}
}
let d_cloned = d_opt.expect("d element");
b.remove_children_where(|n| matches!(n, Node::Element(e) if e.name()=="d"));
let c = a
.children
.iter_mut()
.find_map(|n| {
if let Node::Element(e) = n {
if e.name() == "c" {
Some(e)
} else {
None
}
} else {
None
}
})
.unwrap();
c.append_child(Node::Element(d_cloned));
assert_eq!(
a.to_string(),
"<a><b></b><c><d></d></c></a>".replace("<a>", "<a>")
);
}