servo: Merge #10202 - Added a mapping between nodes and stylesheets (from cbrewster:stylesheet-node-mapping); r=nox
authorConnor Brewster <brewsterc@my.caspercollege.edu>
Thu, 31 Mar 2016 20:18:33 +0500
changeset 338369 73c424e034be2083598f75ec82bc01376379119c
parent 338368 bd477565ee83a2e17536e99a50dc53162dc8a2c7
child 338370 4f452853bc3d132c72b2e982c5ff9e0205a7f37d
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnox
servo: Merge #10202 - Added a mapping between nodes and stylesheets (from cbrewster:stylesheet-node-mapping); r=nox Currently a work in progress solution for #10143. I am not sure how to make the stylesheets() func return a `Ref<Vec<Arc<Stylesheet>>>` or if this way work just as well. If anyone has any feedback, that would be great. Source-Repo: https://github.com/servo/servo Source-Revision: b38fafcf11d2588fecc565c581260106411ad484
servo/components/script/dom/document.rs
servo/components/script/dom/window.rs
--- a/servo/components/script/dom/document.rs
+++ b/servo/components/script/dom/document.rs
@@ -152,17 +152,17 @@ pub struct Document {
     images: MutNullableHeap<JS<HTMLCollection>>,
     embeds: MutNullableHeap<JS<HTMLCollection>>,
     links: MutNullableHeap<JS<HTMLCollection>>,
     forms: MutNullableHeap<JS<HTMLCollection>>,
     scripts: MutNullableHeap<JS<HTMLCollection>>,
     anchors: MutNullableHeap<JS<HTMLCollection>>,
     applets: MutNullableHeap<JS<HTMLCollection>>,
     /// List of stylesheets associated with nodes in this document. |None| if the list needs to be refreshed.
-    stylesheets: DOMRefCell<Option<Vec<Arc<Stylesheet>>>>,
+    stylesheets: DOMRefCell<Option<Vec<(JS<Node>, Arc<Stylesheet>)>>>,
     /// Whether the list of stylesheets has changed since the last reflow was triggered.
     stylesheets_changed_since_reflow: Cell<bool>,
     ready_state: Cell<DocumentReadyState>,
     /// Whether the DOMContentLoaded event has already been dispatched.
     domcontentloaded_dispatched: Cell<bool>,
     /// The element that has most recently requested focus for itself.
     possibly_focused: MutNullableHeap<JS<Element>>,
     /// The element that currently has the document focus context.
@@ -1630,38 +1630,39 @@ impl Document {
         NodeList::new_simple_list(&self.window, iter)
     }
 
     fn get_html_element(&self) -> Option<Root<HTMLHtmlElement>> {
         self.GetDocumentElement().and_then(Root::downcast)
     }
 
     /// Returns the list of stylesheets associated with nodes in the document.
-    pub fn stylesheets(&self) -> Ref<Vec<Arc<Stylesheet>>> {
+    pub fn stylesheets(&self) -> Vec<Arc<Stylesheet>> {
         {
             let mut stylesheets = self.stylesheets.borrow_mut();
             if stylesheets.is_none() {
-                let new_stylesheets: Vec<Arc<Stylesheet>> = self.upcast::<Node>()
+                *stylesheets = Some(self.upcast::<Node>()
                     .traverse_preorder()
                     .filter_map(|node| {
                         if let Some(node) = node.downcast::<HTMLStyleElement>() {
                             node.get_stylesheet()
                         } else if let Some(node) = node.downcast::<HTMLLinkElement>() {
                             node.get_stylesheet()
                         } else if let Some(node) = node.downcast::<HTMLMetaElement>() {
                             node.get_stylesheet()
                         } else {
                             None
-                        }
+                        }.map(|stylesheet| (JS::from_rooted(&node), stylesheet))
                     })
-                    .collect();
-                *stylesheets = Some(new_stylesheets);
+                    .collect());
             };
         }
-        Ref::map(self.stylesheets.borrow(), |t| t.as_ref().unwrap())
+        self.stylesheets.borrow().as_ref().unwrap().iter()
+                        .map(|&(_, ref stylesheet)| stylesheet.clone())
+                        .collect()
     }
 
     /// https://html.spec.whatwg.org/multipage/#appropriate-template-contents-owner-document
     pub fn appropriate_template_contents_owner_document(&self) -> Root<Document> {
         self.appropriate_template_contents_owner_document.or_init(|| {
             let doctype = if self.is_html_document {
                 IsHTMLDocument::HTMLDocument
             } else {
--- a/servo/components/script/dom/window.rs
+++ b/servo/components/script/dom/window.rs
@@ -991,17 +991,17 @@ impl Window {
 
         // Send new document and relevant styles to layout.
         let reflow = ScriptReflow {
             reflow_info: Reflow {
                 goal: goal,
                 page_clip_rect: self.page_clip_rect.get(),
             },
             document: self.Document().upcast::<Node>().to_trusted_node_address(),
-            document_stylesheets: document.stylesheets().clone(),
+            document_stylesheets: document.stylesheets(),
             stylesheets_changed: stylesheets_changed,
             window_size: window_size,
             script_join_chan: join_chan,
             query_type: query_type,
         };
 
         let LayoutChan(ref chan) = self.layout_chan;
         chan.send(Msg::Reflow(reflow)).unwrap();