diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2017-06-25 00:38:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-25 00:38:23 -0700 |
commit | f740366c611d375f0bb259ae0f4d846c05ec276e (patch) | |
tree | f9442642b19c77bdc81ab9796a06bb84b778be22 /components/script/dom/node.rs | |
parent | c39de45d81eb54c3a8dc78b41883b07dbd7d3896 (diff) | |
parent | 011ff28aee34f59eb1e788de2666062c7b6dbff4 (diff) | |
download | servo-f740366c611d375f0bb259ae0f4d846c05ec276e.tar.gz servo-f740366c611d375f0bb259ae0f4d846c05ec276e.zip |
Auto merge of #17515 - frewsxcv:frewsxcv-simple-node-iterator, r=KiChjang
Merge explicit node iterator structures into single generic structure.
<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17515)
<!-- Reviewable:end -->
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 2e0152195dd..bf24682ede7 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -504,15 +504,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(), } } @@ -524,15 +526,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(), } } @@ -550,9 +554,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(), } } @@ -760,15 +765,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(), } } @@ -788,15 +795,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(), } } @@ -1162,40 +1171,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>, @@ -1288,37 +1263,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 } } |