michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: michael@0: /* michael@0: * WARNING! michael@0: * michael@0: * Do not edit this file directly, it is built from the sources at michael@0: * https://github.com/mozilla/source-map/ michael@0: */ michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: michael@0: this.EXPORTED_SYMBOLS = [ "SourceMapConsumer", "SourceMapGenerator", "SourceNode" ]; michael@0: michael@0: Components.utils.import('resource://gre/modules/devtools/Require.jsm'); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('source-map/source-map-consumer', ['require', 'exports', 'module' , 'source-map/util', 'source-map/binary-search', 'source-map/array-set', 'source-map/base64-vlq'], function(require, exports, module) { michael@0: michael@0: var util = require('source-map/util'); michael@0: var binarySearch = require('source-map/binary-search'); michael@0: var ArraySet = require('source-map/array-set').ArraySet; michael@0: var base64VLQ = require('source-map/base64-vlq'); michael@0: michael@0: /** michael@0: * A SourceMapConsumer instance represents a parsed source map which we can michael@0: * query for information about the original file positions by giving it a file michael@0: * position in the generated source. michael@0: * michael@0: * The only parameter is the raw source map (either as a JSON string, or michael@0: * already parsed to an object). According to the spec, source maps have the michael@0: * following attributes: michael@0: * michael@0: * - version: Which version of the source map spec this map is following. michael@0: * - sources: An array of URLs to the original source files. michael@0: * - names: An array of identifiers which can be referrenced by individual mappings. michael@0: * - sourceRoot: Optional. The URL root from which all sources are relative. michael@0: * - sourcesContent: Optional. An array of contents of the original source files. michael@0: * - mappings: A string of base64 VLQs which contain the actual mappings. michael@0: * - file: The generated file this source map is associated with. michael@0: * michael@0: * Here is an example source map, taken from the source map spec[0]: michael@0: * michael@0: * { michael@0: * version : 3, michael@0: * file: "out.js", michael@0: * sourceRoot : "", michael@0: * sources: ["foo.js", "bar.js"], michael@0: * names: ["src", "maps", "are", "fun"], michael@0: * mappings: "AA,AB;;ABCDE;" michael@0: * } michael@0: * michael@0: * [0]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?pli=1# michael@0: */ michael@0: function SourceMapConsumer(aSourceMap) { michael@0: var sourceMap = aSourceMap; michael@0: if (typeof aSourceMap === 'string') { michael@0: sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, '')); michael@0: } michael@0: michael@0: var version = util.getArg(sourceMap, 'version'); michael@0: var sources = util.getArg(sourceMap, 'sources'); michael@0: // Sass 3.3 leaves out the 'names' array, so we deviate from the spec (which michael@0: // requires the array) to play nice here. michael@0: var names = util.getArg(sourceMap, 'names', []); michael@0: var sourceRoot = util.getArg(sourceMap, 'sourceRoot', null); michael@0: var sourcesContent = util.getArg(sourceMap, 'sourcesContent', null); michael@0: var mappings = util.getArg(sourceMap, 'mappings'); michael@0: var file = util.getArg(sourceMap, 'file', null); michael@0: michael@0: // Once again, Sass deviates from the spec and supplies the version as a michael@0: // string rather than a number, so we use loose equality checking here. michael@0: if (version != this._version) { michael@0: throw new Error('Unsupported version: ' + version); michael@0: } michael@0: michael@0: // Pass `true` below to allow duplicate names and sources. While source maps michael@0: // are intended to be compressed and deduplicated, the TypeScript compiler michael@0: // sometimes generates source maps with duplicates in them. See Github issue michael@0: // #72 and bugzil.la/889492. michael@0: this._names = ArraySet.fromArray(names, true); michael@0: this._sources = ArraySet.fromArray(sources, true); michael@0: michael@0: this.sourceRoot = sourceRoot; michael@0: this.sourcesContent = sourcesContent; michael@0: this._mappings = mappings; michael@0: this.file = file; michael@0: } michael@0: michael@0: /** michael@0: * Create a SourceMapConsumer from a SourceMapGenerator. michael@0: * michael@0: * @param SourceMapGenerator aSourceMap michael@0: * The source map that will be consumed. michael@0: * @returns SourceMapConsumer michael@0: */ michael@0: SourceMapConsumer.fromSourceMap = michael@0: function SourceMapConsumer_fromSourceMap(aSourceMap) { michael@0: var smc = Object.create(SourceMapConsumer.prototype); michael@0: michael@0: smc._names = ArraySet.fromArray(aSourceMap._names.toArray(), true); michael@0: smc._sources = ArraySet.fromArray(aSourceMap._sources.toArray(), true); michael@0: smc.sourceRoot = aSourceMap._sourceRoot; michael@0: smc.sourcesContent = aSourceMap._generateSourcesContent(smc._sources.toArray(), michael@0: smc.sourceRoot); michael@0: smc.file = aSourceMap._file; michael@0: michael@0: smc.__generatedMappings = aSourceMap._mappings.slice() michael@0: .sort(util.compareByGeneratedPositions); michael@0: smc.__originalMappings = aSourceMap._mappings.slice() michael@0: .sort(util.compareByOriginalPositions); michael@0: michael@0: return smc; michael@0: }; michael@0: michael@0: /** michael@0: * The version of the source mapping spec that we are consuming. michael@0: */ michael@0: SourceMapConsumer.prototype._version = 3; michael@0: michael@0: /** michael@0: * The list of original sources. michael@0: */ michael@0: Object.defineProperty(SourceMapConsumer.prototype, 'sources', { michael@0: get: function () { michael@0: return this._sources.toArray().map(function (s) { michael@0: return this.sourceRoot ? util.join(this.sourceRoot, s) : s; michael@0: }, this); michael@0: } michael@0: }); michael@0: michael@0: // `__generatedMappings` and `__originalMappings` are arrays that hold the michael@0: // parsed mapping coordinates from the source map's "mappings" attribute. They michael@0: // are lazily instantiated, accessed via the `_generatedMappings` and michael@0: // `_originalMappings` getters respectively, and we only parse the mappings michael@0: // and create these arrays once queried for a source location. We jump through michael@0: // these hoops because there can be many thousands of mappings, and parsing michael@0: // them is expensive, so we only want to do it if we must. michael@0: // michael@0: // Each object in the arrays is of the form: michael@0: // michael@0: // { michael@0: // generatedLine: The line number in the generated code, michael@0: // generatedColumn: The column number in the generated code, michael@0: // source: The path to the original source file that generated this michael@0: // chunk of code, michael@0: // originalLine: The line number in the original source that michael@0: // corresponds to this chunk of generated code, michael@0: // originalColumn: The column number in the original source that michael@0: // corresponds to this chunk of generated code, michael@0: // name: The name of the original symbol which generated this chunk of michael@0: // code. michael@0: // } michael@0: // michael@0: // All properties except for `generatedLine` and `generatedColumn` can be michael@0: // `null`. michael@0: // michael@0: // `_generatedMappings` is ordered by the generated positions. michael@0: // michael@0: // `_originalMappings` is ordered by the original positions. michael@0: michael@0: SourceMapConsumer.prototype.__generatedMappings = null; michael@0: Object.defineProperty(SourceMapConsumer.prototype, '_generatedMappings', { michael@0: get: function () { michael@0: if (!this.__generatedMappings) { michael@0: this.__generatedMappings = []; michael@0: this.__originalMappings = []; michael@0: this._parseMappings(this._mappings, this.sourceRoot); michael@0: } michael@0: michael@0: return this.__generatedMappings; michael@0: } michael@0: }); michael@0: michael@0: SourceMapConsumer.prototype.__originalMappings = null; michael@0: Object.defineProperty(SourceMapConsumer.prototype, '_originalMappings', { michael@0: get: function () { michael@0: if (!this.__originalMappings) { michael@0: this.__generatedMappings = []; michael@0: this.__originalMappings = []; michael@0: this._parseMappings(this._mappings, this.sourceRoot); michael@0: } michael@0: michael@0: return this.__originalMappings; michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Parse the mappings in a string in to a data structure which we can easily michael@0: * query (the ordered arrays in the `this.__generatedMappings` and michael@0: * `this.__originalMappings` properties). michael@0: */ michael@0: SourceMapConsumer.prototype._parseMappings = michael@0: function SourceMapConsumer_parseMappings(aStr, aSourceRoot) { michael@0: var generatedLine = 1; michael@0: var previousGeneratedColumn = 0; michael@0: var previousOriginalLine = 0; michael@0: var previousOriginalColumn = 0; michael@0: var previousSource = 0; michael@0: var previousName = 0; michael@0: var mappingSeparator = /^[,;]/; michael@0: var str = aStr; michael@0: var mapping; michael@0: var temp; michael@0: michael@0: while (str.length > 0) { michael@0: if (str.charAt(0) === ';') { michael@0: generatedLine++; michael@0: str = str.slice(1); michael@0: previousGeneratedColumn = 0; michael@0: } michael@0: else if (str.charAt(0) === ',') { michael@0: str = str.slice(1); michael@0: } michael@0: else { michael@0: mapping = {}; michael@0: mapping.generatedLine = generatedLine; michael@0: michael@0: // Generated column. michael@0: temp = base64VLQ.decode(str); michael@0: mapping.generatedColumn = previousGeneratedColumn + temp.value; michael@0: previousGeneratedColumn = mapping.generatedColumn; michael@0: str = temp.rest; michael@0: michael@0: if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) { michael@0: // Original source. michael@0: temp = base64VLQ.decode(str); michael@0: mapping.source = this._sources.at(previousSource + temp.value); michael@0: previousSource += temp.value; michael@0: str = temp.rest; michael@0: if (str.length === 0 || mappingSeparator.test(str.charAt(0))) { michael@0: throw new Error('Found a source, but no line and column'); michael@0: } michael@0: michael@0: // Original line. michael@0: temp = base64VLQ.decode(str); michael@0: mapping.originalLine = previousOriginalLine + temp.value; michael@0: previousOriginalLine = mapping.originalLine; michael@0: // Lines are stored 0-based michael@0: mapping.originalLine += 1; michael@0: str = temp.rest; michael@0: if (str.length === 0 || mappingSeparator.test(str.charAt(0))) { michael@0: throw new Error('Found a source and line, but no column'); michael@0: } michael@0: michael@0: // Original column. michael@0: temp = base64VLQ.decode(str); michael@0: mapping.originalColumn = previousOriginalColumn + temp.value; michael@0: previousOriginalColumn = mapping.originalColumn; michael@0: str = temp.rest; michael@0: michael@0: if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) { michael@0: // Original name. michael@0: temp = base64VLQ.decode(str); michael@0: mapping.name = this._names.at(previousName + temp.value); michael@0: previousName += temp.value; michael@0: str = temp.rest; michael@0: } michael@0: } michael@0: michael@0: this.__generatedMappings.push(mapping); michael@0: if (typeof mapping.originalLine === 'number') { michael@0: this.__originalMappings.push(mapping); michael@0: } michael@0: } michael@0: } michael@0: michael@0: this.__originalMappings.sort(util.compareByOriginalPositions); michael@0: }; michael@0: michael@0: /** michael@0: * Find the mapping that best matches the hypothetical "needle" mapping that michael@0: * we are searching for in the given "haystack" of mappings. michael@0: */ michael@0: SourceMapConsumer.prototype._findMapping = michael@0: function SourceMapConsumer_findMapping(aNeedle, aMappings, aLineName, michael@0: aColumnName, aComparator) { michael@0: // To return the position we are searching for, we must first find the michael@0: // mapping for the given position and then return the opposite position it michael@0: // points to. Because the mappings are sorted, we can use binary search to michael@0: // find the best mapping. michael@0: michael@0: if (aNeedle[aLineName] <= 0) { michael@0: throw new TypeError('Line must be greater than or equal to 1, got ' michael@0: + aNeedle[aLineName]); michael@0: } michael@0: if (aNeedle[aColumnName] < 0) { michael@0: throw new TypeError('Column must be greater than or equal to 0, got ' michael@0: + aNeedle[aColumnName]); michael@0: } michael@0: michael@0: return binarySearch.search(aNeedle, aMappings, aComparator); michael@0: }; michael@0: michael@0: /** michael@0: * Returns the original source, line, and column information for the generated michael@0: * source's line and column positions provided. The only argument is an object michael@0: * with the following properties: michael@0: * michael@0: * - line: The line number in the generated source. michael@0: * - column: The column number in the generated source. michael@0: * michael@0: * and an object is returned with the following properties: michael@0: * michael@0: * - source: The original source file, or null. michael@0: * - line: The line number in the original source, or null. michael@0: * - column: The column number in the original source, or null. michael@0: * - name: The original identifier, or null. michael@0: */ michael@0: SourceMapConsumer.prototype.originalPositionFor = michael@0: function SourceMapConsumer_originalPositionFor(aArgs) { michael@0: var needle = { michael@0: generatedLine: util.getArg(aArgs, 'line'), michael@0: generatedColumn: util.getArg(aArgs, 'column') michael@0: }; michael@0: michael@0: var mapping = this._findMapping(needle, michael@0: this._generatedMappings, michael@0: "generatedLine", michael@0: "generatedColumn", michael@0: util.compareByGeneratedPositions); michael@0: michael@0: if (mapping) { michael@0: var source = util.getArg(mapping, 'source', null); michael@0: if (source && this.sourceRoot) { michael@0: source = util.join(this.sourceRoot, source); michael@0: } michael@0: return { michael@0: source: source, michael@0: line: util.getArg(mapping, 'originalLine', null), michael@0: column: util.getArg(mapping, 'originalColumn', null), michael@0: name: util.getArg(mapping, 'name', null) michael@0: }; michael@0: } michael@0: michael@0: return { michael@0: source: null, michael@0: line: null, michael@0: column: null, michael@0: name: null michael@0: }; michael@0: }; michael@0: michael@0: /** michael@0: * Returns the original source content. The only argument is the url of the michael@0: * original source file. Returns null if no original source content is michael@0: * availible. michael@0: */ michael@0: SourceMapConsumer.prototype.sourceContentFor = michael@0: function SourceMapConsumer_sourceContentFor(aSource) { michael@0: if (!this.sourcesContent) { michael@0: return null; michael@0: } michael@0: michael@0: if (this.sourceRoot) { michael@0: aSource = util.relative(this.sourceRoot, aSource); michael@0: } michael@0: michael@0: if (this._sources.has(aSource)) { michael@0: return this.sourcesContent[this._sources.indexOf(aSource)]; michael@0: } michael@0: michael@0: var url; michael@0: if (this.sourceRoot michael@0: && (url = util.urlParse(this.sourceRoot))) { michael@0: // XXX: file:// URIs and absolute paths lead to unexpected behavior for michael@0: // many users. We can help them out when they expect file:// URIs to michael@0: // behave like it would if they were running a local HTTP server. See michael@0: // https://bugzilla.mozilla.org/show_bug.cgi?id=885597. michael@0: var fileUriAbsPath = aSource.replace(/^file:\/\//, ""); michael@0: if (url.scheme == "file" michael@0: && this._sources.has(fileUriAbsPath)) { michael@0: return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)] michael@0: } michael@0: michael@0: if ((!url.path || url.path == "/") michael@0: && this._sources.has("/" + aSource)) { michael@0: return this.sourcesContent[this._sources.indexOf("/" + aSource)]; michael@0: } michael@0: } michael@0: michael@0: throw new Error('"' + aSource + '" is not in the SourceMap.'); michael@0: }; michael@0: michael@0: /** michael@0: * Returns the generated line and column information for the original source, michael@0: * line, and column positions provided. The only argument is an object with michael@0: * the following properties: michael@0: * michael@0: * - source: The filename of the original source. michael@0: * - line: The line number in the original source. michael@0: * - column: The column number in the original source. michael@0: * michael@0: * and an object is returned with the following properties: michael@0: * michael@0: * - line: The line number in the generated source, or null. michael@0: * - column: The column number in the generated source, or null. michael@0: */ michael@0: SourceMapConsumer.prototype.generatedPositionFor = michael@0: function SourceMapConsumer_generatedPositionFor(aArgs) { michael@0: var needle = { michael@0: source: util.getArg(aArgs, 'source'), michael@0: originalLine: util.getArg(aArgs, 'line'), michael@0: originalColumn: util.getArg(aArgs, 'column') michael@0: }; michael@0: michael@0: if (this.sourceRoot) { michael@0: needle.source = util.relative(this.sourceRoot, needle.source); michael@0: } michael@0: michael@0: var mapping = this._findMapping(needle, michael@0: this._originalMappings, michael@0: "originalLine", michael@0: "originalColumn", michael@0: util.compareByOriginalPositions); michael@0: michael@0: if (mapping) { michael@0: return { michael@0: line: util.getArg(mapping, 'generatedLine', null), michael@0: column: util.getArg(mapping, 'generatedColumn', null) michael@0: }; michael@0: } michael@0: michael@0: return { michael@0: line: null, michael@0: column: null michael@0: }; michael@0: }; michael@0: michael@0: SourceMapConsumer.GENERATED_ORDER = 1; michael@0: SourceMapConsumer.ORIGINAL_ORDER = 2; michael@0: michael@0: /** michael@0: * Iterate over each mapping between an original source/line/column and a michael@0: * generated line/column in this source map. michael@0: * michael@0: * @param Function aCallback michael@0: * The function that is called with each mapping. michael@0: * @param Object aContext michael@0: * Optional. If specified, this object will be the value of `this` every michael@0: * time that `aCallback` is called. michael@0: * @param aOrder michael@0: * Either `SourceMapConsumer.GENERATED_ORDER` or michael@0: * `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to michael@0: * iterate over the mappings sorted by the generated file's line/column michael@0: * order or the original's source/line/column order, respectively. Defaults to michael@0: * `SourceMapConsumer.GENERATED_ORDER`. michael@0: */ michael@0: SourceMapConsumer.prototype.eachMapping = michael@0: function SourceMapConsumer_eachMapping(aCallback, aContext, aOrder) { michael@0: var context = aContext || null; michael@0: var order = aOrder || SourceMapConsumer.GENERATED_ORDER; michael@0: michael@0: var mappings; michael@0: switch (order) { michael@0: case SourceMapConsumer.GENERATED_ORDER: michael@0: mappings = this._generatedMappings; michael@0: break; michael@0: case SourceMapConsumer.ORIGINAL_ORDER: michael@0: mappings = this._originalMappings; michael@0: break; michael@0: default: michael@0: throw new Error("Unknown order of iteration."); michael@0: } michael@0: michael@0: var sourceRoot = this.sourceRoot; michael@0: mappings.map(function (mapping) { michael@0: var source = mapping.source; michael@0: if (source && sourceRoot) { michael@0: source = util.join(sourceRoot, source); michael@0: } michael@0: return { michael@0: source: source, michael@0: generatedLine: mapping.generatedLine, michael@0: generatedColumn: mapping.generatedColumn, michael@0: originalLine: mapping.originalLine, michael@0: originalColumn: mapping.originalColumn, michael@0: name: mapping.name michael@0: }; michael@0: }).forEach(aCallback, context); michael@0: }; michael@0: michael@0: exports.SourceMapConsumer = SourceMapConsumer; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('source-map/util', ['require', 'exports', 'module' , ], function(require, exports, module) { michael@0: michael@0: /** michael@0: * This is a helper function for getting values from parameter/options michael@0: * objects. michael@0: * michael@0: * @param args The object we are extracting values from michael@0: * @param name The name of the property we are getting. michael@0: * @param defaultValue An optional value to return if the property is missing michael@0: * from the object. If this is not specified and the property is missing, an michael@0: * error will be thrown. michael@0: */ michael@0: function getArg(aArgs, aName, aDefaultValue) { michael@0: if (aName in aArgs) { michael@0: return aArgs[aName]; michael@0: } else if (arguments.length === 3) { michael@0: return aDefaultValue; michael@0: } else { michael@0: throw new Error('"' + aName + '" is a required argument.'); michael@0: } michael@0: } michael@0: exports.getArg = getArg; michael@0: michael@0: var urlRegexp = /([\w+\-.]+):\/\/((\w+:\w+)@)?([\w.]+)?(:(\d+))?(\S+)?/; michael@0: var dataUrlRegexp = /^data:.+\,.+/; michael@0: michael@0: function urlParse(aUrl) { michael@0: var match = aUrl.match(urlRegexp); michael@0: if (!match) { michael@0: return null; michael@0: } michael@0: return { michael@0: scheme: match[1], michael@0: auth: match[3], michael@0: host: match[4], michael@0: port: match[6], michael@0: path: match[7] michael@0: }; michael@0: } michael@0: exports.urlParse = urlParse; michael@0: michael@0: function urlGenerate(aParsedUrl) { michael@0: var url = aParsedUrl.scheme + "://"; michael@0: if (aParsedUrl.auth) { michael@0: url += aParsedUrl.auth + "@" michael@0: } michael@0: if (aParsedUrl.host) { michael@0: url += aParsedUrl.host; michael@0: } michael@0: if (aParsedUrl.port) { michael@0: url += ":" + aParsedUrl.port michael@0: } michael@0: if (aParsedUrl.path) { michael@0: url += aParsedUrl.path; michael@0: } michael@0: return url; michael@0: } michael@0: exports.urlGenerate = urlGenerate; michael@0: michael@0: function join(aRoot, aPath) { michael@0: var url; michael@0: michael@0: if (aPath.match(urlRegexp) || aPath.match(dataUrlRegexp)) { michael@0: return aPath; michael@0: } michael@0: michael@0: if (aPath.charAt(0) === '/' && (url = urlParse(aRoot))) { michael@0: url.path = aPath; michael@0: return urlGenerate(url); michael@0: } michael@0: michael@0: return aRoot.replace(/\/$/, '') + '/' + aPath; michael@0: } michael@0: exports.join = join; michael@0: michael@0: /** michael@0: * Because behavior goes wacky when you set `__proto__` on objects, we michael@0: * have to prefix all the strings in our set with an arbitrary character. michael@0: * michael@0: * See https://github.com/mozilla/source-map/pull/31 and michael@0: * https://github.com/mozilla/source-map/issues/30 michael@0: * michael@0: * @param String aStr michael@0: */ michael@0: function toSetString(aStr) { michael@0: return '$' + aStr; michael@0: } michael@0: exports.toSetString = toSetString; michael@0: michael@0: function fromSetString(aStr) { michael@0: return aStr.substr(1); michael@0: } michael@0: exports.fromSetString = fromSetString; michael@0: michael@0: function relative(aRoot, aPath) { michael@0: aRoot = aRoot.replace(/\/$/, ''); michael@0: michael@0: var url = urlParse(aRoot); michael@0: if (aPath.charAt(0) == "/" && url && url.path == "/") { michael@0: return aPath.slice(1); michael@0: } michael@0: michael@0: return aPath.indexOf(aRoot + '/') === 0 michael@0: ? aPath.substr(aRoot.length + 1) michael@0: : aPath; michael@0: } michael@0: exports.relative = relative; michael@0: michael@0: function strcmp(aStr1, aStr2) { michael@0: var s1 = aStr1 || ""; michael@0: var s2 = aStr2 || ""; michael@0: return (s1 > s2) - (s1 < s2); michael@0: } michael@0: michael@0: /** michael@0: * Comparator between two mappings where the original positions are compared. michael@0: * michael@0: * Optionally pass in `true` as `onlyCompareGenerated` to consider two michael@0: * mappings with the same original source/line/column, but different generated michael@0: * line and column the same. Useful when searching for a mapping with a michael@0: * stubbed out mapping. michael@0: */ michael@0: function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) { michael@0: var cmp; michael@0: michael@0: cmp = strcmp(mappingA.source, mappingB.source); michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.originalLine - mappingB.originalLine; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.originalColumn - mappingB.originalColumn; michael@0: if (cmp || onlyCompareOriginal) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = strcmp(mappingA.name, mappingB.name); michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.generatedLine - mappingB.generatedLine; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: return mappingA.generatedColumn - mappingB.generatedColumn; michael@0: }; michael@0: exports.compareByOriginalPositions = compareByOriginalPositions; michael@0: michael@0: /** michael@0: * Comparator between two mappings where the generated positions are michael@0: * compared. michael@0: * michael@0: * Optionally pass in `true` as `onlyCompareGenerated` to consider two michael@0: * mappings with the same generated line and column, but different michael@0: * source/name/original line and column the same. Useful when searching for a michael@0: * mapping with a stubbed out mapping. michael@0: */ michael@0: function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) { michael@0: var cmp; michael@0: michael@0: cmp = mappingA.generatedLine - mappingB.generatedLine; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.generatedColumn - mappingB.generatedColumn; michael@0: if (cmp || onlyCompareGenerated) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = strcmp(mappingA.source, mappingB.source); michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.originalLine - mappingB.originalLine; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.originalColumn - mappingB.originalColumn; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: return strcmp(mappingA.name, mappingB.name); michael@0: }; michael@0: exports.compareByGeneratedPositions = compareByGeneratedPositions; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('source-map/binary-search', ['require', 'exports', 'module' , ], function(require, exports, module) { michael@0: michael@0: /** michael@0: * Recursive implementation of binary search. michael@0: * michael@0: * @param aLow Indices here and lower do not contain the needle. michael@0: * @param aHigh Indices here and higher do not contain the needle. michael@0: * @param aNeedle The element being searched for. michael@0: * @param aHaystack The non-empty array being searched. michael@0: * @param aCompare Function which takes two elements and returns -1, 0, or 1. michael@0: */ michael@0: function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare) { michael@0: // This function terminates when one of the following is true: michael@0: // michael@0: // 1. We find the exact element we are looking for. michael@0: // michael@0: // 2. We did not find the exact element, but we can return the next michael@0: // closest element that is less than that element. michael@0: // michael@0: // 3. We did not find the exact element, and there is no next-closest michael@0: // element which is less than the one we are searching for, so we michael@0: // return null. michael@0: var mid = Math.floor((aHigh - aLow) / 2) + aLow; michael@0: var cmp = aCompare(aNeedle, aHaystack[mid], true); michael@0: if (cmp === 0) { michael@0: // Found the element we are looking for. michael@0: return aHaystack[mid]; michael@0: } michael@0: else if (cmp > 0) { michael@0: // aHaystack[mid] is greater than our needle. michael@0: if (aHigh - mid > 1) { michael@0: // The element is in the upper half. michael@0: return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare); michael@0: } michael@0: // We did not find an exact match, return the next closest one michael@0: // (termination case 2). michael@0: return aHaystack[mid]; michael@0: } michael@0: else { michael@0: // aHaystack[mid] is less than our needle. michael@0: if (mid - aLow > 1) { michael@0: // The element is in the lower half. michael@0: return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare); michael@0: } michael@0: // The exact needle element was not found in this haystack. Determine if michael@0: // we are in termination case (2) or (3) and return the appropriate thing. michael@0: return aLow < 0 michael@0: ? null michael@0: : aHaystack[aLow]; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * This is an implementation of binary search which will always try and return michael@0: * the next lowest value checked if there is no exact hit. This is because michael@0: * mappings between original and generated line/col pairs are single points, michael@0: * and there is an implicit region between each of them, so a miss just means michael@0: * that you aren't on the very start of a region. michael@0: * michael@0: * @param aNeedle The element you are looking for. michael@0: * @param aHaystack The array that is being searched. michael@0: * @param aCompare A function which takes the needle and an element in the michael@0: * array and returns -1, 0, or 1 depending on whether the needle is less michael@0: * than, equal to, or greater than the element, respectively. michael@0: */ michael@0: exports.search = function search(aNeedle, aHaystack, aCompare) { michael@0: return aHaystack.length > 0 michael@0: ? recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare) michael@0: : null; michael@0: }; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('source-map/array-set', ['require', 'exports', 'module' , 'source-map/util'], function(require, exports, module) { michael@0: michael@0: var util = require('source-map/util'); michael@0: michael@0: /** michael@0: * A data structure which is a combination of an array and a set. Adding a new michael@0: * member is O(1), testing for membership is O(1), and finding the index of an michael@0: * element is O(1). Removing elements from the set is not supported. Only michael@0: * strings are supported for membership. michael@0: */ michael@0: function ArraySet() { michael@0: this._array = []; michael@0: this._set = {}; michael@0: } michael@0: michael@0: /** michael@0: * Static method for creating ArraySet instances from an existing array. michael@0: */ michael@0: ArraySet.fromArray = function ArraySet_fromArray(aArray, aAllowDuplicates) { michael@0: var set = new ArraySet(); michael@0: for (var i = 0, len = aArray.length; i < len; i++) { michael@0: set.add(aArray[i], aAllowDuplicates); michael@0: } michael@0: return set; michael@0: }; michael@0: michael@0: /** michael@0: * Add the given string to this set. michael@0: * michael@0: * @param String aStr michael@0: */ michael@0: ArraySet.prototype.add = function ArraySet_add(aStr, aAllowDuplicates) { michael@0: var isDuplicate = this.has(aStr); michael@0: var idx = this._array.length; michael@0: if (!isDuplicate || aAllowDuplicates) { michael@0: this._array.push(aStr); michael@0: } michael@0: if (!isDuplicate) { michael@0: this._set[util.toSetString(aStr)] = idx; michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Is the given string a member of this set? michael@0: * michael@0: * @param String aStr michael@0: */ michael@0: ArraySet.prototype.has = function ArraySet_has(aStr) { michael@0: return Object.prototype.hasOwnProperty.call(this._set, michael@0: util.toSetString(aStr)); michael@0: }; michael@0: michael@0: /** michael@0: * What is the index of the given string in the array? michael@0: * michael@0: * @param String aStr michael@0: */ michael@0: ArraySet.prototype.indexOf = function ArraySet_indexOf(aStr) { michael@0: if (this.has(aStr)) { michael@0: return this._set[util.toSetString(aStr)]; michael@0: } michael@0: throw new Error('"' + aStr + '" is not in the set.'); michael@0: }; michael@0: michael@0: /** michael@0: * What is the element at the given index? michael@0: * michael@0: * @param Number aIdx michael@0: */ michael@0: ArraySet.prototype.at = function ArraySet_at(aIdx) { michael@0: if (aIdx >= 0 && aIdx < this._array.length) { michael@0: return this._array[aIdx]; michael@0: } michael@0: throw new Error('No element indexed by ' + aIdx); michael@0: }; michael@0: michael@0: /** michael@0: * Returns the array representation of this set (which has the proper indices michael@0: * indicated by indexOf). Note that this is a copy of the internal array used michael@0: * for storing the members so that no one can mess with internal state. michael@0: */ michael@0: ArraySet.prototype.toArray = function ArraySet_toArray() { michael@0: return this._array.slice(); michael@0: }; michael@0: michael@0: exports.ArraySet = ArraySet; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: * michael@0: * Based on the Base 64 VLQ implementation in Closure Compiler: michael@0: * https://code.google.com/p/closure-compiler/source/browse/trunk/src/com/google/debugging/sourcemap/Base64VLQ.java michael@0: * michael@0: * Copyright 2011 The Closure Compiler Authors. All rights reserved. michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are michael@0: * met: michael@0: * michael@0: * * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * * Redistributions in binary form must reproduce the above michael@0: * copyright notice, this list of conditions and the following michael@0: * disclaimer in the documentation and/or other materials provided michael@0: * with the distribution. michael@0: * * Neither the name of Google Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: define('source-map/base64-vlq', ['require', 'exports', 'module' , 'source-map/base64'], function(require, exports, module) { michael@0: michael@0: var base64 = require('source-map/base64'); michael@0: michael@0: // A single base 64 digit can contain 6 bits of data. For the base 64 variable michael@0: // length quantities we use in the source map spec, the first bit is the sign, michael@0: // the next four bits are the actual value, and the 6th bit is the michael@0: // continuation bit. The continuation bit tells us whether there are more michael@0: // digits in this value following this digit. michael@0: // michael@0: // Continuation michael@0: // | Sign michael@0: // | | michael@0: // V V michael@0: // 101011 michael@0: michael@0: var VLQ_BASE_SHIFT = 5; michael@0: michael@0: // binary: 100000 michael@0: var VLQ_BASE = 1 << VLQ_BASE_SHIFT; michael@0: michael@0: // binary: 011111 michael@0: var VLQ_BASE_MASK = VLQ_BASE - 1; michael@0: michael@0: // binary: 100000 michael@0: var VLQ_CONTINUATION_BIT = VLQ_BASE; michael@0: michael@0: /** michael@0: * Converts from a two-complement value to a value where the sign bit is michael@0: * is placed in the least significant bit. For example, as decimals: michael@0: * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary) michael@0: * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) michael@0: */ michael@0: function toVLQSigned(aValue) { michael@0: return aValue < 0 michael@0: ? ((-aValue) << 1) + 1 michael@0: : (aValue << 1) + 0; michael@0: } michael@0: michael@0: /** michael@0: * Converts to a two-complement value from a value where the sign bit is michael@0: * is placed in the least significant bit. For example, as decimals: michael@0: * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1 michael@0: * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2 michael@0: */ michael@0: function fromVLQSigned(aValue) { michael@0: var isNegative = (aValue & 1) === 1; michael@0: var shifted = aValue >> 1; michael@0: return isNegative michael@0: ? -shifted michael@0: : shifted; michael@0: } michael@0: michael@0: /** michael@0: * Returns the base 64 VLQ encoded value. michael@0: */ michael@0: exports.encode = function base64VLQ_encode(aValue) { michael@0: var encoded = ""; michael@0: var digit; michael@0: michael@0: var vlq = toVLQSigned(aValue); michael@0: michael@0: do { michael@0: digit = vlq & VLQ_BASE_MASK; michael@0: vlq >>>= VLQ_BASE_SHIFT; michael@0: if (vlq > 0) { michael@0: // There are still more digits in this value, so we must make sure the michael@0: // continuation bit is marked. michael@0: digit |= VLQ_CONTINUATION_BIT; michael@0: } michael@0: encoded += base64.encode(digit); michael@0: } while (vlq > 0); michael@0: michael@0: return encoded; michael@0: }; michael@0: michael@0: /** michael@0: * Decodes the next base 64 VLQ value from the given string and returns the michael@0: * value and the rest of the string. michael@0: */ michael@0: exports.decode = function base64VLQ_decode(aStr) { michael@0: var i = 0; michael@0: var strLen = aStr.length; michael@0: var result = 0; michael@0: var shift = 0; michael@0: var continuation, digit; michael@0: michael@0: do { michael@0: if (i >= strLen) { michael@0: throw new Error("Expected more digits in base 64 VLQ value."); michael@0: } michael@0: digit = base64.decode(aStr.charAt(i++)); michael@0: continuation = !!(digit & VLQ_CONTINUATION_BIT); michael@0: digit &= VLQ_BASE_MASK; michael@0: result = result + (digit << shift); michael@0: shift += VLQ_BASE_SHIFT; michael@0: } while (continuation); michael@0: michael@0: return { michael@0: value: fromVLQSigned(result), michael@0: rest: aStr.slice(i) michael@0: }; michael@0: }; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('source-map/base64', ['require', 'exports', 'module' , ], function(require, exports, module) { michael@0: michael@0: var charToIntMap = {}; michael@0: var intToCharMap = {}; michael@0: michael@0: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' michael@0: .split('') michael@0: .forEach(function (ch, index) { michael@0: charToIntMap[ch] = index; michael@0: intToCharMap[index] = ch; michael@0: }); michael@0: michael@0: /** michael@0: * Encode an integer in the range of 0 to 63 to a single base 64 digit. michael@0: */ michael@0: exports.encode = function base64_encode(aNumber) { michael@0: if (aNumber in intToCharMap) { michael@0: return intToCharMap[aNumber]; michael@0: } michael@0: throw new TypeError("Must be between 0 and 63: " + aNumber); michael@0: }; michael@0: michael@0: /** michael@0: * Decode a single base 64 digit to an integer. michael@0: */ michael@0: exports.decode = function base64_decode(aChar) { michael@0: if (aChar in charToIntMap) { michael@0: return charToIntMap[aChar]; michael@0: } michael@0: throw new TypeError("Not a valid base 64 digit: " + aChar); michael@0: }; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('source-map/source-map-generator', ['require', 'exports', 'module' , 'source-map/base64-vlq', 'source-map/util', 'source-map/array-set'], function(require, exports, module) { michael@0: michael@0: var base64VLQ = require('source-map/base64-vlq'); michael@0: var util = require('source-map/util'); michael@0: var ArraySet = require('source-map/array-set').ArraySet; michael@0: michael@0: /** michael@0: * An instance of the SourceMapGenerator represents a source map which is michael@0: * being built incrementally. To create a new one, you must pass an object michael@0: * with the following properties: michael@0: * michael@0: * - file: The filename of the generated source. michael@0: * - sourceRoot: An optional root for all URLs in this source map. michael@0: */ michael@0: function SourceMapGenerator(aArgs) { michael@0: this._file = util.getArg(aArgs, 'file'); michael@0: this._sourceRoot = util.getArg(aArgs, 'sourceRoot', null); michael@0: this._sources = new ArraySet(); michael@0: this._names = new ArraySet(); michael@0: this._mappings = []; michael@0: this._sourcesContents = null; michael@0: } michael@0: michael@0: SourceMapGenerator.prototype._version = 3; michael@0: michael@0: /** michael@0: * Creates a new SourceMapGenerator based on a SourceMapConsumer michael@0: * michael@0: * @param aSourceMapConsumer The SourceMap. michael@0: */ michael@0: SourceMapGenerator.fromSourceMap = michael@0: function SourceMapGenerator_fromSourceMap(aSourceMapConsumer) { michael@0: var sourceRoot = aSourceMapConsumer.sourceRoot; michael@0: var generator = new SourceMapGenerator({ michael@0: file: aSourceMapConsumer.file, michael@0: sourceRoot: sourceRoot michael@0: }); michael@0: aSourceMapConsumer.eachMapping(function (mapping) { michael@0: var newMapping = { michael@0: generated: { michael@0: line: mapping.generatedLine, michael@0: column: mapping.generatedColumn michael@0: } michael@0: }; michael@0: michael@0: if (mapping.source) { michael@0: newMapping.source = mapping.source; michael@0: if (sourceRoot) { michael@0: newMapping.source = util.relative(sourceRoot, newMapping.source); michael@0: } michael@0: michael@0: newMapping.original = { michael@0: line: mapping.originalLine, michael@0: column: mapping.originalColumn michael@0: }; michael@0: michael@0: if (mapping.name) { michael@0: newMapping.name = mapping.name; michael@0: } michael@0: } michael@0: michael@0: generator.addMapping(newMapping); michael@0: }); michael@0: aSourceMapConsumer.sources.forEach(function (sourceFile) { michael@0: var content = aSourceMapConsumer.sourceContentFor(sourceFile); michael@0: if (content) { michael@0: generator.setSourceContent(sourceFile, content); michael@0: } michael@0: }); michael@0: return generator; michael@0: }; michael@0: michael@0: /** michael@0: * Add a single mapping from original source line and column to the generated michael@0: * source's line and column for this source map being created. The mapping michael@0: * object should have the following properties: michael@0: * michael@0: * - generated: An object with the generated line and column positions. michael@0: * - original: An object with the original line and column positions. michael@0: * - source: The original source file (relative to the sourceRoot). michael@0: * - name: An optional original token name for this mapping. michael@0: */ michael@0: SourceMapGenerator.prototype.addMapping = michael@0: function SourceMapGenerator_addMapping(aArgs) { michael@0: var generated = util.getArg(aArgs, 'generated'); michael@0: var original = util.getArg(aArgs, 'original', null); michael@0: var source = util.getArg(aArgs, 'source', null); michael@0: var name = util.getArg(aArgs, 'name', null); michael@0: michael@0: this._validateMapping(generated, original, source, name); michael@0: michael@0: if (source && !this._sources.has(source)) { michael@0: this._sources.add(source); michael@0: } michael@0: michael@0: if (name && !this._names.has(name)) { michael@0: this._names.add(name); michael@0: } michael@0: michael@0: this._mappings.push({ michael@0: generatedLine: generated.line, michael@0: generatedColumn: generated.column, michael@0: originalLine: original != null && original.line, michael@0: originalColumn: original != null && original.column, michael@0: source: source, michael@0: name: name michael@0: }); michael@0: }; michael@0: michael@0: /** michael@0: * Set the source content for a source file. michael@0: */ michael@0: SourceMapGenerator.prototype.setSourceContent = michael@0: function SourceMapGenerator_setSourceContent(aSourceFile, aSourceContent) { michael@0: var source = aSourceFile; michael@0: if (this._sourceRoot) { michael@0: source = util.relative(this._sourceRoot, source); michael@0: } michael@0: michael@0: if (aSourceContent !== null) { michael@0: // Add the source content to the _sourcesContents map. michael@0: // Create a new _sourcesContents map if the property is null. michael@0: if (!this._sourcesContents) { michael@0: this._sourcesContents = {}; michael@0: } michael@0: this._sourcesContents[util.toSetString(source)] = aSourceContent; michael@0: } else { michael@0: // Remove the source file from the _sourcesContents map. michael@0: // If the _sourcesContents map is empty, set the property to null. michael@0: delete this._sourcesContents[util.toSetString(source)]; michael@0: if (Object.keys(this._sourcesContents).length === 0) { michael@0: this._sourcesContents = null; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Applies the mappings of a sub-source-map for a specific source file to the michael@0: * source map being generated. Each mapping to the supplied source file is michael@0: * rewritten using the supplied source map. Note: The resolution for the michael@0: * resulting mappings is the minimium of this map and the supplied map. michael@0: * michael@0: * @param aSourceMapConsumer The source map to be applied. michael@0: * @param aSourceFile Optional. The filename of the source file. michael@0: * If omitted, SourceMapConsumer's file property will be used. michael@0: */ michael@0: SourceMapGenerator.prototype.applySourceMap = michael@0: function SourceMapGenerator_applySourceMap(aSourceMapConsumer, aSourceFile) { michael@0: // If aSourceFile is omitted, we will use the file property of the SourceMap michael@0: if (!aSourceFile) { michael@0: aSourceFile = aSourceMapConsumer.file; michael@0: } michael@0: var sourceRoot = this._sourceRoot; michael@0: // Make "aSourceFile" relative if an absolute Url is passed. michael@0: if (sourceRoot) { michael@0: aSourceFile = util.relative(sourceRoot, aSourceFile); michael@0: } michael@0: // Applying the SourceMap can add and remove items from the sources and michael@0: // the names array. michael@0: var newSources = new ArraySet(); michael@0: var newNames = new ArraySet(); michael@0: michael@0: // Find mappings for the "aSourceFile" michael@0: this._mappings.forEach(function (mapping) { michael@0: if (mapping.source === aSourceFile && mapping.originalLine) { michael@0: // Check if it can be mapped by the source map, then update the mapping. michael@0: var original = aSourceMapConsumer.originalPositionFor({ michael@0: line: mapping.originalLine, michael@0: column: mapping.originalColumn michael@0: }); michael@0: if (original.source !== null) { michael@0: // Copy mapping michael@0: if (sourceRoot) { michael@0: mapping.source = util.relative(sourceRoot, original.source); michael@0: } else { michael@0: mapping.source = original.source; michael@0: } michael@0: mapping.originalLine = original.line; michael@0: mapping.originalColumn = original.column; michael@0: if (original.name !== null && mapping.name !== null) { michael@0: // Only use the identifier name if it's an identifier michael@0: // in both SourceMaps michael@0: mapping.name = original.name; michael@0: } michael@0: } michael@0: } michael@0: michael@0: var source = mapping.source; michael@0: if (source && !newSources.has(source)) { michael@0: newSources.add(source); michael@0: } michael@0: michael@0: var name = mapping.name; michael@0: if (name && !newNames.has(name)) { michael@0: newNames.add(name); michael@0: } michael@0: michael@0: }, this); michael@0: this._sources = newSources; michael@0: this._names = newNames; michael@0: michael@0: // Copy sourcesContents of applied map. michael@0: aSourceMapConsumer.sources.forEach(function (sourceFile) { michael@0: var content = aSourceMapConsumer.sourceContentFor(sourceFile); michael@0: if (content) { michael@0: if (sourceRoot) { michael@0: sourceFile = util.relative(sourceRoot, sourceFile); michael@0: } michael@0: this.setSourceContent(sourceFile, content); michael@0: } michael@0: }, this); michael@0: }; michael@0: michael@0: /** michael@0: * A mapping can have one of the three levels of data: michael@0: * michael@0: * 1. Just the generated position. michael@0: * 2. The Generated position, original position, and original source. michael@0: * 3. Generated and original position, original source, as well as a name michael@0: * token. michael@0: * michael@0: * To maintain consistency, we validate that any new mapping being added falls michael@0: * in to one of these categories. michael@0: */ michael@0: SourceMapGenerator.prototype._validateMapping = michael@0: function SourceMapGenerator_validateMapping(aGenerated, aOriginal, aSource, michael@0: aName) { michael@0: if (aGenerated && 'line' in aGenerated && 'column' in aGenerated michael@0: && aGenerated.line > 0 && aGenerated.column >= 0 michael@0: && !aOriginal && !aSource && !aName) { michael@0: // Case 1. michael@0: return; michael@0: } michael@0: else if (aGenerated && 'line' in aGenerated && 'column' in aGenerated michael@0: && aOriginal && 'line' in aOriginal && 'column' in aOriginal michael@0: && aGenerated.line > 0 && aGenerated.column >= 0 michael@0: && aOriginal.line > 0 && aOriginal.column >= 0 michael@0: && aSource) { michael@0: // Cases 2 and 3. michael@0: return; michael@0: } michael@0: else { michael@0: throw new Error('Invalid mapping: ' + JSON.stringify({ michael@0: generated: aGenerated, michael@0: source: aSource, michael@0: original: aOriginal, michael@0: name: aName michael@0: })); michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Serialize the accumulated mappings in to the stream of base 64 VLQs michael@0: * specified by the source map format. michael@0: */ michael@0: SourceMapGenerator.prototype._serializeMappings = michael@0: function SourceMapGenerator_serializeMappings() { michael@0: var previousGeneratedColumn = 0; michael@0: var previousGeneratedLine = 1; michael@0: var previousOriginalColumn = 0; michael@0: var previousOriginalLine = 0; michael@0: var previousName = 0; michael@0: var previousSource = 0; michael@0: var result = ''; michael@0: var mapping; michael@0: michael@0: // The mappings must be guaranteed to be in sorted order before we start michael@0: // serializing them or else the generated line numbers (which are defined michael@0: // via the ';' separators) will be all messed up. Note: it might be more michael@0: // performant to maintain the sorting as we insert them, rather than as we michael@0: // serialize them, but the big O is the same either way. michael@0: this._mappings.sort(util.compareByGeneratedPositions); michael@0: michael@0: for (var i = 0, len = this._mappings.length; i < len; i++) { michael@0: mapping = this._mappings[i]; michael@0: michael@0: if (mapping.generatedLine !== previousGeneratedLine) { michael@0: previousGeneratedColumn = 0; michael@0: while (mapping.generatedLine !== previousGeneratedLine) { michael@0: result += ';'; michael@0: previousGeneratedLine++; michael@0: } michael@0: } michael@0: else { michael@0: if (i > 0) { michael@0: if (!util.compareByGeneratedPositions(mapping, this._mappings[i - 1])) { michael@0: continue; michael@0: } michael@0: result += ','; michael@0: } michael@0: } michael@0: michael@0: result += base64VLQ.encode(mapping.generatedColumn michael@0: - previousGeneratedColumn); michael@0: previousGeneratedColumn = mapping.generatedColumn; michael@0: michael@0: if (mapping.source) { michael@0: result += base64VLQ.encode(this._sources.indexOf(mapping.source) michael@0: - previousSource); michael@0: previousSource = this._sources.indexOf(mapping.source); michael@0: michael@0: // lines are stored 0-based in SourceMap spec version 3 michael@0: result += base64VLQ.encode(mapping.originalLine - 1 michael@0: - previousOriginalLine); michael@0: previousOriginalLine = mapping.originalLine - 1; michael@0: michael@0: result += base64VLQ.encode(mapping.originalColumn michael@0: - previousOriginalColumn); michael@0: previousOriginalColumn = mapping.originalColumn; michael@0: michael@0: if (mapping.name) { michael@0: result += base64VLQ.encode(this._names.indexOf(mapping.name) michael@0: - previousName); michael@0: previousName = this._names.indexOf(mapping.name); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: }; michael@0: michael@0: SourceMapGenerator.prototype._generateSourcesContent = michael@0: function SourceMapGenerator_generateSourcesContent(aSources, aSourceRoot) { michael@0: return aSources.map(function (source) { michael@0: if (!this._sourcesContents) { michael@0: return null; michael@0: } michael@0: if (aSourceRoot) { michael@0: source = util.relative(aSourceRoot, source); michael@0: } michael@0: var key = util.toSetString(source); michael@0: return Object.prototype.hasOwnProperty.call(this._sourcesContents, michael@0: key) michael@0: ? this._sourcesContents[key] michael@0: : null; michael@0: }, this); michael@0: }; michael@0: michael@0: /** michael@0: * Externalize the source map. michael@0: */ michael@0: SourceMapGenerator.prototype.toJSON = michael@0: function SourceMapGenerator_toJSON() { michael@0: var map = { michael@0: version: this._version, michael@0: file: this._file, michael@0: sources: this._sources.toArray(), michael@0: names: this._names.toArray(), michael@0: mappings: this._serializeMappings() michael@0: }; michael@0: if (this._sourceRoot) { michael@0: map.sourceRoot = this._sourceRoot; michael@0: } michael@0: if (this._sourcesContents) { michael@0: map.sourcesContent = this._generateSourcesContent(map.sources, map.sourceRoot); michael@0: } michael@0: michael@0: return map; michael@0: }; michael@0: michael@0: /** michael@0: * Render the source map being generated to a string. michael@0: */ michael@0: SourceMapGenerator.prototype.toString = michael@0: function SourceMapGenerator_toString() { michael@0: return JSON.stringify(this); michael@0: }; michael@0: michael@0: exports.SourceMapGenerator = SourceMapGenerator; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('source-map/source-node', ['require', 'exports', 'module' , 'source-map/source-map-generator', 'source-map/util'], function(require, exports, module) { michael@0: michael@0: var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; michael@0: var util = require('source-map/util'); michael@0: michael@0: /** michael@0: * SourceNodes provide a way to abstract over interpolating/concatenating michael@0: * snippets of generated JavaScript source code while maintaining the line and michael@0: * column information associated with the original source code. michael@0: * michael@0: * @param aLine The original line number. michael@0: * @param aColumn The original column number. michael@0: * @param aSource The original source's filename. michael@0: * @param aChunks Optional. An array of strings which are snippets of michael@0: * generated JS, or other SourceNodes. michael@0: * @param aName The original identifier. michael@0: */ michael@0: function SourceNode(aLine, aColumn, aSource, aChunks, aName) { michael@0: this.children = []; michael@0: this.sourceContents = {}; michael@0: this.line = aLine === undefined ? null : aLine; michael@0: this.column = aColumn === undefined ? null : aColumn; michael@0: this.source = aSource === undefined ? null : aSource; michael@0: this.name = aName === undefined ? null : aName; michael@0: if (aChunks != null) this.add(aChunks); michael@0: } michael@0: michael@0: /** michael@0: * Creates a SourceNode from generated code and a SourceMapConsumer. michael@0: * michael@0: * @param aGeneratedCode The generated code michael@0: * @param aSourceMapConsumer The SourceMap for the generated code michael@0: */ michael@0: SourceNode.fromStringWithSourceMap = michael@0: function SourceNode_fromStringWithSourceMap(aGeneratedCode, aSourceMapConsumer) { michael@0: // The SourceNode we want to fill with the generated code michael@0: // and the SourceMap michael@0: var node = new SourceNode(); michael@0: michael@0: // The generated code michael@0: // Processed fragments are removed from this array. michael@0: var remainingLines = aGeneratedCode.split('\n'); michael@0: michael@0: // We need to remember the position of "remainingLines" michael@0: var lastGeneratedLine = 1, lastGeneratedColumn = 0; michael@0: michael@0: // The generate SourceNodes we need a code range. michael@0: // To extract it current and last mapping is used. michael@0: // Here we store the last mapping. michael@0: var lastMapping = null; michael@0: michael@0: aSourceMapConsumer.eachMapping(function (mapping) { michael@0: if (lastMapping === null) { michael@0: // We add the generated code until the first mapping michael@0: // to the SourceNode without any mapping. michael@0: // Each line is added as separate string. michael@0: while (lastGeneratedLine < mapping.generatedLine) { michael@0: node.add(remainingLines.shift() + "\n"); michael@0: lastGeneratedLine++; michael@0: } michael@0: if (lastGeneratedColumn < mapping.generatedColumn) { michael@0: var nextLine = remainingLines[0]; michael@0: node.add(nextLine.substr(0, mapping.generatedColumn)); michael@0: remainingLines[0] = nextLine.substr(mapping.generatedColumn); michael@0: lastGeneratedColumn = mapping.generatedColumn; michael@0: } michael@0: } else { michael@0: // We add the code from "lastMapping" to "mapping": michael@0: // First check if there is a new line in between. michael@0: if (lastGeneratedLine < mapping.generatedLine) { michael@0: var code = ""; michael@0: // Associate full lines with "lastMapping" michael@0: do { michael@0: code += remainingLines.shift() + "\n"; michael@0: lastGeneratedLine++; michael@0: lastGeneratedColumn = 0; michael@0: } while (lastGeneratedLine < mapping.generatedLine); michael@0: // When we reached the correct line, we add code until we michael@0: // reach the correct column too. michael@0: if (lastGeneratedColumn < mapping.generatedColumn) { michael@0: var nextLine = remainingLines[0]; michael@0: code += nextLine.substr(0, mapping.generatedColumn); michael@0: remainingLines[0] = nextLine.substr(mapping.generatedColumn); michael@0: lastGeneratedColumn = mapping.generatedColumn; michael@0: } michael@0: // Create the SourceNode. michael@0: addMappingWithCode(lastMapping, code); michael@0: } else { michael@0: // There is no new line in between. michael@0: // Associate the code between "lastGeneratedColumn" and michael@0: // "mapping.generatedColumn" with "lastMapping" michael@0: var nextLine = remainingLines[0]; michael@0: var code = nextLine.substr(0, mapping.generatedColumn - michael@0: lastGeneratedColumn); michael@0: remainingLines[0] = nextLine.substr(mapping.generatedColumn - michael@0: lastGeneratedColumn); michael@0: lastGeneratedColumn = mapping.generatedColumn; michael@0: addMappingWithCode(lastMapping, code); michael@0: } michael@0: } michael@0: lastMapping = mapping; michael@0: }, this); michael@0: // We have processed all mappings. michael@0: // Associate the remaining code in the current line with "lastMapping" michael@0: // and add the remaining lines without any mapping michael@0: addMappingWithCode(lastMapping, remainingLines.join("\n")); michael@0: michael@0: // Copy sourcesContent into SourceNode michael@0: aSourceMapConsumer.sources.forEach(function (sourceFile) { michael@0: var content = aSourceMapConsumer.sourceContentFor(sourceFile); michael@0: if (content) { michael@0: node.setSourceContent(sourceFile, content); michael@0: } michael@0: }); michael@0: michael@0: return node; michael@0: michael@0: function addMappingWithCode(mapping, code) { michael@0: if (mapping === null || mapping.source === undefined) { michael@0: node.add(code); michael@0: } else { michael@0: node.add(new SourceNode(mapping.originalLine, michael@0: mapping.originalColumn, michael@0: mapping.source, michael@0: code, michael@0: mapping.name)); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Add a chunk of generated JS to this source node. michael@0: * michael@0: * @param aChunk A string snippet of generated JS code, another instance of michael@0: * SourceNode, or an array where each member is one of those things. michael@0: */ michael@0: SourceNode.prototype.add = function SourceNode_add(aChunk) { michael@0: if (Array.isArray(aChunk)) { michael@0: aChunk.forEach(function (chunk) { michael@0: this.add(chunk); michael@0: }, this); michael@0: } michael@0: else if (aChunk instanceof SourceNode || typeof aChunk === "string") { michael@0: if (aChunk) { michael@0: this.children.push(aChunk); michael@0: } michael@0: } michael@0: else { michael@0: throw new TypeError( michael@0: "Expected a SourceNode, string, or an array of SourceNodes and strings. Got " + aChunk michael@0: ); michael@0: } michael@0: return this; michael@0: }; michael@0: michael@0: /** michael@0: * Add a chunk of generated JS to the beginning of this source node. michael@0: * michael@0: * @param aChunk A string snippet of generated JS code, another instance of michael@0: * SourceNode, or an array where each member is one of those things. michael@0: */ michael@0: SourceNode.prototype.prepend = function SourceNode_prepend(aChunk) { michael@0: if (Array.isArray(aChunk)) { michael@0: for (var i = aChunk.length-1; i >= 0; i--) { michael@0: this.prepend(aChunk[i]); michael@0: } michael@0: } michael@0: else if (aChunk instanceof SourceNode || typeof aChunk === "string") { michael@0: this.children.unshift(aChunk); michael@0: } michael@0: else { michael@0: throw new TypeError( michael@0: "Expected a SourceNode, string, or an array of SourceNodes and strings. Got " + aChunk michael@0: ); michael@0: } michael@0: return this; michael@0: }; michael@0: michael@0: /** michael@0: * Walk over the tree of JS snippets in this node and its children. The michael@0: * walking function is called once for each snippet of JS and is passed that michael@0: * snippet and the its original associated source's line/column location. michael@0: * michael@0: * @param aFn The traversal function. michael@0: */ michael@0: SourceNode.prototype.walk = function SourceNode_walk(aFn) { michael@0: var chunk; michael@0: for (var i = 0, len = this.children.length; i < len; i++) { michael@0: chunk = this.children[i]; michael@0: if (chunk instanceof SourceNode) { michael@0: chunk.walk(aFn); michael@0: } michael@0: else { michael@0: if (chunk !== '') { michael@0: aFn(chunk, { source: this.source, michael@0: line: this.line, michael@0: column: this.column, michael@0: name: this.name }); michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Like `String.prototype.join` except for SourceNodes. Inserts `aStr` between michael@0: * each of `this.children`. michael@0: * michael@0: * @param aSep The separator. michael@0: */ michael@0: SourceNode.prototype.join = function SourceNode_join(aSep) { michael@0: var newChildren; michael@0: var i; michael@0: var len = this.children.length; michael@0: if (len > 0) { michael@0: newChildren = []; michael@0: for (i = 0; i < len-1; i++) { michael@0: newChildren.push(this.children[i]); michael@0: newChildren.push(aSep); michael@0: } michael@0: newChildren.push(this.children[i]); michael@0: this.children = newChildren; michael@0: } michael@0: return this; michael@0: }; michael@0: michael@0: /** michael@0: * Call String.prototype.replace on the very right-most source snippet. Useful michael@0: * for trimming whitespace from the end of a source node, etc. michael@0: * michael@0: * @param aPattern The pattern to replace. michael@0: * @param aReplacement The thing to replace the pattern with. michael@0: */ michael@0: SourceNode.prototype.replaceRight = function SourceNode_replaceRight(aPattern, aReplacement) { michael@0: var lastChild = this.children[this.children.length - 1]; michael@0: if (lastChild instanceof SourceNode) { michael@0: lastChild.replaceRight(aPattern, aReplacement); michael@0: } michael@0: else if (typeof lastChild === 'string') { michael@0: this.children[this.children.length - 1] = lastChild.replace(aPattern, aReplacement); michael@0: } michael@0: else { michael@0: this.children.push(''.replace(aPattern, aReplacement)); michael@0: } michael@0: return this; michael@0: }; michael@0: michael@0: /** michael@0: * Set the source content for a source file. This will be added to the SourceMapGenerator michael@0: * in the sourcesContent field. michael@0: * michael@0: * @param aSourceFile The filename of the source file michael@0: * @param aSourceContent The content of the source file michael@0: */ michael@0: SourceNode.prototype.setSourceContent = michael@0: function SourceNode_setSourceContent(aSourceFile, aSourceContent) { michael@0: this.sourceContents[util.toSetString(aSourceFile)] = aSourceContent; michael@0: }; michael@0: michael@0: /** michael@0: * Walk over the tree of SourceNodes. The walking function is called for each michael@0: * source file content and is passed the filename and source content. michael@0: * michael@0: * @param aFn The traversal function. michael@0: */ michael@0: SourceNode.prototype.walkSourceContents = michael@0: function SourceNode_walkSourceContents(aFn) { michael@0: for (var i = 0, len = this.children.length; i < len; i++) { michael@0: if (this.children[i] instanceof SourceNode) { michael@0: this.children[i].walkSourceContents(aFn); michael@0: } michael@0: } michael@0: michael@0: var sources = Object.keys(this.sourceContents); michael@0: for (var i = 0, len = sources.length; i < len; i++) { michael@0: aFn(util.fromSetString(sources[i]), this.sourceContents[sources[i]]); michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Return the string representation of this source node. Walks over the tree michael@0: * and concatenates all the various snippets together to one string. michael@0: */ michael@0: SourceNode.prototype.toString = function SourceNode_toString() { michael@0: var str = ""; michael@0: this.walk(function (chunk) { michael@0: str += chunk; michael@0: }); michael@0: return str; michael@0: }; michael@0: michael@0: /** michael@0: * Returns the string representation of this source node along with a source michael@0: * map. michael@0: */ michael@0: SourceNode.prototype.toStringWithSourceMap = function SourceNode_toStringWithSourceMap(aArgs) { michael@0: var generated = { michael@0: code: "", michael@0: line: 1, michael@0: column: 0 michael@0: }; michael@0: var map = new SourceMapGenerator(aArgs); michael@0: var sourceMappingActive = false; michael@0: var lastOriginalSource = null; michael@0: var lastOriginalLine = null; michael@0: var lastOriginalColumn = null; michael@0: var lastOriginalName = null; michael@0: this.walk(function (chunk, original) { michael@0: generated.code += chunk; michael@0: if (original.source !== null michael@0: && original.line !== null michael@0: && original.column !== null) { michael@0: if(lastOriginalSource !== original.source michael@0: || lastOriginalLine !== original.line michael@0: || lastOriginalColumn !== original.column michael@0: || lastOriginalName !== original.name) { michael@0: map.addMapping({ michael@0: source: original.source, michael@0: original: { michael@0: line: original.line, michael@0: column: original.column michael@0: }, michael@0: generated: { michael@0: line: generated.line, michael@0: column: generated.column michael@0: }, michael@0: name: original.name michael@0: }); michael@0: } michael@0: lastOriginalSource = original.source; michael@0: lastOriginalLine = original.line; michael@0: lastOriginalColumn = original.column; michael@0: lastOriginalName = original.name; michael@0: sourceMappingActive = true; michael@0: } else if (sourceMappingActive) { michael@0: map.addMapping({ michael@0: generated: { michael@0: line: generated.line, michael@0: column: generated.column michael@0: } michael@0: }); michael@0: lastOriginalSource = null; michael@0: sourceMappingActive = false; michael@0: } michael@0: chunk.split('').forEach(function (ch) { michael@0: if (ch === '\n') { michael@0: generated.line++; michael@0: generated.column = 0; michael@0: } else { michael@0: generated.column++; michael@0: } michael@0: }); michael@0: }); michael@0: this.walkSourceContents(function (sourceFile, sourceContent) { michael@0: map.setSourceContent(sourceFile, sourceContent); michael@0: }); michael@0: michael@0: return { code: generated.code, map: map }; michael@0: }; michael@0: michael@0: exports.SourceNode = SourceNode; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: this.SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer; michael@0: this.SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; michael@0: this.SourceNode = require('source-map/source-node').SourceNode;