Skip to content

Commit

Permalink
see #22, replaceChild
Browse files Browse the repository at this point in the history
  • Loading branch information
andrieshiemstra committed May 12, 2022
1 parent a4d22d0 commit e54cdbf
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/modules/htmldom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ fn init_node_proxy<R: JsRealmAdapter>(realm: &R) -> Result<R::JsValueAdapterType
//
if args.len() != 1 || !args[0].js_is_proxy_instance() {
return Err(JsError::new_str(
"appendChremoveChildild expects a single Node argument",
"removeChild expects a single Node argument",
));
}
let p_data = realm.js_proxy_instance_get_info(&args[0])?;
Expand All @@ -430,6 +430,43 @@ fn init_node_proxy<R: JsRealmAdapter>(realm: &R) -> Result<R::JsValueAdapterType
}
})
})
.add_method("replaceChild", |_rt, realm, id, args| {
//
if args.len() != 2 || !args[0].js_is_proxy_instance() || !args[1].js_is_proxy_instance()
{
return Err(JsError::new_str(
"replaceChild expects two Node arguments (newChild, oldChild)",
));
}

let p_data_new_child = realm.js_proxy_instance_get_info(&args[0])?;
if !p_data_new_child.0.eq("greco.htmldom.Node") {
return Err(JsError::new_str(
"replaceChild expects two Node arguments (newChild, oldChild)",
));
}

let new_child = with_node(&p_data_new_child.1, |child| child.clone());

let p_data_old_child = realm.js_proxy_instance_get_info(&args[1])?;
if !p_data_old_child.0.eq("greco.htmldom.Node") {
return Err(JsError::new_str(
"replaceChild expects two Node arguments (newChild, oldChild)",
));
}

let old_child = with_node(&p_data_old_child.1, |child| child.clone());

with_node(&id, |node| match node.as_element() {
None => Err(JsError::new_str("Node was not an Element")),
Some(_element) => {
old_child.insert_before(new_child);
old_child.detach();
let _ = old_child.parent().take();
Ok(args[1].clone())
}
})
})
.add_method("createElement", |_rt, realm, id, args| {
//

Expand Down

0 comments on commit e54cdbf

Please sign in to comment.