Implement document_from_node and window_from_node helpers.(fixes #1761)

This commit is contained in:
lpy 2014-03-01 14:24:41 +08:00
parent ea29e3a001
commit 77938bf6f6
3 changed files with 22 additions and 12 deletions

View file

@ -19,6 +19,7 @@ use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::nodelist::{NodeList};
use dom::text::Text;
use dom::processinginstruction::ProcessingInstruction;
use dom::window::Window;
use layout_interface::{LayoutChan, ReapLayoutDataMsg, UntrustedNodeAddress};
use layout_interface::TrustedNodeAddress;
use servo_util::str::{DOMString, null_str_as_empty};
@ -396,7 +397,7 @@ impl NodeHelpers for JS<Node> {
// http://dom.spec.whatwg.org/#node-is-inserted
fn node_inserted(&self) {
assert!(self.parent_node().is_some());
let document = self.get().owner_doc();
let document = document_from_node(self);
for node in self.traverse_preorder() {
if node.is_element() {
@ -411,7 +412,7 @@ impl NodeHelpers for JS<Node> {
// http://dom.spec.whatwg.org/#node-is-removed
fn node_removed(&self) {
assert!(self.parent_node().is_none());
let document = self.get().owner_doc();
let document = document_from_node(self);
for node in self.traverse_preorder() {
if node.is_element() {
@ -964,7 +965,7 @@ impl Node {
}
// Step 2.
if node.get().owner_doc() != *document {
if document_from_node(node) != *document {
for mut descendant in node.traverse_preorder() {
descendant.get_mut().set_owner_doc(document);
}
@ -1101,7 +1102,7 @@ impl Node {
};
// Step 9.
Node::adopt(node, &parent.get().owner_doc());
Node::adopt(node, &document_from_node(parent));
// Step 10.
Node::insert(node, parent, referenceChild, Unsuppressed);
@ -1156,7 +1157,7 @@ impl Node {
pub fn replace_all(mut node: Option<JS<Node>>, parent: &mut JS<Node>) {
// Step 1.
match node {
Some(ref mut node) => Node::adopt(node, &parent.get().owner_doc()),
Some(ref mut node) => Node::adopt(node, &document_from_node(parent)),
None => (),
}
@ -1381,7 +1382,7 @@ impl Node {
};
// Step 9.
Node::adopt(node, &parent.get().owner_doc());
Node::adopt(node, &document_from_node(parent));
{
// Step 10.
@ -1627,3 +1628,12 @@ impl Reflectable for Node {
}
}
pub fn document_from_node<T: NodeBase>(derived: &JS<T>) -> JS<Document> {
let node: JS<Node> = NodeCast::from(derived);
node.get().owner_doc().clone()
}
pub fn window_from_node<T: NodeBase>(derived: &JS<T>) -> JS<Window> {
let document: JS<Document> = document_from_node(derived);
document.get().window.clone()
}