implemented iterator for child elements

This commit is contained in:
Shamir Khodzha 2013-12-30 00:25:09 +04:00
parent be8ed32562
commit 8bb1724c1e
2 changed files with 20 additions and 7 deletions

View file

@ -162,9 +162,7 @@ impl Reflectable for Document {
impl Document { impl Document {
pub fn GetDocumentElement(&self) -> Option<AbstractNode> { pub fn GetDocumentElement(&self) -> Option<AbstractNode> {
do self.node.children().find |c| { self.node.child_elements().next()
c.is_element()
}
} }
fn get_cx(&self) -> *JSContext { fn get_cx(&self) -> *JSContext {

View file

@ -25,6 +25,7 @@ use std::cast::transmute;
use std::cast; use std::cast;
use std::unstable::raw::Box; use std::unstable::raw::Box;
use std::util; use std::util;
use std::iter::Filter;
// //
// The basic Node structure // The basic Node structure
@ -486,6 +487,10 @@ impl<'self> AbstractNode {
self.node().children() self.node().children()
} }
pub fn child_elements(&self) -> Filter<AbstractNode, AbstractNodeChildrenIterator> {
self.node().child_elements()
}
pub fn is_in_doc(&self) -> bool { pub fn is_in_doc(&self) -> bool {
self.node().flags.is_in_doc() self.node().flags.is_in_doc()
} }
@ -749,6 +754,10 @@ impl Node {
} }
} }
pub fn child_elements(&self) -> Filter<AbstractNode, AbstractNodeChildrenIterator> {
self.children().filter(|node| node.is_element())
}
pub fn reflect_node<N: Reflectable> pub fn reflect_node<N: Reflectable>
(node: @mut N, (node: @mut N,
document: AbstractDocument, document: AbstractDocument,
@ -1041,11 +1050,13 @@ impl Node {
if node.children().any(|c| c.is_text()) { if node.children().any(|c| c.is_text()) {
return Err(HierarchyRequest); return Err(HierarchyRequest);
} }
match node.children().count(|c| c.is_element()) { match node.child_elements().len() {
0 => (), 0 => (),
// Step 6.1.2 // Step 6.1.2
1 => { 1 => {
if parent.children().any(|c| c.is_element()) { // FIXME: change to empty() when https://github.com/mozilla/rust/issues/11218
// will be fixed
if parent.child_elements().len() > 0 {
return Err(HierarchyRequest); return Err(HierarchyRequest);
} }
if inclusively_followed_by_doctype(child) { if inclusively_followed_by_doctype(child) {
@ -1058,7 +1069,9 @@ impl Node {
}, },
// Step 6.2 // Step 6.2
ElementNodeTypeId(_) => { ElementNodeTypeId(_) => {
if parent.children().any(|c| c.is_element()) { // FIXME: change to empty() when https://github.com/mozilla/rust/issues/11218
// will be fixed
if parent.child_elements().len() > 0 {
return Err(HierarchyRequest); return Err(HierarchyRequest);
} }
if inclusively_followed_by_doctype(child) { if inclusively_followed_by_doctype(child) {
@ -1079,7 +1092,9 @@ impl Node {
} }
}, },
None => { None => {
if parent.children().any(|c| c.is_element()) { // FIXME: change to empty() when https://github.com/mozilla/rust/issues/11218
// will be fixed
if parent.child_elements().len() > 0 {
return Err(HierarchyRequest); return Err(HierarchyRequest);
} }
}, },