diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index 59a24c879c8..bd323e15f85 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -41,7 +41,7 @@ pub trait ReflectableDocument { #[deriving(Eq)] pub struct AbstractDocument { - document: *Document + document: *mut Box } impl AbstractDocument { @@ -52,6 +52,18 @@ impl AbstractDocument { } } + pub fn document<'a>(&'a self) -> &'a Document { + unsafe { + &(*self.document).data + } + } + + pub fn mut_document<'a>(&'a self) -> &'a mut Document { + unsafe { + &mut (*self.document).data + } + } + unsafe fn transmute(&self, f: &fn(&T) -> R) -> R { let box: *Box = cast::transmute(self.document); f(&(*box).data) @@ -62,20 +74,8 @@ impl AbstractDocument { f(&mut (*box).data) } - pub fn with_base(&self, callback: &fn(&Document) -> R) -> R { - unsafe { - self.transmute(callback) - } - } - - pub fn with_mut_base(&self, callback: &fn(&mut Document) -> R) -> R { - unsafe { - self.transmute_mut(callback) - } - } - pub fn with_html(&self, callback: &fn(&HTMLDocument) -> R) -> R { - match self.with_base(|doc| doc.doctype) { + match self.document().doctype { HTML => unsafe { self.transmute(callback) }, _ => fail!("attempt to downcast a non-HTMLDocument to HTMLDocument") } @@ -83,7 +83,7 @@ impl AbstractDocument { pub fn from_box(ptr: *mut Box) -> AbstractDocument { AbstractDocument { - document: ptr as *Document + document: ptr as *mut Box } } @@ -91,13 +91,12 @@ impl AbstractDocument { assert!(do root.traverse_preorder().all |node| { node.node().owner_doc() == *self }); - self.with_mut_base(|document| { - document.root = Some(root); - // Register elements having "id" attribute to the owner doc. - document.register_nodes_with_id(&root); - document.content_changed(); - }); + let document = self.mut_document(); + document.root = Some(root); + // Register elements having "id" attribute to the owner doc. + document.register_nodes_with_id(&root); + document.content_changed(); } } @@ -152,19 +151,15 @@ impl ReflectableDocument for Document { impl Reflectable for AbstractDocument { fn reflector<'a>(&'a self) -> &'a Reflector { - do self.with_mut_base |doc| { - unsafe { cast::transmute(doc.reflector()) } - } + self.document().reflector() } fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector { - do self.with_mut_base |doc| { - unsafe { cast::transmute(doc.mut_reflector()) } - } + self.mut_document().mut_reflector() } fn wrap_object_shared(@mut self, cx: *JSContext, scope: *JSObject) -> *JSObject { - match self.with_base(|doc| doc.doctype) { + match self.document().doctype { HTML => { let doc: @mut HTMLDocument = unsafe { cast::transmute(self.document) }; doc.wrap_object_shared(cx, scope) @@ -176,9 +171,7 @@ impl Reflectable for AbstractDocument { } fn GetParentObject(&self, cx: *JSContext) -> Option<@mut Reflectable> { - do self.with_mut_base |doc| { - doc.GetParentObject(cx) - } + self.document().GetParentObject(cx) } } diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs index 718e358762a..2ee2e8ade5a 100644 --- a/src/components/script/dom/element.rs +++ b/src/components/script/dom/element.rs @@ -199,9 +199,8 @@ impl<'self> Element { } if abstract_self.is_in_doc() { - do self.node.owner_doc().with_base |owner| { - owner.content_changed(); - } + let document = self.node.owner_doc(); + document.document().content_changed(); } } } @@ -286,8 +285,7 @@ impl Element { } pub fn GetClientRects(&self, abstract_self: AbstractNode) -> @mut ClientRectList { - let document = self.node.owner_doc(); - let win = document.with_base(|doc| doc.window); + let win = self.node.owner_doc().document().window; let node = abstract_self; assert!(node.is_element()); let (port, chan) = comm::stream(); @@ -313,8 +311,7 @@ impl Element { } pub fn GetBoundingClientRect(&self, abstract_self: AbstractNode) -> @mut ClientRect { - let document = self.node.owner_doc(); - let win = document.with_base(|doc| doc.window); + let win = self.node.owner_doc().document().window; let node = abstract_self; assert!(node.is_element()); let (port, chan) = comm::stream(); diff --git a/src/components/script/dom/htmlimageelement.rs b/src/components/script/dom/htmlimageelement.rs index d9d301b6dfe..dc60d560743 100644 --- a/src/components/script/dom/htmlimageelement.rs +++ b/src/components/script/dom/htmlimageelement.rs @@ -43,12 +43,10 @@ impl HTMLImageElement { pub fn AfterSetAttr(&mut self, name: &DOMString, _value: &DOMString) { let name = null_str_as_empty(name); if "src" == name { - let doc = self.htmlelement.element.node.owner_doc(); - do doc.with_base |doc| { - let window = doc.window; - let url = window.page.url.map(|&(ref url, _)| url.clone()); - self.update_image(window.image_cache_task.clone(), url); - } + let document = self.htmlelement.element.node.owner_doc(); + let window = document.document().window; + let url = window.page.url.map(|&(ref url, _)| url.clone()); + self.update_image(window.image_cache_task.clone(), url); } } @@ -100,7 +98,7 @@ impl HTMLImageElement { pub fn Width(&self, abstract_self: AbstractNode) -> u32 { let node = &self.htmlelement.element.node; - let page = node.owner_doc().with_base(|doc| doc.window).page; + let page = node.owner_doc().document().window.page; let (port, chan) = stream(); match page.query_layout(ContentBoxQuery(abstract_self, chan), port) { ContentBoxResponse(rect) => { @@ -121,7 +119,7 @@ impl HTMLImageElement { pub fn Height(&self, abstract_self: AbstractNode) -> u32 { let node = &self.htmlelement.element.node; - let page = node.owner_doc().with_base(|doc| doc.window).page; + let page = node.owner_doc().document().window.page; let (port, chan) = stream(); match page.query_layout(ContentBoxQuery(abstract_self, chan), port) { ContentBoxResponse(rect) => { diff --git a/src/components/script/dom/node.rs b/src/components/script/dom/node.rs index 6583cf6985b..6169864edac 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -428,26 +428,25 @@ impl<'self, View> AbstractNode { // Issue #1030: should not walk the tree pub fn is_in_doc(&self) -> bool { - do self.node().owner_doc().with_base |document| { - match document.GetDocumentElement() { - None => false, - Some(root) => { - let mut node = *self; - let mut in_doc; - loop { - match node.parent_node() { - Some(parent) => { - node = parent; - }, - None => { - // Issue #1029: this is horrible. - in_doc = unsafe { node.raw_object() as uint == root.raw_object() as uint }; - break; - } + let document = self.node().owner_doc(); + match document.document().GetDocumentElement() { + None => false, + Some(root) => { + let mut node = *self; + let mut in_doc; + loop { + match node.parent_node() { + Some(parent) => { + node = parent; + }, + None => { + // Issue #1029: this is horrible. + in_doc = unsafe { node.raw_object() as uint == root.raw_object() as uint }; + break; } } - in_doc } + in_doc } } } @@ -493,26 +492,18 @@ impl Node { } // Unregister elements having "id' from the old doc. - do old_doc.with_mut_base |old_doc| { - old_doc.unregister_nodes_with_id(&abstract_self); - } + old_doc.mut_document().unregister_nodes_with_id(&abstract_self); // Register elements having "id" attribute to the owner doc. - do doc.with_mut_base |doc| { - doc.register_nodes_with_id(&abstract_self); - } + doc.mut_document().register_nodes_with_id(&abstract_self); // Signal the old document that it needs to update its display if old_doc != doc { - do old_doc.with_base |old_doc| { - old_doc.content_changed(); - } + old_doc.document().content_changed(); } // Signal the new document that it needs to update its display - do doc.with_base |doc| { - doc.content_changed(); - } + doc.document().content_changed(); } pub fn new(type_id: NodeTypeId, doc: AbstractDocument) -> Node { @@ -682,7 +673,7 @@ impl Node { } pub fn get_scope_and_cx(&self) -> (*JSObject, *JSContext) { - let win = self.owner_doc().with_base(|doc| doc.window); + let win = self.owner_doc().document().window; (win.reflector().get_jsobject(), win.get_cx()) } @@ -714,10 +705,8 @@ impl Node { let node = if is_empty { None } else { - let text_node = do self.owner_doc().with_base |document| { - document.CreateTextNode(self.owner_doc(), value) - }; - Some(text_node) + let document = self.owner_doc(); + Some(document.document().CreateTextNode(document, value)) }; self.replace_all(abstract_self, node); } @@ -728,9 +717,8 @@ impl Node { characterdata.data = null_str_as_empty(value); // Notify the document that the content of this node is different - do self.owner_doc().with_base |doc| { - doc.content_changed(); - } + let document = self.owner_doc(); + document.document().content_changed(); } } DoctypeNodeTypeId => {} @@ -743,9 +731,8 @@ impl Node { } fn wait_until_safe_to_modify_dom(&self) { - do self.owner_doc().with_base |doc| { - doc.wait_until_safe_to_modify_dom(); - } + let document = self.owner_doc(); + document.document().wait_until_safe_to_modify_dom(); } pub fn AppendChild(&mut self, @@ -810,15 +797,12 @@ impl Node { // Unregister elements having "id' from the owner doc. // This need be called before target nodes are removed from tree. - do self.owner_doc.with_mut_base |doc| { - doc.unregister_nodes_with_id(&abstract_self); - } + self.owner_doc.mut_document().unregister_nodes_with_id(&abstract_self); abstract_self.remove_child(node); // Signal the document that it needs to update its display. - do self.owner_doc().with_base |document| { - document.content_changed(); - } + let document = self.owner_doc(); + document.document().content_changed(); Ok(node) } diff --git a/src/components/script/dom/window.rs b/src/components/script/dom/window.rs index 82d098d9b22..2862e06855b 100644 --- a/src/components/script/dom/window.rs +++ b/src/components/script/dom/window.rs @@ -253,16 +253,14 @@ impl Traceable for Window { unsafe { match self.page.frame { Some(frame) => { - do frame.document.with_base |doc| { - (*tracer).debugPrinter = ptr::null(); - (*tracer).debugPrintIndex = -1; - do "document".to_c_str().with_ref |name| { - (*tracer).debugPrintArg = name as *libc::c_void; - debug!("tracing document"); - JS_CallTracer(tracer as *JSTracer, - doc.reflector_.object, - JSTRACE_OBJECT as u32); - } + (*tracer).debugPrinter = ptr::null(); + (*tracer).debugPrintIndex = -1; + do "document".to_c_str().with_ref |name| { + (*tracer).debugPrintArg = name as *libc::c_void; + debug!("tracing document"); + JS_CallTracer(tracer as *JSTracer, + frame.document.reflector().get_jsobject(), + JSTRACE_OBJECT as u32); } } None => () diff --git a/src/components/script/script_task.rs b/src/components/script/script_task.rs index e025fdfc5d5..43a87eca228 100644 --- a/src/components/script/script_task.rs +++ b/src/components/script/script_task.rs @@ -214,9 +214,8 @@ impl<'self> Iterator<@mut Page> for PageTreeIterator<'self> { impl Page { /// Adds the given damage. fn damage(&mut self, level: DocumentDamageLevel) { - let root = do self.frame.get_ref().document.with_base |doc| { - doc.GetDocumentElement() - }; + let root = self.frame.get_ref().document.document(). + GetDocumentElement(); match root { None => {}, Some(root) => { @@ -277,9 +276,7 @@ impl Page { let root = match self.frame { None => fail!(~"Tried to relayout with no root frame!"), Some(ref frame) => { - do frame.document.with_base |doc| { - doc.GetDocumentElement() - } + frame.document.document().GetDocumentElement() } }; match root { @@ -804,9 +801,8 @@ impl ScriptTask { ClickEvent(_button, point) => { debug!("ClickEvent: clicked at %?", point); - let root = do page.frame.expect("root frame is None").document.with_base |doc| { - doc.GetDocumentElement() - }; + let document = page.frame.expect("root frame is None").document; + let root = document.document().GetDocumentElement(); if root.is_none() { return; }