Skip to content

Commit

Permalink
Implement document_from_node and window_from_node helpers.(fixes #1761)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpy committed Mar 1, 2014
1 parent ea29e3a commit 77938bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/components/script/dom/element.rs
Expand Up @@ -21,7 +21,7 @@ use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlimageelement::HTMLImageElement;
use dom::htmliframeelement::HTMLIFrameElement;
use dom::htmlobjectelement::HTMLObjectElement;
use dom::node::{ElementNodeTypeId, Node, NodeHelpers, NodeIterator};
use dom::node::{ElementNodeTypeId, Node, NodeHelpers, NodeIterator, document_from_node};
use dom::htmlserializer::serialize;
use layout_interface::{ContentBoxQuery, ContentBoxResponse, ContentBoxesQuery};
use layout_interface::{ContentBoxesResponse, ContentChangedDocumentDamage};
Expand Down Expand Up @@ -586,7 +586,7 @@ impl IElement for JS<Element> {
fn bind_to_tree_impl(&self) {
match self.get().get_attribute(Null, "id") {
Some(attr) => {
let mut doc = self.get().node.owner_doc();
let mut doc = document_from_node(self);
doc.get_mut().register_named_element(self, attr.get().Value());
}
_ => ()
Expand All @@ -596,7 +596,7 @@ impl IElement for JS<Element> {
fn unbind_from_tree_impl(&self) {
match self.get().get_attribute(Null, "id") {
Some(attr) => {
let mut doc = self.get().node.owner_doc();
let mut doc = document_from_node(self);
doc.get_mut().unregister_named_element(attr.get().Value());
}
_ => ()
Expand Down
6 changes: 3 additions & 3 deletions src/components/script/dom/htmlimageelement.rs
Expand Up @@ -11,7 +11,7 @@ use dom::document::Document;
use dom::element::{Element, HTMLImageElementTypeId};
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::node::{Node, ElementNodeTypeId, NodeHelpers};
use dom::node::{Node, ElementNodeTypeId, NodeHelpers, window_from_node};
use extra::url::Url;
use servo_util::geometry::to_px;
use layout_interface::{ContentBoxQuery, ContentBoxResponse};
Expand Down Expand Up @@ -146,8 +146,8 @@ impl HTMLImageElement {

pub fn Width(&self, abstract_self: &JS<HTMLImageElement>) -> u32 {
let node: JS<Node> = NodeCast::from(abstract_self);
let doc = node.get().owner_doc();
let page = doc.get().window.get().page();
let window = window_from_node(&node);
let page = window.get().page();
let (port, chan) = Chan::new();
let addr = node.to_trusted_node_address();
match page.query_layout(ContentBoxQuery(addr, chan), port) {
Expand Down
22 changes: 16 additions & 6 deletions src/components/script/dom/node.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 => (),
}

Expand Down Expand Up @@ -1381,7 +1382,7 @@ impl Node {
};

// Step 9.
Node::adopt(node, &parent.get().owner_doc());
Node::adopt(node, &document_from_node(parent));

{
// Step 10.
Expand Down Expand Up @@ -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()
}

5 comments on commit 77938bf

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging lpy/servo/issue1761 = 77938bf into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lpy/servo/issue1761 = 77938bf merged ok, testing candidate = 478c9bf

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 478c9bf

Please sign in to comment.