Auto merge of #5981 - Jinwoo-Song:nodeiterator, r=Manishearth

Implement NodeIterator's basic functionality. (Fixes #1235)  But the cases for node removals are not implemented yet. 

r? @jdm 
cc @yichoi

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/5981)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-05-28 05:14:08 -05:00
commit 2b52006b1c
8 changed files with 364 additions and 2316 deletions

View file

@ -58,6 +58,7 @@ use dom::keyboardevent::KeyboardEvent;
use dom::messageevent::MessageEvent; use dom::messageevent::MessageEvent;
use dom::node::{self, Node, NodeHelpers, NodeTypeId, CloneChildrenFlag, NodeDamage, window_from_node}; use dom::node::{self, Node, NodeHelpers, NodeTypeId, CloneChildrenFlag, NodeDamage, window_from_node};
use dom::nodelist::NodeList; use dom::nodelist::NodeList;
use dom::nodeiterator::NodeIterator;
use dom::text::Text; use dom::text::Text;
use dom::processinginstruction::ProcessingInstruction; use dom::processinginstruction::ProcessingInstruction;
use dom::range::Range; use dom::range::Range;
@ -1363,6 +1364,12 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
Range::new_with_doc(self) Range::new_with_doc(self)
} }
// https://dom.spec.whatwg.org/#dom-document-createnodeiteratorroot-whattoshow-filter
fn CreateNodeIterator(self, root: JSRef<Node>, whatToShow: u32, filter: Option<NodeFilter>)
-> Temporary<NodeIterator> {
NodeIterator::new(self, root, whatToShow, filter)
}
// https://dom.spec.whatwg.org/#dom-document-createtreewalker // https://dom.spec.whatwg.org/#dom-document-createtreewalker
fn CreateTreeWalker(self, root: JSRef<Node>, whatToShow: u32, filter: Option<NodeFilter>) fn CreateTreeWalker(self, root: JSRef<Node>, whatToShow: u32, filter: Option<NodeFilter>)
-> Temporary<TreeWalker> { -> Temporary<TreeWalker> {

View file

@ -426,6 +426,9 @@ pub trait NodeHelpers {
fn child_elements(self) -> ChildElementIterator; fn child_elements(self) -> ChildElementIterator;
fn following_siblings(self) -> NodeSiblingIterator; fn following_siblings(self) -> NodeSiblingIterator;
fn preceding_siblings(self) -> ReverseSiblingIterator; fn preceding_siblings(self) -> ReverseSiblingIterator;
fn following_nodes(self, root: JSRef<Node>) -> FollowingNodeIterator;
fn preceding_nodes(self, root: JSRef<Node>) -> PrecedingNodeIterator;
fn descending_last_children(self) -> LastChildIterator;
fn is_in_doc(self) -> bool; fn is_in_doc(self) -> bool;
fn is_inclusive_ancestor_of(self, parent: JSRef<Node>) -> bool; fn is_inclusive_ancestor_of(self, parent: JSRef<Node>) -> bool;
fn is_parent_of(self, child: JSRef<Node>) -> bool; fn is_parent_of(self, child: JSRef<Node>) -> bool;
@ -785,6 +788,26 @@ impl<'a> NodeHelpers for JSRef<'a, Node> {
} }
} }
fn following_nodes(self, root: JSRef<Node>) -> FollowingNodeIterator {
FollowingNodeIterator {
current: Some(Temporary::from_rooted(self)),
root: Temporary::from_rooted(root),
}
}
fn preceding_nodes(self, root: JSRef<Node>) -> PrecedingNodeIterator {
PrecedingNodeIterator {
current: Some(Temporary::from_rooted(self)),
root: Temporary::from_rooted(root),
}
}
fn descending_last_children(self) -> LastChildIterator {
LastChildIterator {
current: self.GetLastChild(),
}
}
fn is_parent_of(self, child: JSRef<Node>) -> bool { fn is_parent_of(self, child: JSRef<Node>) -> bool {
match child.parent_node.get().root() { match child.parent_node.get().root() {
Some(ref parent) => parent.r() == self, Some(ref parent) => parent.r() == self,
@ -922,7 +945,6 @@ impl<'a> NodeHelpers for JSRef<'a, Node> {
Ok(NodeList::new_simple_list(window.r(), iter)) Ok(NodeList::new_simple_list(window.r(), iter))
} }
fn ancestors(self) -> AncestorIterator { fn ancestors(self) -> AncestorIterator {
AncestorIterator { AncestorIterator {
current: self.GetParentNode() current: self.GetParentNode()
@ -1236,6 +1258,114 @@ impl Iterator for ReverseSiblingIterator {
} }
} }
pub struct FollowingNodeIterator {
current: Option<Temporary<Node>>,
root: Temporary<Node>,
}
impl Iterator for FollowingNodeIterator {
type Item = Temporary<Node>;
// https://dom.spec.whatwg.org/#concept-tree-following
fn next(&mut self) -> Option<Temporary<Node>> {
let current = match self.current.take() {
None => return None,
Some(current) => current,
};
let node = current.root();
if let Some(first_child) = node.r().GetFirstChild() {
self.current = Some(first_child);
return node.r().GetFirstChild()
}
if self.root == current {
self.current = None;
return None;
}
if let Some(next_sibling) = node.r().GetNextSibling() {
self.current = Some(next_sibling);
return node.r().GetNextSibling()
}
for ancestor in node.r().inclusive_ancestors() {
if self.root == ancestor {
break;
}
if let Some(next_sibling) = ancestor.root().r().GetNextSibling() {
self.current = Some(next_sibling);
return ancestor.root().r().GetNextSibling()
}
}
self.current = None;
return None
}
}
pub struct PrecedingNodeIterator {
current: Option<Temporary<Node>>,
root: Temporary<Node>,
}
impl Iterator for PrecedingNodeIterator {
type Item = Temporary<Node>;
// https://dom.spec.whatwg.org/#concept-tree-preceding
fn next(&mut self) -> Option<Temporary<Node>> {
let current = match self.current.take() {
None => return None,
Some(current) => current,
};
if self.root == current {
self.current = None;
return None
}
let node = current.root();
if let Some(previous_sibling) = node.r().GetPreviousSibling() {
if self.root == previous_sibling {
self.current = None;
return None
}
if let Some(last_child) = previous_sibling.root().r().descending_last_children().last() {
self.current = Some(last_child);
return previous_sibling.root().r().descending_last_children().last()
}
self.current = Some(previous_sibling);
return node.r().GetPreviousSibling()
};
if let Some(parent_node) = node.r().GetParentNode() {
self.current = Some(parent_node);
return node.r().GetParentNode()
}
self.current = None;
return None
}
}
pub struct LastChildIterator {
current: Option<Temporary<Node>>,
}
impl Iterator for LastChildIterator {
type Item = Temporary<Node>;
fn next(&mut self) -> Option<Temporary<Node>> {
let current = match self.current.take() {
None => return None,
Some(current) => current,
}.root();
self.current = current.r().GetLastChild();
Some(Temporary::from_rooted(current.r()))
}
}
pub struct AncestorIterator { pub struct AncestorIterator {
current: Option<Temporary<Node>>, current: Option<Temporary<Node>>,
} }
@ -1298,7 +1428,6 @@ impl Iterator for TreeIterator {
} }
} }
/// Specifies whether children must be recursively cloned or not. /// Specifies whether children must be recursively cloned or not.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub enum CloneChildrenFlag { pub enum CloneChildrenFlag {

View file

@ -2,24 +2,226 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::callback::ExceptionHandling::Rethrow;
use dom::bindings::codegen::Bindings::NodeIteratorBinding; use dom::bindings::codegen::Bindings::NodeIteratorBinding;
use dom::bindings::codegen::Bindings::NodeIteratorBinding::NodeIteratorMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::Bindings::NodeFilterBinding::NodeFilter;
use dom::bindings::codegen::Bindings::NodeFilterBinding::NodeFilterConstants;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef; use dom::bindings::global::GlobalRef;
use dom::bindings::js::Temporary; use dom::bindings::js::{JS, JSRef, MutHeap, OptionalRootable, Temporary, Rootable, RootedReference};
use dom::bindings::utils::{Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::document::{Document, DocumentHelpers};
use dom::node::{Node, NodeHelpers};
use std::cell::Cell;
#[dom_struct] #[dom_struct]
pub struct NodeIterator { pub struct NodeIterator {
reflector_: Reflector reflector_: Reflector,
root_node: JS<Node>,
reference_node: MutHeap<JS<Node>>,
pointer_before_reference_node: Cell<bool>,
what_to_show: u32,
filter: Filter,
} }
impl NodeIterator { impl NodeIterator {
fn new_inherited() -> NodeIterator { fn new_inherited(root_node: JSRef<Node>,
what_to_show: u32,
filter: Filter) -> NodeIterator {
NodeIterator { NodeIterator {
reflector_: Reflector::new() reflector_: Reflector::new(),
root_node: JS::from_rooted(root_node),
reference_node: MutHeap::new(JS::from_rooted(root_node)),
pointer_before_reference_node: Cell::new(true),
what_to_show: what_to_show,
filter: filter
} }
} }
pub fn new(global: GlobalRef) -> Temporary<NodeIterator> { pub fn new_with_filter(document: JSRef<Document>,
reflect_dom_object(box NodeIterator::new_inherited(), global, NodeIteratorBinding::Wrap) root_node: JSRef<Node>,
what_to_show: u32,
filter: Filter) -> Temporary<NodeIterator> {
let window = document.window().root();
reflect_dom_object(box NodeIterator::new_inherited(root_node, what_to_show, filter),
GlobalRef::Window(window.r()),
NodeIteratorBinding::Wrap)
}
pub fn new(document: JSRef<Document>,
root_node: JSRef<Node>,
what_to_show: u32,
node_filter: Option<NodeFilter>) -> Temporary<NodeIterator> {
let filter = match node_filter {
None => Filter::None,
Some(jsfilter) => Filter::Callback(jsfilter)
};
NodeIterator::new_with_filter(document, root_node, what_to_show, filter)
} }
} }
impl<'a> NodeIteratorMethods for JSRef<'a, NodeIterator> {
// https://dom.spec.whatwg.org/#dom-nodeiterator-root
fn Root(self) -> Temporary<Node> {
Temporary::from_rooted(self.root_node)
}
// https://dom.spec.whatwg.org/#dom-nodeiterator-whattoshow
fn WhatToShow(self) -> u32 {
self.what_to_show
}
// https://dom.spec.whatwg.org/#dom-nodeiterator-filter
fn GetFilter(self) -> Option<NodeFilter> {
match self.filter {
Filter::None => None,
Filter::Callback(nf) => Some(nf),
Filter::Native(_) => panic!("Cannot convert native node filter to DOM NodeFilter")
}
}
// https://dom.spec.whatwg.org/#dom-nodeiterator-referencenode
fn ReferenceNode(self) -> Temporary<Node> {
Temporary::from_rooted(self.reference_node.get())
}
// https://dom.spec.whatwg.org/#dom-nodeiterator-pointerbeforereferencenode
fn PointerBeforeReferenceNode(self) -> bool {
self.pointer_before_reference_node.get()
}
// https://dom.spec.whatwg.org/#dom-nodeiterator-nextnode
fn NextNode(self) -> Fallible<Option<Temporary<Node>>> {
// https://dom.spec.whatwg.org/#concept-NodeIterator-traverse
// Step 1.
let node = self.reference_node.get().root();
// Step 2.
let mut before_node = self.pointer_before_reference_node.get();
// Step 3-1.
if before_node {
before_node = false;
// Step 3-2.
let result = try!(self.accept_node(node.r()));
// Step 3-3.
if result == NodeFilterConstants::FILTER_ACCEPT {
// Step 4.
self.reference_node.set(JS::from_rooted(node.r()));
self.pointer_before_reference_node.set(before_node);
return Ok(Some(Temporary::from_rooted(node.r())));
}
}
// Step 3-1.
for following_node in node.r().following_nodes(self.root_node.root().r()) {
let following_node = following_node.root();
// Step 3-2.
let result = try!(self.accept_node(following_node.r()));
// Step 3-3.
if result == NodeFilterConstants::FILTER_ACCEPT {
// Step 4.
self.reference_node.set(JS::from_rooted(following_node.r()));
self.pointer_before_reference_node.set(before_node);
return Ok(Some(Temporary::from_rooted(following_node.r())));
}
}
return Ok(None);
}
// https://dom.spec.whatwg.org/#dom-nodeiterator-previousnode
fn PreviousNode(self) -> Fallible<Option<Temporary<Node>>> {
// https://dom.spec.whatwg.org/#concept-NodeIterator-traverse
// Step 1.
let node = self.reference_node.get().root();
// Step 2.
let mut before_node = self.pointer_before_reference_node.get();
// Step 3-1.
if !before_node {
before_node = true;
// Step 3-2.
let result = try!(self.accept_node(node.r()));
// Step 3-3.
if result == NodeFilterConstants::FILTER_ACCEPT {
// Step 4.
self.reference_node.set(JS::from_rooted(node.r()));
self.pointer_before_reference_node.set(before_node);
return Ok(Some(Temporary::from_rooted(node.r())));
}
}
// Step 3-1.
for preceding_node in node.r().preceding_nodes(self.root_node.root().r()) {
let preceding_node = preceding_node.root();
// Step 3-2.
let result = try!(self.accept_node(preceding_node.r()));
// Step 3-3.
if result == NodeFilterConstants::FILTER_ACCEPT {
// Step 4.
self.reference_node.set(JS::from_rooted(preceding_node.r()));
self.pointer_before_reference_node.set(before_node);
return Ok(Some(Temporary::from_rooted(preceding_node.r())));
}
}
return Ok(None);
}
// https://dom.spec.whatwg.org/#dom-nodeiterator-detach
fn Detach(self) {
// This method intentionally left blank.
}
}
trait PrivateNodeIteratorHelpers {
fn accept_node(self, node: JSRef<Node>) -> Fallible<u16>;
fn is_root_node(self, node: JSRef<Node>) -> bool;
}
impl<'a> PrivateNodeIteratorHelpers for JSRef<'a, NodeIterator> {
// https://dom.spec.whatwg.org/#concept-node-filter
fn accept_node(self, node: JSRef<Node>) -> Fallible<u16> {
// Step 1.
let n = node.NodeType() - 1;
// Step 2.
if (self.what_to_show & (1 << n)) == 0 {
return Ok(NodeFilterConstants::FILTER_SKIP)
}
// Step 3-5.
match self.filter {
Filter::None => Ok(NodeFilterConstants::FILTER_ACCEPT),
Filter::Native(f) => Ok((f)(node)),
Filter::Callback(callback) => callback.AcceptNode_(self, node, Rethrow)
}
}
fn is_root_node(self, node: JSRef<Node>) -> bool {
JS::from_rooted(node) == self.root_node
}
}
#[jstraceable]
pub enum Filter {
None,
Native(fn (node: JSRef<Node>) -> u16),
Callback(NodeFilter)
}

View file

@ -58,8 +58,8 @@ interface Document : Node {
Range createRange(); Range createRange();
// NodeFilter.SHOW_ALL = 0xFFFFFFFF // NodeFilter.SHOW_ALL = 0xFFFFFFFF
// [NewObject] [NewObject]
// NodeIterator createNodeIterator(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null); NodeIterator createNodeIterator(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null);
[NewObject] [NewObject]
TreeWalker createTreeWalker(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null); TreeWalker createTreeWalker(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null);
}; };

View file

@ -12,21 +12,21 @@
// Import from http://hg.mozilla.org/mozilla-central/raw-file/a5a720259d79/dom/webidl/NodeIterator.webidl // Import from http://hg.mozilla.org/mozilla-central/raw-file/a5a720259d79/dom/webidl/NodeIterator.webidl
interface NodeIterator { interface NodeIterator {
// [Constant] [Constant]
// readonly attribute Node root; readonly attribute Node root;
// [Pure] [Pure]
// readonly attribute Node? referenceNode; readonly attribute Node referenceNode;
// [Pure] [Pure]
// readonly attribute boolean pointerBeforeReferenceNode; readonly attribute boolean pointerBeforeReferenceNode;
// [Constant] [Constant]
// readonly attribute unsigned long whatToShow; readonly attribute unsigned long whatToShow;
// [Constant] [Constant]
// readonly attribute NodeFilter? filter; readonly attribute NodeFilter? filter;
// [Throws] [Throws]
// Node? nextNode(); Node? nextNode();
// [Throws] [Throws]
// Node? previousNode(); Node? previousNode();
// void detach(); void detach();
}; };

View file

@ -102,9 +102,6 @@
[Document interface: attribute origin] [Document interface: attribute origin]
expected: FAIL expected: FAIL
[Document interface: operation createNodeIterator(Node,unsigned long,NodeFilter)]
expected: FAIL
[Document interface: operation query(DOMString)] [Document interface: operation query(DOMString)]
expected: FAIL expected: FAIL
@ -132,12 +129,6 @@
[Document interface: xmlDoc must inherit property "origin" with the proper type (3)] [Document interface: xmlDoc must inherit property "origin" with the proper type (3)]
expected: FAIL expected: FAIL
[Document interface: xmlDoc must inherit property "createNodeIterator" with the proper type (25)]
expected: FAIL
[Document interface: calling createNodeIterator(Node,unsigned long,NodeFilter) on xmlDoc with too few arguments must throw TypeError]
expected: FAIL
[Document interface: xmlDoc must inherit property "query" with the proper type (34)] [Document interface: xmlDoc must inherit property "query" with the proper type (34)]
expected: FAIL expected: FAIL
@ -363,58 +354,7 @@
[Range interface: calling surroundContents(Node) on detachedRange with too few arguments must throw TypeError] [Range interface: calling surroundContents(Node) on detachedRange with too few arguments must throw TypeError]
expected: FAIL expected: FAIL
[NodeIterator interface: attribute root] [NodeIterator interface object length]
expected: FAIL
[NodeIterator interface: attribute referenceNode]
expected: FAIL
[NodeIterator interface: attribute pointerBeforeReferenceNode]
expected: FAIL
[NodeIterator interface: attribute whatToShow]
expected: FAIL
[NodeIterator interface: attribute filter]
expected: FAIL
[NodeIterator interface: operation nextNode()]
expected: FAIL
[NodeIterator interface: operation previousNode()]
expected: FAIL
[NodeIterator interface: operation detach()]
expected: FAIL
[NodeIterator must be primary interface of document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false)]
expected: FAIL
[Stringification of document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false)]
expected: FAIL
[NodeIterator interface: document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false) must inherit property "root" with the proper type (0)]
expected: FAIL
[NodeIterator interface: document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false) must inherit property "referenceNode" with the proper type (1)]
expected: FAIL
[NodeIterator interface: document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false) must inherit property "pointerBeforeReferenceNode" with the proper type (2)]
expected: FAIL
[NodeIterator interface: document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false) must inherit property "whatToShow" with the proper type (3)]
expected: FAIL
[NodeIterator interface: document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false) must inherit property "filter" with the proper type (4)]
expected: FAIL
[NodeIterator interface: document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false) must inherit property "nextNode" with the proper type (5)]
expected: FAIL
[NodeIterator interface: document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false) must inherit property "previousNode" with the proper type (6)]
expected: FAIL
[NodeIterator interface: document.createNodeIterator(document.body, NodeFilter.SHOW_ALL, null, false) must inherit property "detach" with the proper type (7)]
expected: FAIL expected: FAIL
[TreeWalker interface object length] [TreeWalker interface object length]

File diff suppressed because it is too large Load diff

View file

@ -1032,12 +1032,6 @@
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "origin" with the proper type (3)] [Document interface: document.implementation.createDocument(null, "", null) must inherit property "origin" with the proper type (3)]
expected: FAIL expected: FAIL
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "createNodeIterator" with the proper type (25)]
expected: FAIL
[Document interface: calling createNodeIterator(Node,unsigned long,NodeFilter) on document.implementation.createDocument(null, "", null) with too few arguments must throw TypeError]
expected: FAIL
[Document interface: document.implementation.createDocument(null, "", null) must inherit property "styleSheets" with the proper type (27)] [Document interface: document.implementation.createDocument(null, "", null) must inherit property "styleSheets" with the proper type (27)]
expected: FAIL expected: FAIL