Merge explicit node iterator structures into single generic structure.

This commit is contained in:
Corey Farwell 2017-06-24 18:51:50 -04:00
parent a29261dec1
commit 011ff28aee
2 changed files with 49 additions and 85 deletions

View file

@ -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 {
struct SimpleNodeIterator<I>
where I: Fn(&Node) -> Option<Root<Node>>
{
current: Option<Root<Node>>,
next_node: I,
}
impl Iterator for LastChildIterator {
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.GetLastChild();
Some(current)
}
}
pub struct AncestorIterator {
current: Option<Root<Node>>,
}
impl Iterator for AncestorIterator {
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
}
}

View file

@ -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;
@ -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();
@ -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>> {