diff options
author | Corey Farwell <coreyf@rwell.org> | 2017-06-24 18:51:50 -0400 |
---|---|---|
committer | Corey Farwell <coreyf@rwell.org> | 2017-06-25 00:29:08 -0400 |
commit | 011ff28aee34f59eb1e788de2666062c7b6dbff4 (patch) | |
tree | 7bfeb1a11067cf2a0a54242fa25585f7cc747650 /components/script/dom/node.rs | |
parent | a29261dec196ab5c887655e479e503ca6cbb9229 (diff) | |
download | servo-011ff28aee34f59eb1e788de2666062c7b6dbff4.tar.gz servo-011ff28aee34f59eb1e788de2666062c7b6dbff4.zip |
Merge explicit node iterator structures into single generic structure.
Diffstat (limited to 'components/script/dom/node.rs')
-rw-r--r-- | components/script/dom/node.rs | 120 |
1 files changed, 40 insertions, 80 deletions
diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index cace0497433..fb74eb1780c 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -499,15 +499,17 @@ impl Node { TreeIterator::new(self) } - pub fn inclusively_following_siblings(&self) -> NodeSiblingIterator { - NodeSiblingIterator { + pub fn inclusively_following_siblings(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { current: Some(Root::from_ref(self)), + next_node: |n| n.GetNextSibling(), } } - pub fn inclusively_preceding_siblings(&self) -> ReverseSiblingIterator { - ReverseSiblingIterator { + pub fn inclusively_preceding_siblings(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { current: Some(Root::from_ref(self)), + next_node: |n| n.GetPreviousSibling(), } } @@ -519,15 +521,17 @@ impl Node { parent.ancestors().any(|ancestor| &*ancestor == self) } - pub fn following_siblings(&self) -> NodeSiblingIterator { - NodeSiblingIterator { + pub fn following_siblings(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { current: self.GetNextSibling(), + next_node: |n| n.GetNextSibling(), } } - pub fn preceding_siblings(&self) -> ReverseSiblingIterator { - ReverseSiblingIterator { + pub fn preceding_siblings(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { current: self.GetPreviousSibling(), + next_node: |n| n.GetPreviousSibling(), } } @@ -545,9 +549,10 @@ impl Node { } } - pub fn descending_last_children(&self) -> LastChildIterator { - LastChildIterator { + pub fn descending_last_children(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { current: self.GetLastChild(), + next_node: |n| n.GetLastChild(), } } @@ -755,15 +760,17 @@ impl Node { Ok(NodeList::new_simple_list(&window, iter)) } - pub fn ancestors(&self) -> AncestorIterator { - AncestorIterator { - current: self.GetParentNode() + pub fn ancestors(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { + current: self.GetParentNode(), + next_node: |n| n.GetParentNode(), } } - pub fn inclusive_ancestors(&self) -> AncestorIterator { - AncestorIterator { - current: Some(Root::from_ref(self)) + pub fn inclusive_ancestors(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { + current: Some(Root::from_ref(self)), + next_node: |n| n.GetParentNode(), } } @@ -783,15 +790,17 @@ impl Node { self.is_in_doc() && self.owner_doc().browsing_context().is_some() } - pub fn children(&self) -> NodeSiblingIterator { - NodeSiblingIterator { + pub fn children(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { current: self.GetFirstChild(), + next_node: |n| n.GetNextSibling(), } } - pub fn rev_children(&self) -> ReverseSiblingIterator { - ReverseSiblingIterator { + pub fn rev_children(&self) -> impl Iterator<Item=Root<Node>> { + SimpleNodeIterator { current: self.GetLastChild(), + next_node: |n| n.GetPreviousSibling(), } } @@ -1157,40 +1166,6 @@ impl LayoutNodeHelpers for LayoutJS<Node> { // Iteration and traversal // -pub struct NodeSiblingIterator { - current: Option<Root<Node>>, -} - -impl Iterator for NodeSiblingIterator { - type Item = Root<Node>; - - fn next(&mut self) -> Option<Root<Node>> { - let current = match self.current.take() { - None => return None, - Some(current) => current, - }; - self.current = current.GetNextSibling(); - Some(current) - } -} - -pub struct ReverseSiblingIterator { - current: Option<Root<Node>>, -} - -impl Iterator for ReverseSiblingIterator { - type Item = Root<Node>; - - fn next(&mut self) -> Option<Root<Node>> { - let current = match self.current.take() { - None => return None, - Some(current) => current, - }; - self.current = current.GetPreviousSibling(); - Some(current) - } -} - pub struct FollowingNodeIterator { current: Option<Root<Node>>, root: Root<Node>, @@ -1283,37 +1258,22 @@ impl Iterator for PrecedingNodeIterator { } } -pub struct LastChildIterator { - current: Option<Root<Node>>, -} - -impl Iterator for LastChildIterator { - type Item = Root<Node>; - - fn next(&mut self) -> Option<Root<Node>> { - let current = match self.current.take() { - None => return None, - Some(current) => current, - }; - self.current = current.GetLastChild(); - Some(current) - } -} - -pub struct AncestorIterator { +struct SimpleNodeIterator<I> + where I: Fn(&Node) -> Option<Root<Node>> +{ current: Option<Root<Node>>, + next_node: I, } -impl Iterator for AncestorIterator { +impl<I> Iterator for SimpleNodeIterator<I> + where I: Fn(&Node) -> Option<Root<Node>> +{ type Item = Root<Node>; - fn next(&mut self) -> Option<Root<Node>> { - let current = match self.current.take() { - None => return None, - Some(current) => current, - }; - self.current = current.GetParentNode(); - Some(current) + fn next(&mut self) -> Option<Self::Item> { + let current = self.current.take(); + self.current = current.as_ref().and_then(|c| (self.next_node)(c)); + current } } |