diff --git a/lib/source-map/source-map-consumer.js b/lib/source-map/source-map-consumer.js index e7b3538e..1855df42 100644 --- a/lib/source-map/source-map-consumer.js +++ b/lib/source-map/source-map-consumer.js @@ -298,9 +298,9 @@ define(function (require, exports, module) { }; /** - * Returns the original source content. The only argument is - * the url of the original source file. Returns null if no - * original source content is availible. + * Returns the original source content. The only argument is the url of the + * original source file. Returns null if no original source content is + * availible. */ SourceMapConsumer.prototype.sourceContentFor = function SourceMapConsumer_sourceContentFor(aSource) { @@ -318,10 +318,21 @@ define(function (require, exports, module) { var url; if (this.sourceRoot - && (url = util.urlParse(this.sourceRoot)) - && (!url.path || url.path == "/") - && this._sources.has("/" + aSource)) { - return this.sourcesContent[this._sources.indexOf("/" + aSource)]; + && (url = util.urlParse(this.sourceRoot))) { + // XXX: file:// URIs and absolute paths lead to unexpected behavior for + // many users. We can help them out when they expect file:// URIs to + // behave like it would if they were running a local HTTP server. See + // https://bugzilla.mozilla.org/show_bug.cgi?id=885597. + var fileUriAbsPath = aSource.replace(/^file:\/\//, ""); + if (url.scheme == "file" + && this._sources.has(fileUriAbsPath)) { + return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)] + } + + if ((!url.path || url.path == "/") + && this._sources.has("/" + aSource)) { + return this.sourcesContent[this._sources.indexOf("/" + aSource)]; + } } throw new Error('"' + aSource + '" is not in the SourceMap.'); diff --git a/test/source-map/test-source-map-consumer.js b/test/source-map/test-source-map-consumer.js index 2f9a23d0..0d566e8c 100644 --- a/test/source-map/test-source-map-consumer.js +++ b/test/source-map/test-source-map-consumer.js @@ -303,4 +303,19 @@ define(function (require, exports, module) { assert.equal(map.sourceContentFor("/a"), "foo"); }; + exports['test bug 885597'] = function (assert, util) { + var map = new SourceMapConsumer({ + "version": 3, + "file": "foo.js", + "sourceRoot": "file:///Users/AlGore/Invented/The/Internet/", + "sources": ["/a"], + "names": [], + "mappings": "AACA", + "sourcesContent": ["foo"] + }); + + var s = map.sources[0]; + assert.equal(map.sourceContentFor(s), "foo"); + }; + });