Skip to content

Commit

Permalink
GenericNode::first_child
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Jul 4, 2021
1 parent 0e60231 commit 6679e34
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/sycamore/src/generic_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub trait GenericNode: fmt::Debug + Clone + PartialEq + Eq + Hash + 'static {
/// Appends a child to the node's children.
fn append_child(&self, child: &Self);

/// Get the first child of the node.
fn first_child(&self) -> Option<Self>;

/// Insert a new child node to this node's children. If `reference_node` is `Some`, the child
/// will be inserted before the reference node. Else if `None`, the child will be inserted
/// at the end.
Expand Down
7 changes: 7 additions & 0 deletions packages/sycamore/src/generic_node/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ impl GenericNode for DomNode {
self.node.append_child(&child.node).unwrap();
}

fn first_child(&self) -> Option<Self> {
self.node.first_child().map(|node| Self {
id: Default::default(),
node: Rc::new(node),
})
}

fn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>) {
self.node
.insert_before(&new_node.node, reference_node.map(|n| n.node.as_ref()))
Expand Down
7 changes: 7 additions & 0 deletions packages/sycamore/src/generic_node/ssr_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ impl GenericNode for SsrNode {
}
}

fn first_child(&self) -> Option<Self> {
match self.0.ty.as_ref() {
SsrNodeType::Element(element) => element.borrow_mut().children.first().cloned(),
_ => panic!("node type cannot have children"),
}
}

fn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>) {
if let Some(reference_node) = reference_node {
debug_assert_eq!(
Expand Down

0 comments on commit 6679e34

Please sign in to comment.