Skip to content

Commit

Permalink
src/node/node_ref.rs: revise code
Browse files Browse the repository at this point in the history
  • Loading branch information
niklak committed Oct 26, 2024
1 parent c6bc1e9 commit c8625f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
12 changes: 11 additions & 1 deletion src/node/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,47 @@ impl<T: Debug> Debug for InnerNode<T> {
}

impl InnerNode<NodeData> {
/// 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),
_ => None,
}
}

/// 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),
Expand Down
55 changes: 23 additions & 32 deletions src/node/node_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,24 @@ 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.
pub fn prev_element_sibling(&self) -> Option<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() {
Expand All @@ -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<Node<'a>> {
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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 } => {
Expand Down
1 change: 0 additions & 1 deletion tests/selection-property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,4 @@ fn test_immediate_text() {
.collect();

assert_eq!(immediate_text, "Hello !Hello !");

}

0 comments on commit c8625f9

Please sign in to comment.