diff --git a/src/node/inner.rs b/src/node/inner.rs index f7f463b..0f44c76 100644 --- a/src/node/inner.rs +++ b/src/node/inner.rs @@ -44,30 +44,39 @@ impl Debug for InnerNode { } impl InnerNode { + /// Checks if the node is a document node. pub fn is_document(&self) -> bool { matches!(self.data, NodeData::Document) } + /// Checks if the node is an element node. pub fn is_element(&self) -> bool { matches!(self.data, NodeData::Element(_)) } + /// Checks if the node is a text node. pub fn is_text(&self) -> bool { matches!(self.data, NodeData::Text { .. }) } + /// Checks if the node is a comment node. pub fn is_comment(&self) -> bool { matches!(self.data, NodeData::Comment { .. }) } + /// Checks if the node is a fragment node. pub fn is_fragment(&self) -> bool { matches!(self.data, NodeData::Fragment) } + /// Checks if the node is a doctype node. pub fn is_doctype(&self) -> bool { matches!(self.data, NodeData::Doctype { .. }) } - + /// Returns a reference to the node as an element. If the node is not an element, `None` is returned. + /// + /// # Returns + /// `Option<&Element>` pub fn as_element(&self) -> Option<&Element> { match self.data { NodeData::Element(ref e) => Some(e), @@ -75,6 +84,7 @@ impl InnerNode { } } + /// Returns a mutable reference to the node as an element. If the node is not an element, `None` is returned. pub fn as_element_mut(&mut self) -> Option<&mut Element> { match self.data { NodeData::Element(ref mut e) => Some(e), diff --git a/src/node/node_ref.rs b/src/node/node_ref.rs index 9bce9d1..a9ef52e 100644 --- a/src/node/node_ref.rs +++ b/src/node/node_ref.rs @@ -187,17 +187,16 @@ impl<'a> Node<'a> { let nodes = self.tree.nodes.borrow(); let mut node = nodes.get(self.id.value)?; - let r = loop { - if let Some(id) = node.next_sibling { - node = nodes.get(id.value)?; - if node.is_element() { - break Some(NodeRef::new(id, self.tree)); - } - } else { + let sibling = loop { + let Some(id) = node.next_sibling else { break None; + }; + node = nodes.get(id.value)?; + if node.is_element() { + break Some(NodeRef::new(id, self.tree)); } }; - r + sibling } /// Returns the previous sibling, that is an [`crate::node::node_data::Element`] of the selected node. @@ -205,7 +204,7 @@ impl<'a> Node<'a> { let nodes = self.tree.nodes.borrow(); let mut node = nodes.get(self.id.value)?; - let r = loop { + let sibling = loop { if let Some(id) = node.prev_sibling { node = nodes.get(id.value)?; if node.is_element() { @@ -215,24 +214,23 @@ impl<'a> Node<'a> { break None; } }; - r + sibling } /// Returns the first child, that is an [`crate::node::node_data::Element`] of the selected node. pub fn first_element_child(&self) -> Option> { let nodes = self.tree.nodes.borrow(); - if let Some(node) = nodes.get(self.id.value) { - let mut next_child_id = node.first_child; - - while let Some(node_id) = next_child_id { - if node.is_element() { - return Some(NodeRef { - id: node_id, - tree: self.tree, - }); - } - next_child_id = node.next_sibling; + let node = nodes.get(self.id.value)?; + let mut next_child_id = node.first_child; + + while let Some(node_id) = next_child_id { + if node.is_element() { + return Some(NodeRef { + id: node_id, + tree: self.tree, + }); } + next_child_id = node.next_sibling; } None } @@ -426,16 +424,12 @@ impl<'a> Node<'a> { let mut ops = vec![self.id]; let mut text = StrTendril::new(); let nodes = self.tree.nodes.borrow(); - while !ops.is_empty() { - let id = ops.remove(0); + while let Some(id) = ops.pop() { if let Some(node) = nodes.get(id.value) { match node.data { NodeData::Element(_) => { - for child in self.tree.child_ids_of(&id).into_iter().rev() { - ops.insert(0, child); - } + ops.extend(self.tree.child_ids_of(&id).into_iter().rev()); } - NodeData::Text { ref contents } => text.push_tendril(contents), _ => continue, @@ -464,14 +458,11 @@ impl<'a> Node<'a> { pub fn has_text(&self, needle: &str) -> bool { let mut ops = vec![self.id]; let nodes = self.tree.nodes.borrow(); - while !ops.is_empty() { - let id = ops.remove(0); + while let Some(id) = ops.pop() { if let Some(node) = nodes.get(id.value) { match node.data { NodeData::Element(_) => { - for child in self.tree.child_ids_of(&id).into_iter().rev() { - ops.insert(0, child); - } + ops.extend(self.tree.child_ids_of(&id).into_iter().rev()); } NodeData::Text { ref contents } => { diff --git a/tests/selection-property.rs b/tests/selection-property.rs index b37b4df..2f81427 100644 --- a/tests/selection-property.rs +++ b/tests/selection-property.rs @@ -293,5 +293,4 @@ fn test_immediate_text() { .collect(); assert_eq!(immediate_text, "Hello !Hello !"); - }