Bug 1388855 - Simplify source map URL extraction in stylesheet actor; r?gl draft
authorTom Tromey <tom@tromey.com>
Wed, 09 Aug 2017 13:47:23 -0600
changeset 659074 c3ce6961c36e93d4b6fb4c9c7659fb5cab62b597
parent 659073 bcc9876096bc52316c2f86cbd8856167ab966016
child 729868 5313da09e5752670f29ea0c44d05f7bc67ab8f28
push id77998
push userbmo:ttromey@mozilla.com
push dateTue, 05 Sep 2017 13:05:43 +0000
reviewersgl
bugs1388855
milestone57.0a1
Bug 1388855 - Simplify source map URL extraction in stylesheet actor; r?gl MozReview-Commit-ID: 3WeNugNx7M
devtools/server/actors/stylesheets.js
--- a/devtools/server/actors/stylesheets.js
+++ b/devtools/server/actors/stylesheets.js
@@ -543,51 +543,49 @@ var StyleSheetActor = protocol.ActorClas
    * Fetch the source map for this stylesheet.
    *
    * @return {Promise}
    *         A promise that resolves with a SourceMapConsumer, or null.
    */
   _fetchSourceMap: function () {
     let deferred = defer();
 
-    this._getText().then(sheetContent => {
-      let url = this._extractSourceMapUrl(sheetContent);
-      if (!url) {
-        // no source map for this stylesheet
-        deferred.resolve(null);
-        return;
-      }
+    let url = this.rawSheet.sourceMapURL;
+    if (!url) {
+      // no source map for this stylesheet
+      deferred.resolve(null);
+      return deferred.promise;
+    }
 
-      url = normalize(url, this.safeHref);
-      let options = {
-        loadFromCache: false,
-        policy: Ci.nsIContentPolicy.TYPE_INTERNAL_STYLESHEET,
-        window: this.window
-      };
+    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
-        // this happens, SourceMapConsumer may fail with a JSON.parse error.
-        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.safeHref);
-        this._sourceMap = promise.resolve(consumer);
+    let map = fetch(url, options).then(({content}) => {
+      // Fetching the source map might have failed with a 404 or other. When
+      // this happens, SourceMapConsumer may fail with a JSON.parse error.
+      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.safeHref);
+      this._sourceMap = promise.resolve(consumer);
 
-        deferred.resolve(consumer);
-        return consumer;
-      }, deferred.reject);
+      deferred.resolve(consumer);
+      return consumer;
+    }, deferred.reject);
 
-      this._sourceMap = map;
-    }, deferred.reject);
+    this._sourceMap = map;
 
     return deferred.promise;
   },
 
   /**
    * Clear and unmanage the original source actors for this stylesheet.
    */
   _clearOriginalSources: function () {
@@ -609,36 +607,16 @@ var StyleSheetActor = protocol.ActorClas
         ? scriptURL
         : absSourceMapURL);
     sourceMap.sourceRoot = sourceMap.sourceRoot
       ? normalize(sourceMap.sourceRoot, base)
       : base;
   },
 
   /**
-   * Get the source map url specified in the text of a stylesheet.
-   *
-   * @param  {string} content
-   *         The text of the style sheet.
-   * @return {string}
-   *         Url of source map.
-   */
-  _extractSourceMapUrl: function (content) {
-    // If a SourceMap response header was saved on the style sheet, use it.
-    if (this.rawSheet.sourceMapURL) {
-      return this.rawSheet.sourceMapURL;
-    }
-    let matches = /sourceMappingURL\=([^\s\*]*)/.exec(content);
-    if (matches) {
-      return matches[1];
-    }
-    return null;
-  },
-
-  /**
    * Protocol method that gets the location in the original source of a
    * line, column pair in this stylesheet, if its source mapped, otherwise
    * a promise of the same location.
    */
   getOriginalLocation: function (line, column) {
     return this.getSourceMap().then((sourceMap) => {
       if (sourceMap) {
         return sourceMap.originalPositionFor({ line: line, column: column });