From eb1ef59aeec09aed5780586e4de5810b5073ee9f Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Fri, 31 Oct 2014 10:51:22 -0400 Subject: [PATCH 1/3] s/AbstractNodeChildrenIterator/NodeChildrenIterator/ --- components/script/dom/node.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 2375037eeac..fcf273113cb 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -380,10 +380,10 @@ impl<'a> PrivateNodeHelpers for JSRef<'a, Node> { pub trait NodeHelpers<'a> { fn ancestors(self) -> AncestorIterator<'a>; - fn children(self) -> AbstractNodeChildrenIterator<'a>; + fn children(self) -> NodeChildrenIterator<'a>; fn rev_children(self) -> ReverseChildrenIterator<'a>; fn child_elements(self) -> ChildElementIterator<'a>; - fn following_siblings(self) -> AbstractNodeChildrenIterator<'a>; + fn following_siblings(self) -> NodeChildrenIterator<'a>; fn is_in_doc(self) -> bool; fn is_inclusive_ancestor_of(self, parent: JSRef) -> bool; fn is_parent_of(self, child: JSRef) -> bool; @@ -442,7 +442,7 @@ pub trait NodeHelpers<'a> { fn debug_str(self) -> String; fn traverse_preorder(self) -> TreeIterator<'a>; - fn inclusively_following_siblings(self) -> AbstractNodeChildrenIterator<'a>; + fn inclusively_following_siblings(self) -> NodeChildrenIterator<'a>; fn to_trusted_node_address(self) -> TrustedNodeAddress; @@ -661,8 +661,8 @@ impl<'a> NodeHelpers<'a> for JSRef<'a, Node> { TreeIterator::new(self) } - fn inclusively_following_siblings(self) -> AbstractNodeChildrenIterator<'a> { - AbstractNodeChildrenIterator { + fn inclusively_following_siblings(self) -> NodeChildrenIterator<'a> { + NodeChildrenIterator { current: Some(self.clone()), } } @@ -671,8 +671,8 @@ impl<'a> NodeHelpers<'a> for JSRef<'a, Node> { self == parent || parent.ancestors().any(|ancestor| ancestor == self) } - fn following_siblings(self) -> AbstractNodeChildrenIterator<'a> { - AbstractNodeChildrenIterator { + fn following_siblings(self) -> NodeChildrenIterator<'a> { + NodeChildrenIterator { current: self.next_sibling().root().map(|next| next.clone()), } } @@ -763,8 +763,8 @@ impl<'a> NodeHelpers<'a> for JSRef<'a, Node> { self.owner_doc().root().is_html_document() } - fn children(self) -> AbstractNodeChildrenIterator<'a> { - AbstractNodeChildrenIterator { + fn children(self) -> NodeChildrenIterator<'a> { + NodeChildrenIterator { current: self.first_child.get().map(|node| (*node.root()).clone()), } } @@ -968,13 +968,13 @@ impl RawLayoutNodeHelpers for Node { pub type ChildElementIterator<'a> = Map<'a, JSRef<'a, Node>, JSRef<'a, Element>, - Filter<'a, JSRef<'a, Node>, AbstractNodeChildrenIterator<'a>>>; + Filter<'a, JSRef<'a, Node>, NodeChildrenIterator<'a>>>; -pub struct AbstractNodeChildrenIterator<'a> { +pub struct NodeChildrenIterator<'a> { current: Option>, } -impl<'a> Iterator> for AbstractNodeChildrenIterator<'a> { +impl<'a> Iterator> for NodeChildrenIterator<'a> { fn next(&mut self) -> Option> { let node = self.current; self.current = node.and_then(|node| node.next_sibling().map(|node| *node.root().deref())); From 11d2251b1efd03218fb48f6258fc0c2dc731078b Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Fri, 31 Oct 2014 10:52:39 -0400 Subject: [PATCH 2/3] ChildElementIterator is now peekable --- components/script/dom/node.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index fcf273113cb..326c080f22e 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -59,7 +59,7 @@ use libc; use libc::{uintptr_t, c_void}; use std::cell::{Cell, RefCell, Ref, RefMut}; use std::default::Default; -use std::iter::{Map, Filter}; +use std::iter::{Map, Filter, Peekable}; use std::mem; use style; use style::ComputedValues; @@ -784,6 +784,7 @@ impl<'a> NodeHelpers<'a> for JSRef<'a, Node> { let elem: JSRef = ElementCast::to_ref(node).unwrap(); elem.clone() }) + .peekable() } fn wait_until_safe_to_modify_dom(self) { @@ -966,9 +967,10 @@ impl RawLayoutNodeHelpers for Node { // Iteration and traversal // -pub type ChildElementIterator<'a> = Map<'a, JSRef<'a, Node>, - JSRef<'a, Element>, - Filter<'a, JSRef<'a, Node>, NodeChildrenIterator<'a>>>; +pub type ChildElementIterator<'a> = Peekable, + Map<'a, JSRef<'a, Node>, + JSRef<'a, Element>, + Filter<'a, JSRef<'a, Node>, NodeChildrenIterator<'a>>>>; pub struct NodeChildrenIterator<'a> { current: Option>, @@ -1258,9 +1260,7 @@ impl Node { 0 => (), // Step 6.1.2 1 => { - // FIXME: change to empty() when https://github.com/mozilla/rust/issues/11218 - // will be fixed - if parent.child_elements().count() > 0 { + if parent.child_elements().peek().is_some() { return Err(HierarchyRequest); } match child { @@ -1279,9 +1279,7 @@ impl Node { }, // Step 6.2 ElementNodeTypeId(_) => { - // FIXME: change to empty() when https://github.com/mozilla/rust/issues/11218 - // will be fixed - if parent.child_elements().count() > 0 { + if parent.child_elements().peek().is_some() { return Err(HierarchyRequest); } match child { @@ -1308,9 +1306,7 @@ impl Node { } }, None => { - // FIXME: change to empty() when https://github.com/mozilla/rust/issues/11218 - // will be fixed - if parent.child_elements().count() > 0 { + if parent.child_elements().peek().is_some() { return Err(HierarchyRequest); } }, From d5d4d0bec44c0ec4db100ade498a14300873cadd Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Fri, 31 Oct 2014 11:17:08 -0400 Subject: [PATCH 3/3] s/peek().is_some()/!is_empty()/ --- components/script/dom/node.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 326c080f22e..23041d58a7b 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -1260,7 +1260,7 @@ impl Node { 0 => (), // Step 6.1.2 1 => { - if parent.child_elements().peek().is_some() { + if !parent.child_elements().is_empty() { return Err(HierarchyRequest); } match child { @@ -1279,7 +1279,7 @@ impl Node { }, // Step 6.2 ElementNodeTypeId(_) => { - if parent.child_elements().peek().is_some() { + if !parent.child_elements().is_empty() { return Err(HierarchyRequest); } match child { @@ -1306,7 +1306,7 @@ impl Node { } }, None => { - if parent.child_elements().peek().is_some() { + if !parent.child_elements().is_empty() { return Err(HierarchyRequest); } },