Bug 1255787 - Do not assume sourceMap appears only in external stylesheets; r=gl
authorPatrick Brosset <pbrosset@mozilla.com>
Tue, 29 Mar 2016 21:30:11 +0200
changeset 290802 dbb64aae2a239d1ae85a6591a57058ad29ab90e7
parent 290801 5291c136bb0bac166a1dfe96fe2c931e79cf94ce
child 290803 aafa9cc405de7f17f05763ecbfe3278729a701ed
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgl
bugs1255787
milestone48.0a1
Bug 1255787 - Do not assume sourceMap appears only in external stylesheets; r=gl MozReview-Commit-ID: A92DdoAFDGT
devtools/client/inspector/rules/test/browser.ini
devtools/client/inspector/rules/test/browser_rules_inline-source-map.js
devtools/client/inspector/rules/test/doc_inline_sourcemap.html
devtools/server/actors/stylesheets.js
--- a/devtools/client/inspector/rules/test/browser.ini
+++ b/devtools/client/inspector/rules/test/browser.ini
@@ -9,16 +9,17 @@ support-files =
   doc_content_stylesheet_linked.css
   doc_content_stylesheet_script.css
   doc_copystyles.css
   doc_copystyles.html
   doc_cssom.html
   doc_custom.html
   doc_filter.html
   doc_frame_script.js
+  doc_inline_sourcemap.html
   doc_invalid_sourcemap.css
   doc_invalid_sourcemap.html
   doc_keyframeanimation.css
   doc_keyframeanimation.html
   doc_keyframeLineNumbers.html
   doc_media_queries.html
   doc_pseudoelement.html
   doc_ruleLineNumbers.html
@@ -117,16 +118,17 @@ skip-if = os == "mac" # Bug 1245996 : cl
 [browser_rules_filtereditor-appears-on-swatch-click.js]
 [browser_rules_filtereditor-commit-on-ENTER.js]
 [browser_rules_filtereditor-revert-on-ESC.js]
 skip-if = (os == "win" && debug) # bug 963492: win.
 [browser_rules_guessIndentation.js]
 [browser_rules_inherited-properties_01.js]
 [browser_rules_inherited-properties_02.js]
 [browser_rules_inherited-properties_03.js]
+[browser_rules_inline-source-map.js]
 [browser_rules_invalid-source-map.js]
 [browser_rules_keybindings.js]
 [browser_rules_keyframes-rule_01.js]
 [browser_rules_keyframes-rule_02.js]
 [browser_rules_keyframeLineNumbers.js]
 [browser_rules_lineNumbers.js]
 [browser_rules_livepreview.js]
 [browser_rules_mark_overridden_01.js]
new file mode 100644
--- /dev/null
+++ b/devtools/client/inspector/rules/test/browser_rules_inline-source-map.js
@@ -0,0 +1,26 @@
+/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+// Test that when a source map comment appears in an inline stylesheet, the
+// rule-view still appears correctly.
+// Bug 1255787.
+
+const TESTCASE_URI = URL_ROOT + "doc_inline_sourcemap.html";
+const PREF = "devtools.styleeditor.source-maps-enabled";
+
+add_task(function*() {
+  Services.prefs.setBoolPref(PREF, true);
+
+  yield addTab(TESTCASE_URI);
+  let {inspector, view} = yield openRuleView();
+
+  yield selectNode("div", inspector);
+
+  let ruleEl = getRuleViewRule(view, "div");
+  ok(ruleEl, "The 'div' rule exists in the rule-view");
+
+  Services.prefs.clearUserPref(PREF);
+});
new file mode 100644
--- /dev/null
+++ b/devtools/client/inspector/rules/test/doc_inline_sourcemap.html
@@ -0,0 +1,18 @@
+<!doctype html>
+<html>
+<head>
+  <title>CSS source maps in inline stylesheets</title>
+</head>
+<body>
+  <div>CSS source maps in inline stylesheets</div>
+  <style>
+div {
+  color: #ff0066; }
+
+span {
+  background-color: #EEE; }
+
+/*# sourceMappingURL=doc_sourcemaps.css.map */
+  </style>
+</body>
+</html>
--- a/devtools/server/actors/stylesheets.js
+++ b/devtools/server/actors/stylesheets.js
@@ -444,16 +444,32 @@ var StyleSheetActor = protocol.ActorClas
   /**
    * URL of underlying stylesheet.
    */
   get href() {
     return this.rawSheet.href;
   },
 
   /**
+   * Returns the stylesheet href or the document href if the sheet is inline.
+   */
+  get safeHref() {
+    let href = this.href;
+    if (!href) {
+      if (this.ownerNode instanceof Ci.nsIDOMHTMLDocument) {
+        href = this.ownerNode.location.href;
+      } else if (this.ownerNode.ownerDocument &&
+                 this.ownerNode.ownerDocument.location) {
+        href = this.ownerNode.ownerDocument.location.href;
+      }
+    }
+    return href;
+  },
+
+  /**
    * Retrieve the index (order) of stylesheet in the document.
    *
    * @return number
    */
   get styleSheetIndex()
   {
     if (this._styleSheetIndex == -1) {
       for (let i = 0; i < this.document.styleSheets.length; i++) {
@@ -737,17 +753,17 @@ var StyleSheetActor = protocol.ActorClas
     this._getText().then(sheetContent => {
       let url = this._extractSourceMapUrl(sheetContent);
       if (!url) {
         // no source map for this stylesheet
         deferred.resolve(null);
         return;
       }
 
-      url = normalize(url, this.href);
+      url = normalize(url, this.safeHref);
       let options = {
         loadFromCache: false,
         policy: Ci.nsIContentPolicy.TYPE_INTERNAL_STYLESHEET,
         window: this.window
       };
 
       let map = fetch(url, options).then(({content}) => {
         // Fetching the source map might have failed with a 404 or other. When
@@ -755,17 +771,17 @@ var StyleSheetActor = protocol.ActorClas
         let consumer;
         try {
           consumer = new SourceMapConsumer(content);
         } catch (e) {
           deferred.reject(new Error(
             `Source map at ${url} not found or invalid`));
           return null;
         }
-        this._setSourceMapRoot(consumer, url, this.href);
+        this._setSourceMapRoot(consumer, url, this.safeHref);
         this._sourceMap = promise.resolve(consumer);
 
         deferred.resolve(consumer);
         return consumer;
       }, deferred.reject);
 
       this._sourceMap = map;
     }, deferred.reject);