Bug 1278823 - styleeditor: fix csscoverage report creation;r=jryans draft
authorJulian Descottes <jdescottes@mozilla.com>
Tue, 28 Jun 2016 11:18:39 +0200
changeset 381811 4949f86776f43fdf6e3ba5d34c4f17bc234a74d3
parent 381810 6a98de1fe670ac29765b990986cbccd03ce3893e
child 524038 bc5e151b55bcdba35963398b9a912024bb46dce5
push id21559
push userjdescottes@mozilla.com
push dateTue, 28 Jun 2016 09:19:30 +0000
reviewersjryans
bugs1278823
milestone50.0a1
Bug 1278823 - styleeditor: fix csscoverage report creation;r=jryans The sheetToUrl function in csscoverage is only used to create ids for the csscoverage map of knownRules. Instead of asking the UI to format stylesheet URLs using the same logic as the server, StyleEditor.jsm now sends the stylesheet actor to create the report. The csscoverage actor can then compute the stylesheet URL on the server. MozReview-Commit-ID: GDtWhbi2ScW
devtools/client/styleeditor/StyleEditorUI.jsm
devtools/server/actors/csscoverage.js
devtools/shared/specs/csscoverage.js
--- a/devtools/client/styleeditor/StyleEditorUI.jsm
+++ b/devtools/client/styleeditor/StyleEditorUI.jsm
@@ -611,29 +611,29 @@ StyleEditorUI.prototype = {
           this.emit("editor-selected", showEditor);
 
           // Is there any CSS coverage markup to include?
           let usage = yield csscoverage.getUsage(this._target);
           if (usage == null) {
             return;
           }
 
-          let href = csscoverage.sheetToUrl(showEditor.styleSheet);
-          let reportData = yield usage.createEditorReport(href);
+          let sheet = showEditor.styleSheet;
+          let {reports} = yield usage.createEditorReportForSheet(sheet);
 
           showEditor.removeAllUnusedRegions();
 
-          if (reportData.reports.length > 0) {
+          if (reports.length > 0) {
             // Only apply if this file isn't compressed. We detect a
             // compressed file if there are more rules than lines.
             let editorText = showEditor.sourceEditor.getText();
             let lineCount = editorText.split("\n").length;
             let ruleCount = showEditor.styleSheet.ruleCount;
             if (lineCount >= ruleCount) {
-              showEditor.addUnusedRegions(reportData.reports);
+              showEditor.addUnusedRegions(reports);
             } else {
               this.emit("error", { key: "error-compressed", level: "info" });
             }
           }
         }.bind(this)).then(null, e => console.error(e));
       }.bind(this)
     });
   },
--- a/devtools/server/actors/csscoverage.js
+++ b/devtools/server/actors/csscoverage.js
@@ -335,16 +335,28 @@ var CSSUsageActor = protocol.ActorClassW
 
       reports.push(ruleReport);
     }
 
     return { reports: reports };
   },
 
   /**
+   * Compute the stylesheet URL and delegate the report creation to createEditorReport.
+   * See createEditorReport documentation.
+   *
+   * @param {StyleSheetActor} stylesheetActor
+   *        the stylesheet actor for which the coverage report should be generated.
+   */
+  createEditorReportForSheet: function (stylesheetActor) {
+    let url = sheetToUrl(stylesheetActor.rawSheet);
+    return this.createEditorReport(url);
+  },
+
+  /**
    * Returns a JSONable structure designed for the page report which shows
    * the recommended changes to a page.
    *
    * "preload" means that a rule is used before the load event happens, which
    * means that the page could by optimized by placing it in a <style> element
    * at the top of the page, moving the <link> elements to the bottom.
    *
    * Example:
@@ -689,31 +701,26 @@ exports.SEL_ALL = [
   SEL_EXTERNAL, SEL_FORM, SEL_ELEMENT, SEL_STRUCTURAL, SEL_SEMI,
   SEL_COMBINING, SEL_MEDIA
 ].reduce(function (prev, curr) {
   return prev.concat(curr);
 }, []);
 
 /**
  * Find a URL for a given stylesheet
- * @param stylesheet {StyleSheet|StyleSheetActor}
+ * @param {StyleSheet} stylesheet raw stylesheet
  */
-const sheetToUrl = exports.sheetToUrl = function (stylesheet) {
+const sheetToUrl = function (stylesheet) {
   // For <link> elements
   if (stylesheet.href) {
     return stylesheet.href;
   }
 
   // For <style> elements
   if (stylesheet.ownerNode) {
     let document = stylesheet.ownerNode.ownerDocument;
     let sheets = [...document.querySelectorAll("style")];
     let index = sheets.indexOf(stylesheet.ownerNode);
     return getURL(document) + " → <style> index " + index;
   }
 
-  // When `stylesheet` is a StyleSheetActor, we don't have access to ownerNode
-  if (stylesheet.nodeHref) {
-    return stylesheet.nodeHref + " → <style> index " + stylesheet.styleSheetIndex;
-  }
-
   throw new Error("Unknown sheet source");
 };
--- a/devtools/shared/specs/csscoverage.js
+++ b/devtools/shared/specs/csscoverage.js
@@ -1,15 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 const {Arg, RetVal, generateActorSpec} = require("devtools/shared/protocol");
 
+require("devtools/shared/specs/stylesheets");
+
 const cssUsageSpec = generateActorSpec({
   typeName: "cssUsage",
 
   events: {
     "state-change": {
       type: "stateChange",
       stateChange: Arg(0, "json")
     }
@@ -21,16 +23,20 @@ const cssUsageSpec = generateActorSpec({
     },
     stop: {},
     toggle: {},
     oneshot: {},
     createEditorReport: {
       request: { url: Arg(0, "string") },
       response: { reports: RetVal("array:json") }
     },
+    createEditorReportForSheet: {
+      request: { url: Arg(0, "stylesheet") },
+      response: { reports: RetVal("array:json") }
+    },
     createPageReport: {
       response: RetVal("json")
     },
     _testOnlyVisitedPages: {
       response: { value: RetVal("array:string") }
     },
   },
 });