Skip to content

Commit

Permalink
simplify setting parent node
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Mar 28, 2024
1 parent b6f408e commit 95e495f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
31 changes: 8 additions & 23 deletions crates/core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,18 @@ impl DomNode {
}

/// append the DomNode `child` into this DomNode `self`
pub fn append_children(&self, child: impl IntoIterator<Item = DomNode>) -> Result<(), JsValue> {
pub fn append_children(&self, for_append: impl IntoIterator<Item = DomNode>) -> Result<(), JsValue> {
match &self.inner {
DomInner::Element {
element, children, ..
} => {
for ch in child.into_iter(){
let for_append = for_append.into_iter();
for child in for_append{
element
.append_child(&ch.as_node())
.append_child(&child.as_node())
.expect("append child");
ch.set_parent(&self);
children.borrow_mut().push(ch);
child.set_parent(&self);
children.borrow_mut().push(child);
}
Ok(())
}
Expand Down Expand Up @@ -217,17 +218,8 @@ impl DomNode {
else {
unreachable!("parent must be an element");
};
let DomInner::Element {
element: for_insert_elm,
..
} = &for_insert.inner
else {
unreachable!("for insert must be an element");
};
//TODO: find the index of `self` in the parent and insert before that index
for_insert.set_parent(parent_target);
parent_element
.insert_before(&for_insert_elm, Some(&target_element))
.insert_before(&for_insert.as_node(), Some(&target_element))
.expect("must remove target node");

let mut self_index = None;
Expand All @@ -252,14 +244,7 @@ impl DomNode {
DomInner::Element { element, .. } => element,
_ => unreachable!("target element should be an element"),
};
//TODO: find the index of `self` in the parent and insert the for_insert into that index
match &for_insert.inner {
DomInner::Element { element, .. } => {
target_element.insert_adjacent_element(intern("afterend"), &element)?;

}
_ => unreachable!("unexpected variant to be inserted after.."),
}
target_element.insert_adjacent_element(intern("afterend"), &for_insert.as_element())?;

let parent_target = self.parent.borrow();
let parent_target = parent_target.as_ref().expect("must have a parent");
Expand Down
2 changes: 1 addition & 1 deletion examples/js-performance-benchmark-sauron/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Application for App {
<button
type="button"
class="btn btn-primary btn-block"
on_click={|_|Msg::Update(2)}
on_click={|_|Msg::Update(10)}
id="update">
Update every 10th row
</button>
Expand Down

0 comments on commit 95e495f

Please sign in to comment.