diff options
-rw-r--r-- | components/script/dom/browsercontext.rs | 2 | ||||
-rw-r--r-- | components/script/dom/document.rs | 90 | ||||
-rw-r--r-- | components/script/dom/domimplementation.rs | 3 | ||||
-rw-r--r-- | components/script/dom/element.rs | 5 | ||||
-rw-r--r-- | components/script/dom/htmlimageelement.rs | 2 | ||||
-rw-r--r-- | components/script/dom/range.rs | 2 | ||||
-rw-r--r-- | components/script/dom/servohtmlparser.rs | 2 | ||||
-rw-r--r-- | components/script/dom/treewalker.rs | 2 |
8 files changed, 51 insertions, 57 deletions
diff --git a/components/script/dom/browsercontext.rs b/components/script/dom/browsercontext.rs index 5984ec3b70e..b663327d5af 100644 --- a/components/script/dom/browsercontext.rs +++ b/components/script/dom/browsercontext.rs @@ -4,7 +4,7 @@ use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::utils::{Reflectable, WindowProxyHandler}; -use dom::document::Document; +use dom::document::{Document, DocumentHelpers}; use dom::window::Window; use js::jsapi::JSObject; diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index f383d66c649..90c0ae5a5c4 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -34,7 +34,7 @@ use dom::documentfragment::DocumentFragment; use dom::documenttype::DocumentType; use dom::domimplementation::DOMImplementation; 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::event::{Event, DoesNotBubble, NotCancelable}; use dom::eventtarget::{EventTarget, NodeTargetTypeId, EventTargetHelpers}; @@ -162,6 +162,9 @@ impl CollectionFilter for AppletsFilter { } 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 quirks_mode(self) -> QuirksMode; fn set_quirks_mode(self, mode: QuirksMode); @@ -178,6 +181,21 @@ pub trait DocumentHelpers<'a> { } 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 { &self.extended_deref().url } @@ -311,6 +329,18 @@ pub enum DocumentSource { 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 { fn new_inherited(window: JSRef<Window>, url: Option<Url>, @@ -376,21 +406,6 @@ impl Document { node.set_owner_doc(*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 { @@ -407,34 +422,20 @@ trait PrivateDocumentHelpers { impl<'a> PrivateDocumentHelpers for JSRef<'a, Document> { fn createNodeList(self, callback: |node: JSRef<Node>| -> bool) -> Temporary<NodeList> { let window = self.window.root(); - - match self.GetDocumentElement().root() { - None => { - NodeList::new_simple_list(*window, vec!()) - }, + let nodes = match self.GetDocumentElement().root() { + None => vec!(), Some(root) => { - let mut nodes = vec!(); let root: JSRef<Node> = NodeCast::from_ref(*root); - for child in root.traverse_preorder() { - if callback(child) { - nodes.push(child); - } - } - NodeList::new_simple_list(*window, nodes) + root.traverse_preorder().filter(|&node| callback(node)).collect() } - } - + }; + NodeList::new_simple_list(*window, nodes) } fn get_html_element(self) -> Option<Temporary<HTMLHtmlElement>> { - match self.GetDocumentElement().root() { - Some(ref root) if { - let root: JSRef<Node> = NodeCast::from_ref(**root); - root.type_id() == ElementNodeTypeId(HTMLHtmlElementTypeId) - } => Some(Temporary::from_rooted(HTMLHtmlElementCast::to_ref(**root).unwrap())), - - _ => None, - } + self.GetDocumentElement().root().and_then(|element| { + HTMLHtmlElementCast::to_ref(*element) + }).map(Temporary::from_rooted) } } @@ -687,11 +688,8 @@ impl<'a> DocumentMethods for JSRef<'a, Document> { root.traverse_preorder() .find(|node| node.type_id() == ElementNodeTypeId(HTMLTitleElementTypeId)) .map(|title_elem| { - for child in title_elem.children() { - if child.is_text() { - let text: JSRef<Text> = TextCast::to_ref(child).unwrap(); - title.push_str(text.characterdata().data().as_slice()); - } + for text in title_elem.children().filter_map::<JSRef<Text>>(TextCast::to_ref) { + title.push_str(text.characterdata().data().as_slice()); } }); }); @@ -742,11 +740,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> { self.get_html_element().and_then(|root| { let root = root.root(); let node: JSRef<Node> = NodeCast::from_ref(*root); - node.children().find(|child| { - child.type_id() == ElementNodeTypeId(HTMLHeadElementTypeId) - }).map(|node| { - Temporary::from_rooted(HTMLHeadElementCast::to_ref(node).unwrap()) - }) + node.children().filter_map(HTMLHeadElementCast::to_ref).next().map(Temporary::from_rooted) }) } diff --git a/components/script/dom/domimplementation.rs b/components/script/dom/domimplementation.rs index 364729721b9..6530a728004 100644 --- a/components/script/dom/domimplementation.rs +++ b/components/script/dom/domimplementation.rs @@ -12,7 +12,8 @@ use dom::bindings::global::Window; use dom::bindings::js::{JS, JSRef, Root, Temporary, OptionalRootable}; use dom::bindings::utils::{Reflector, Reflectable, reflect_dom_object}; 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::htmlbodyelement::HTMLBodyElement; use dom::htmlheadelement::HTMLHeadElement; diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index f71e9626b29..08ad02287fa 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -21,7 +21,7 @@ use dom::bindings::error::{ErrorResult, Fallible, NamespaceError, InvalidCharact use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type}; use dom::domrect::DOMRect; use dom::domrectlist::DOMRectList; -use dom::document::{Document, DocumentHelpers}; +use dom::document::{Document, DocumentHelpers, LayoutDocumentHelpers}; use dom::domtokenlist::DOMTokenList; use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::htmlcollection::HTMLCollection; @@ -335,8 +335,7 @@ impl LayoutElementHelpers for JS<Element> { return false } let node: JS<Node> = self.transmute_copy(); - let owner_doc = node.owner_doc_for_layout().unsafe_get(); - (*owner_doc).is_html_document() + node.owner_doc_for_layout().is_html_document_for_layout() } } diff --git a/components/script/dom/htmlimageelement.rs b/components/script/dom/htmlimageelement.rs index 94028b638cf..bc0e58db9be 100644 --- a/components/script/dom/htmlimageelement.rs +++ b/components/script/dom/htmlimageelement.rs @@ -10,7 +10,7 @@ use dom::bindings::codegen::Bindings::HTMLImageElementBinding::HTMLImageElementM use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast, HTMLElementCast, HTMLImageElementDerived}; use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::utils::{Reflectable, Reflector}; -use dom::document::Document; +use dom::document::{Document, DocumentHelpers}; use dom::element::{Element, HTMLImageElementTypeId}; use dom::element::AttributeHandlers; use dom::eventtarget::{EventTarget, NodeTargetTypeId}; diff --git a/components/script/dom/range.rs b/components/script/dom/range.rs index 99b48cea700..8bb34143183 100644 --- a/components/script/dom/range.rs +++ b/components/script/dom/range.rs @@ -9,7 +9,7 @@ use dom::bindings::error::Fallible; use dom::bindings::global::{GlobalRef, Window}; use dom::bindings::js::{JSRef, Temporary}; use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; -use dom::document::Document; +use dom::document::{Document, DocumentHelpers}; #[dom_struct] pub struct Range { diff --git a/components/script/dom/servohtmlparser.rs b/components/script/dom/servohtmlparser.rs index 8bd33ae1efa..6b81ab512c5 100644 --- a/components/script/dom/servohtmlparser.rs +++ b/components/script/dom/servohtmlparser.rs @@ -12,7 +12,7 @@ use dom::bindings::trace::JSTraceable; use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; use dom::node::TrustedNodeAddress; -use dom::document::Document; +use dom::document::{Document, DocumentHelpers}; use parse::html::JSMessage; use std::default::Default; diff --git a/components/script/dom/treewalker.rs b/components/script/dom/treewalker.rs index 44a62f60baa..78d708a4c4b 100644 --- a/components/script/dom/treewalker.rs +++ b/components/script/dom/treewalker.rs @@ -15,7 +15,7 @@ use dom::bindings::error::{ErrorResult, Fallible}; use dom::bindings::global::Window; use dom::bindings::js::{JS, JSRef, OptionalRootable, Temporary}; use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; -use dom::document::Document; +use dom::document::{Document, DocumentHelpers}; use dom::node::{Node, NodeHelpers}; use std::cell::Cell; |