Skip to content

Commit c274c96

Browse files
impl "append" method for Element
1 parent 3894cb1 commit c274c96

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/modules/htmldom/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,36 @@ fn init_node_proxy(realm: &QuickJsRealmAdapter) -> Result<QuickJsValueAdapter, J
959959
}
960960
})
961961
})
962+
.method("append", |_rt, realm, id, args| {
963+
//
964+
with_node(id, |node| match node.as_element() {
965+
None => Err(JsError::new_str("Node was not an Element")),
966+
Some(_element) => {
967+
for arg in args {
968+
if arg.is_string() {
969+
//
970+
let new_node = NodeRef::new_text(arg.to_string()?);
971+
node.append(new_node);
972+
} else if arg.is_proxy_instance() {
973+
let p_data = realm.get_proxy_instance_info(arg)?;
974+
if !p_data.0.eq("greco.htmldom.Node") {
975+
return Err(JsError::new_str(
976+
"appendChild expects a single Node argument",
977+
));
978+
}
979+
980+
let child = with_node(&p_data.1, |child| child.clone());
981+
982+
node.append(child);
983+
} else {
984+
return Err(JsError::new_str("Arg was not an Element or a String"));
985+
}
986+
}
987+
Ok(())
988+
}
989+
})?;
990+
realm.create_null()
991+
})
962992
.method("removeChild", |_rt, realm, id, args| {
963993
// todo, calling this twice by mistake leads to other children being removed
964994

0 commit comments

Comments
 (0)