Skip to content

Commit

Permalink
Merge explicit node iterator structures into single generic structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Jun 25, 2017
1 parent a29261d commit 011ff28
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 85 deletions.
120 changes: 40 additions & 80 deletions components/script/dom/node.rs
Expand Up @@ -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(),
}
}

Expand All @@ -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(),
}
}

Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -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(),
}
}

Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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
}
}

Expand Down
14 changes: 9 additions & 5 deletions components/script/dom/servoparser/mod.rs
Expand Up @@ -24,7 +24,7 @@ use dom::htmlformelement::{FormControlElementHelpers, HTMLFormElement};
use dom::htmlimageelement::HTMLImageElement;
use dom::htmlscriptelement::{HTMLScriptElement, ScriptResult};
use dom::htmltemplateelement::HTMLTemplateElement;
use dom::node::{Node, NodeSiblingIterator};
use dom::node::Node;
use dom::processinginstruction::ProcessingInstruction;
use dom::text::Text;
use dom::virtualmethods::vtable_for;
Expand Down Expand Up @@ -119,7 +119,7 @@ impl ServoParser {
}

// https://html.spec.whatwg.org/multipage/#parsing-html-fragments
pub fn parse_html_fragment(context: &Element, input: DOMString) -> FragmentParsingResult {
pub fn parse_html_fragment(context: &Element, input: DOMString) -> impl Iterator<Item=Root<Node>> {
let context_node = context.upcast::<Node>();
let context_document = context_node.owner_doc();
let window = context_document.window();
Expand Down Expand Up @@ -468,11 +468,15 @@ impl ServoParser {
}
}

pub struct FragmentParsingResult {
inner: NodeSiblingIterator,
struct FragmentParsingResult<I>
where I: Iterator<Item=Root<Node>>
{
inner: I,
}

impl Iterator for FragmentParsingResult {
impl<I> Iterator for FragmentParsingResult<I>
where I: Iterator<Item=Root<Node>>
{
type Item = Root<Node>;

fn next(&mut self) -> Option<Root<Node>> {
Expand Down

0 comments on commit 011ff28

Please sign in to comment.