From 4d0651c98775955488bb730ea986de755ca16525 Mon Sep 17 00:00:00 2001 From: Patrick Brosset Date: Sat, 27 Feb 2016 20:39:21 +0100 Subject: [PATCH] Bug 1249888 - try/catch SourceMapConsumer to avoid empty rule-view when source map is invalid; r=gl --HG-- extra : rebase_source : 744840ba5bb668baf761de0ee1af33395a64aeb4 extra : histedit_source : 157aa5a266678390d2244c790fc44869c3a9f7b7%2C5ea5da38fe59c2612c8fcec8790350732837c9fb --- .../client/inspector/rules/test/browser.ini | 3 ++ .../test/browser_rules_invalid-source-map.js | 43 +++++++++++++++++++ .../rules/test/doc_invalid_sourcemap.css | 3 ++ .../rules/test/doc_invalid_sourcemap.html | 11 +++++ devtools/server/actors/stylesheets.js | 31 ++++++++----- 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 devtools/client/inspector/rules/test/browser_rules_invalid-source-map.js create mode 100644 devtools/client/inspector/rules/test/doc_invalid_sourcemap.css create mode 100644 devtools/client/inspector/rules/test/doc_invalid_sourcemap.html diff --git a/devtools/client/inspector/rules/test/browser.ini b/devtools/client/inspector/rules/test/browser.ini index 7e3f0579ba33..5f1424eb852a 100644 --- a/devtools/client/inspector/rules/test/browser.ini +++ b/devtools/client/inspector/rules/test/browser.ini @@ -14,6 +14,8 @@ support-files = doc_custom.html doc_filter.html doc_frame_script.js + doc_invalid_sourcemap.css + doc_invalid_sourcemap.html doc_keyframeanimation.css doc_keyframeanimation.html doc_keyframeLineNumbers.html @@ -119,6 +121,7 @@ skip-if = (os == "win" && debug) # bug 963492: win. [browser_rules_inherited-properties_01.js] [browser_rules_inherited-properties_02.js] [browser_rules_inherited-properties_03.js] +[browser_rules_invalid-source-map.js] [browser_rules_keybindings.js] [browser_rules_keyframes-rule_01.js] [browser_rules_keyframes-rule_02.js] diff --git a/devtools/client/inspector/rules/test/browser_rules_invalid-source-map.js b/devtools/client/inspector/rules/test/browser_rules_invalid-source-map.js new file mode 100644 index 000000000000..1a796556ccde --- /dev/null +++ b/devtools/client/inspector/rules/test/browser_rules_invalid-source-map.js @@ -0,0 +1,43 @@ +/* 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 is missing/invalid, the rule view still loads +// correctly. + +const TESTCASE_URI = URL_ROOT + "doc_invalid_sourcemap.html"; +const PREF = "devtools.styleeditor.source-maps-enabled"; +const CSS_LOC = "doc_invalid_sourcemap.css:1"; + +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"); + + let prop = getRuleViewProperty(view, "div", "color"); + ok(prop, "The 'color' property exists in this rule"); + + let value = getRuleViewPropertyValue(view, "div", "color"); + is(value, "gold", "The 'color' property has the right value"); + + yield verifyLinkText(view, CSS_LOC); + + Services.prefs.clearUserPref(PREF); +}); + +function verifyLinkText(view, text) { + info("Verifying that the rule-view stylesheet link is " + text); + let label = getRuleViewLinkByIndex(view, 1).querySelector("label"); + return waitForSuccess( + () => label.getAttribute("value") == text, + "Link text changed to display correct location: " + text + ); +} diff --git a/devtools/client/inspector/rules/test/doc_invalid_sourcemap.css b/devtools/client/inspector/rules/test/doc_invalid_sourcemap.css new file mode 100644 index 000000000000..ff96a6b542dc --- /dev/null +++ b/devtools/client/inspector/rules/test/doc_invalid_sourcemap.css @@ -0,0 +1,3 @@ +div { color: gold; } + +/*# sourceMappingURL=this-source-map-does-not-exist.css.map */ \ No newline at end of file diff --git a/devtools/client/inspector/rules/test/doc_invalid_sourcemap.html b/devtools/client/inspector/rules/test/doc_invalid_sourcemap.html new file mode 100644 index 000000000000..2e6422becd82 --- /dev/null +++ b/devtools/client/inspector/rules/test/doc_invalid_sourcemap.html @@ -0,0 +1,11 @@ + + + + + Invalid source map + + + +
invalid source map
+ + diff --git a/devtools/server/actors/stylesheets.js b/devtools/server/actors/stylesheets.js index 0d3383bd7de1..49154dc0a3bd 100644 --- a/devtools/server/actors/stylesheets.js +++ b/devtools/server/actors/stylesheets.js @@ -734,13 +734,13 @@ var StyleSheetActor = protocol.ActorClass({ _fetchSourceMap: function() { let deferred = promise.defer(); - this._getText().then((content) => { - let url = this._extractSourceMapUrl(content); + 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); let options = { @@ -748,15 +748,24 @@ var StyleSheetActor = protocol.ActorClass({ policy: Ci.nsIContentPolicy.TYPE_INTERNAL_STYLESHEET, window: this.window }; - let map = fetch(url, options) - .then(({content}) => { - let map = new SourceMapConsumer(content); - this._setSourceMapRoot(map, url, this.href); - this._sourceMap = promise.resolve(map); - deferred.resolve(map); - return map; - }, deferred.reject); + 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.href); + this._sourceMap = promise.resolve(consumer); + + deferred.resolve(consumer); + return consumer; + }, deferred.reject); this._sourceMap = map; }, deferred.reject);