auto merge of #3780 : Ms2ger/servo/cleanup-document, r=jdm

This commit is contained in:
bors-servo 2014-10-23 11:36:40 -06:00
commit 43b13e7548
8 changed files with 51 additions and 57 deletions

View file

@ -4,7 +4,7 @@
use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::js::{JS, JSRef, Temporary};
use dom::bindings::utils::{Reflectable, WindowProxyHandler}; use dom::bindings::utils::{Reflectable, WindowProxyHandler};
use dom::document::Document; use dom::document::{Document, DocumentHelpers};
use dom::window::Window; use dom::window::Window;
use js::jsapi::JSObject; use js::jsapi::JSObject;

View file

@ -34,7 +34,7 @@ use dom::documentfragment::DocumentFragment;
use dom::documenttype::DocumentType; use dom::documenttype::DocumentType;
use dom::domimplementation::DOMImplementation; use dom::domimplementation::DOMImplementation;
use dom::element::{Element, AttributeHandlers, get_attribute_parts}; use dom::element::{Element, AttributeHandlers, get_attribute_parts};
use dom::element::{HTMLHtmlElementTypeId, HTMLHeadElementTypeId, HTMLTitleElementTypeId}; use dom::element::{HTMLHeadElementTypeId, HTMLTitleElementTypeId};
use dom::element::{HTMLBodyElementTypeId, HTMLFrameSetElementTypeId}; use dom::element::{HTMLBodyElementTypeId, HTMLFrameSetElementTypeId};
use dom::event::{Event, DoesNotBubble, NotCancelable}; use dom::event::{Event, DoesNotBubble, NotCancelable};
use dom::eventtarget::{EventTarget, NodeTargetTypeId, EventTargetHelpers}; use dom::eventtarget::{EventTarget, NodeTargetTypeId, EventTargetHelpers};
@ -162,6 +162,9 @@ impl CollectionFilter for AppletsFilter {
} }
pub trait DocumentHelpers<'a> { pub trait DocumentHelpers<'a> {
fn window(self) -> Temporary<Window>;
fn encoding_name(self) -> Ref<'a, DOMString>;
fn is_html_document(self) -> bool;
fn url(self) -> &'a Url; fn url(self) -> &'a Url;
fn quirks_mode(self) -> QuirksMode; fn quirks_mode(self) -> QuirksMode;
fn set_quirks_mode(self, mode: QuirksMode); fn set_quirks_mode(self, mode: QuirksMode);
@ -178,6 +181,21 @@ pub trait DocumentHelpers<'a> {
} }
impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> { impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
#[inline]
fn window(self) -> Temporary<Window> {
Temporary::new(self.window)
}
#[inline]
fn encoding_name(self) -> Ref<'a, DOMString> {
self.extended_deref().encoding_name.borrow()
}
#[inline]
fn is_html_document(self) -> bool {
self.is_html_document
}
fn url(self) -> &'a Url { fn url(self) -> &'a Url {
&self.extended_deref().url &self.extended_deref().url
} }
@ -311,6 +329,18 @@ pub enum DocumentSource {
NotFromParser, NotFromParser,
} }
pub trait LayoutDocumentHelpers {
unsafe fn is_html_document_for_layout(&self) -> bool;
}
impl LayoutDocumentHelpers for JS<Document> {
#[allow(unrooted_must_root)]
#[inline]
unsafe fn is_html_document_for_layout(&self) -> bool {
(*self.unsafe_get()).is_html_document
}
}
impl Document { impl Document {
fn new_inherited(window: JSRef<Window>, fn new_inherited(window: JSRef<Window>,
url: Option<Url>, url: Option<Url>,
@ -376,21 +406,6 @@ impl Document {
node.set_owner_doc(*document); node.set_owner_doc(*document);
Temporary::from_rooted(*document) Temporary::from_rooted(*document)
} }
#[inline]
pub fn window(&self) -> Temporary<Window> {
Temporary::new(self.window)
}
#[inline]
pub fn encoding_name(&self) -> Ref<DOMString> {
self.encoding_name.borrow()
}
#[inline]
pub fn is_html_document(&self) -> bool {
self.is_html_document
}
} }
impl Reflectable for Document { impl Reflectable for Document {
@ -407,34 +422,20 @@ trait PrivateDocumentHelpers {
impl<'a> PrivateDocumentHelpers for JSRef<'a, Document> { impl<'a> PrivateDocumentHelpers for JSRef<'a, Document> {
fn createNodeList(self, callback: |node: JSRef<Node>| -> bool) -> Temporary<NodeList> { fn createNodeList(self, callback: |node: JSRef<Node>| -> bool) -> Temporary<NodeList> {
let window = self.window.root(); let window = self.window.root();
let nodes = match self.GetDocumentElement().root() {
match self.GetDocumentElement().root() { None => vec!(),
None => {
NodeList::new_simple_list(*window, vec!())
},
Some(root) => { Some(root) => {
let mut nodes = vec!();
let root: JSRef<Node> = NodeCast::from_ref(*root); let root: JSRef<Node> = NodeCast::from_ref(*root);
for child in root.traverse_preorder() { root.traverse_preorder().filter(|&node| callback(node)).collect()
if callback(child) {
nodes.push(child);
}
} }
};
NodeList::new_simple_list(*window, nodes) NodeList::new_simple_list(*window, nodes)
} }
}
}
fn get_html_element(self) -> Option<Temporary<HTMLHtmlElement>> { fn get_html_element(self) -> Option<Temporary<HTMLHtmlElement>> {
match self.GetDocumentElement().root() { self.GetDocumentElement().root().and_then(|element| {
Some(ref root) if { HTMLHtmlElementCast::to_ref(*element)
let root: JSRef<Node> = NodeCast::from_ref(**root); }).map(Temporary::from_rooted)
root.type_id() == ElementNodeTypeId(HTMLHtmlElementTypeId)
} => Some(Temporary::from_rooted(HTMLHtmlElementCast::to_ref(**root).unwrap())),
_ => None,
}
} }
} }
@ -687,12 +688,9 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
root.traverse_preorder() root.traverse_preorder()
.find(|node| node.type_id() == ElementNodeTypeId(HTMLTitleElementTypeId)) .find(|node| node.type_id() == ElementNodeTypeId(HTMLTitleElementTypeId))
.map(|title_elem| { .map(|title_elem| {
for child in title_elem.children() { for text in title_elem.children().filter_map::<JSRef<Text>>(TextCast::to_ref) {
if child.is_text() {
let text: JSRef<Text> = TextCast::to_ref(child).unwrap();
title.push_str(text.characterdata().data().as_slice()); title.push_str(text.characterdata().data().as_slice());
} }
}
}); });
}); });
let v: Vec<&str> = split_html_space_chars(title.as_slice()).collect(); let v: Vec<&str> = split_html_space_chars(title.as_slice()).collect();
@ -742,11 +740,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
self.get_html_element().and_then(|root| { self.get_html_element().and_then(|root| {
let root = root.root(); let root = root.root();
let node: JSRef<Node> = NodeCast::from_ref(*root); let node: JSRef<Node> = NodeCast::from_ref(*root);
node.children().find(|child| { node.children().filter_map(HTMLHeadElementCast::to_ref).next().map(Temporary::from_rooted)
child.type_id() == ElementNodeTypeId(HTMLHeadElementTypeId)
}).map(|node| {
Temporary::from_rooted(HTMLHeadElementCast::to_ref(node).unwrap())
})
}) })
} }

View file

@ -12,7 +12,8 @@ use dom::bindings::global::Window;
use dom::bindings::js::{JS, JSRef, Root, Temporary, OptionalRootable}; use dom::bindings::js::{JS, JSRef, Root, Temporary, OptionalRootable};
use dom::bindings::utils::{Reflector, Reflectable, reflect_dom_object}; use dom::bindings::utils::{Reflector, Reflectable, reflect_dom_object};
use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type}; use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type};
use dom::document::{Document, HTMLDocument, NonHTMLDocument, NotFromParser}; use dom::document::{Document, DocumentHelpers, HTMLDocument, NonHTMLDocument};
use dom::document::NotFromParser;
use dom::documenttype::DocumentType; use dom::documenttype::DocumentType;
use dom::htmlbodyelement::HTMLBodyElement; use dom::htmlbodyelement::HTMLBodyElement;
use dom::htmlheadelement::HTMLHeadElement; use dom::htmlheadelement::HTMLHeadElement;

View file

@ -21,7 +21,7 @@ use dom::bindings::error::{ErrorResult, Fallible, NamespaceError, InvalidCharact
use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type}; use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type};
use dom::domrect::DOMRect; use dom::domrect::DOMRect;
use dom::domrectlist::DOMRectList; use dom::domrectlist::DOMRectList;
use dom::document::{Document, DocumentHelpers}; use dom::document::{Document, DocumentHelpers, LayoutDocumentHelpers};
use dom::domtokenlist::DOMTokenList; use dom::domtokenlist::DOMTokenList;
use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlcollection::HTMLCollection; use dom::htmlcollection::HTMLCollection;
@ -335,8 +335,7 @@ impl LayoutElementHelpers for JS<Element> {
return false return false
} }
let node: JS<Node> = self.transmute_copy(); let node: JS<Node> = self.transmute_copy();
let owner_doc = node.owner_doc_for_layout().unsafe_get(); node.owner_doc_for_layout().is_html_document_for_layout()
(*owner_doc).is_html_document()
} }
} }

View file

@ -10,7 +10,7 @@ use dom::bindings::codegen::Bindings::HTMLImageElementBinding::HTMLImageElementM
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast, HTMLElementCast, HTMLImageElementDerived}; use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast, HTMLElementCast, HTMLImageElementDerived};
use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::js::{JS, JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector}; use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::Document; use dom::document::{Document, DocumentHelpers};
use dom::element::{Element, HTMLImageElementTypeId}; use dom::element::{Element, HTMLImageElementTypeId};
use dom::element::AttributeHandlers; use dom::element::AttributeHandlers;
use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::eventtarget::{EventTarget, NodeTargetTypeId};

View file

@ -9,7 +9,7 @@ use dom::bindings::error::Fallible;
use dom::bindings::global::{GlobalRef, Window}; use dom::bindings::global::{GlobalRef, Window};
use dom::bindings::js::{JSRef, Temporary}; use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::document::Document; use dom::document::{Document, DocumentHelpers};
#[dom_struct] #[dom_struct]
pub struct Range { pub struct Range {

View file

@ -12,7 +12,7 @@ use dom::bindings::trace::JSTraceable;
use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::js::{JS, JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::node::TrustedNodeAddress; use dom::node::TrustedNodeAddress;
use dom::document::Document; use dom::document::{Document, DocumentHelpers};
use parse::html::JSMessage; use parse::html::JSMessage;
use std::default::Default; use std::default::Default;

View file

@ -15,7 +15,7 @@ use dom::bindings::error::{ErrorResult, Fallible};
use dom::bindings::global::Window; use dom::bindings::global::Window;
use dom::bindings::js::{JS, JSRef, OptionalRootable, Temporary}; use dom::bindings::js::{JS, JSRef, OptionalRootable, Temporary};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::document::Document; use dom::document::{Document, DocumentHelpers};
use dom::node::{Node, NodeHelpers}; use dom::node::{Node, NodeHelpers};
use std::cell::Cell; use std::cell::Cell;