toolkit/devtools/sourcemap/SourceMap.jsm

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 2 /*
michael@0 3 * Copyright 2011 Mozilla Foundation and contributors
michael@0 4 * Licensed under the New BSD license. See LICENSE or:
michael@0 5 * http://opensource.org/licenses/BSD-3-Clause
michael@0 6 */
michael@0 7
michael@0 8 /*
michael@0 9 * WARNING!
michael@0 10 *
michael@0 11 * Do not edit this file directly, it is built from the sources at
michael@0 12 * https://github.com/mozilla/source-map/
michael@0 13 */
michael@0 14
michael@0 15 ///////////////////////////////////////////////////////////////////////////////
michael@0 16
michael@0 17
michael@0 18 this.EXPORTED_SYMBOLS = [ "SourceMapConsumer", "SourceMapGenerator", "SourceNode" ];
michael@0 19
michael@0 20 Components.utils.import('resource://gre/modules/devtools/Require.jsm');
michael@0 21 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 22 /*
michael@0 23 * Copyright 2011 Mozilla Foundation and contributors
michael@0 24 * Licensed under the New BSD license. See LICENSE or:
michael@0 25 * http://opensource.org/licenses/BSD-3-Clause
michael@0 26 */
michael@0 27 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 28
michael@0 29 var util = require('source-map/util');
michael@0 30 var binarySearch = require('source-map/binary-search');
michael@0 31 var ArraySet = require('source-map/array-set').ArraySet;
michael@0 32 var base64VLQ = require('source-map/base64-vlq');
michael@0 33
michael@0 34 /**
michael@0 35 * A SourceMapConsumer instance represents a parsed source map which we can
michael@0 36 * query for information about the original file positions by giving it a file
michael@0 37 * position in the generated source.
michael@0 38 *
michael@0 39 * The only parameter is the raw source map (either as a JSON string, or
michael@0 40 * already parsed to an object). According to the spec, source maps have the
michael@0 41 * following attributes:
michael@0 42 *
michael@0 43 * - version: Which version of the source map spec this map is following.
michael@0 44 * - sources: An array of URLs to the original source files.
michael@0 45 * - names: An array of identifiers which can be referrenced by individual mappings.
michael@0 46 * - sourceRoot: Optional. The URL root from which all sources are relative.
michael@0 47 * - sourcesContent: Optional. An array of contents of the original source files.
michael@0 48 * - mappings: A string of base64 VLQs which contain the actual mappings.
michael@0 49 * - file: The generated file this source map is associated with.
michael@0 50 *
michael@0 51 * Here is an example source map, taken from the source map spec[0]:
michael@0 52 *
michael@0 53 * {
michael@0 54 * version : 3,
michael@0 55 * file: "out.js",
michael@0 56 * sourceRoot : "",
michael@0 57 * sources: ["foo.js", "bar.js"],
michael@0 58 * names: ["src", "maps", "are", "fun"],
michael@0 59 * mappings: "AA,AB;;ABCDE;"
michael@0 60 * }
michael@0 61 *
michael@0 62 * [0]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?pli=1#
michael@0 63 */
michael@0 64 function SourceMapConsumer(aSourceMap) {
michael@0 65 var sourceMap = aSourceMap;
michael@0 66 if (typeof aSourceMap === 'string') {
michael@0 67 sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, ''));
michael@0 68 }
michael@0 69
michael@0 70 var version = util.getArg(sourceMap, 'version');
michael@0 71 var sources = util.getArg(sourceMap, 'sources');
michael@0 72 // Sass 3.3 leaves out the 'names' array, so we deviate from the spec (which
michael@0 73 // requires the array) to play nice here.
michael@0 74 var names = util.getArg(sourceMap, 'names', []);
michael@0 75 var sourceRoot = util.getArg(sourceMap, 'sourceRoot', null);
michael@0 76 var sourcesContent = util.getArg(sourceMap, 'sourcesContent', null);
michael@0 77 var mappings = util.getArg(sourceMap, 'mappings');
michael@0 78 var file = util.getArg(sourceMap, 'file', null);
michael@0 79
michael@0 80 // Once again, Sass deviates from the spec and supplies the version as a
michael@0 81 // string rather than a number, so we use loose equality checking here.
michael@0 82 if (version != this._version) {
michael@0 83 throw new Error('Unsupported version: ' + version);
michael@0 84 }
michael@0 85
michael@0 86 // Pass `true` below to allow duplicate names and sources. While source maps
michael@0 87 // are intended to be compressed and deduplicated, the TypeScript compiler
michael@0 88 // sometimes generates source maps with duplicates in them. See Github issue
michael@0 89 // #72 and bugzil.la/889492.
michael@0 90 this._names = ArraySet.fromArray(names, true);
michael@0 91 this._sources = ArraySet.fromArray(sources, true);
michael@0 92
michael@0 93 this.sourceRoot = sourceRoot;
michael@0 94 this.sourcesContent = sourcesContent;
michael@0 95 this._mappings = mappings;
michael@0 96 this.file = file;
michael@0 97 }
michael@0 98
michael@0 99 /**
michael@0 100 * Create a SourceMapConsumer from a SourceMapGenerator.
michael@0 101 *
michael@0 102 * @param SourceMapGenerator aSourceMap
michael@0 103 * The source map that will be consumed.
michael@0 104 * @returns SourceMapConsumer
michael@0 105 */
michael@0 106 SourceMapConsumer.fromSourceMap =
michael@0 107 function SourceMapConsumer_fromSourceMap(aSourceMap) {
michael@0 108 var smc = Object.create(SourceMapConsumer.prototype);
michael@0 109
michael@0 110 smc._names = ArraySet.fromArray(aSourceMap._names.toArray(), true);
michael@0 111 smc._sources = ArraySet.fromArray(aSourceMap._sources.toArray(), true);
michael@0 112 smc.sourceRoot = aSourceMap._sourceRoot;
michael@0 113 smc.sourcesContent = aSourceMap._generateSourcesContent(smc._sources.toArray(),
michael@0 114 smc.sourceRoot);
michael@0 115 smc.file = aSourceMap._file;
michael@0 116
michael@0 117 smc.__generatedMappings = aSourceMap._mappings.slice()
michael@0 118 .sort(util.compareByGeneratedPositions);
michael@0 119 smc.__originalMappings = aSourceMap._mappings.slice()
michael@0 120 .sort(util.compareByOriginalPositions);
michael@0 121
michael@0 122 return smc;
michael@0 123 };
michael@0 124
michael@0 125 /**
michael@0 126 * The version of the source mapping spec that we are consuming.
michael@0 127 */
michael@0 128 SourceMapConsumer.prototype._version = 3;
michael@0 129
michael@0 130 /**
michael@0 131 * The list of original sources.
michael@0 132 */
michael@0 133 Object.defineProperty(SourceMapConsumer.prototype, 'sources', {
michael@0 134 get: function () {
michael@0 135 return this._sources.toArray().map(function (s) {
michael@0 136 return this.sourceRoot ? util.join(this.sourceRoot, s) : s;
michael@0 137 }, this);
michael@0 138 }
michael@0 139 });
michael@0 140
michael@0 141 // `__generatedMappings` and `__originalMappings` are arrays that hold the
michael@0 142 // parsed mapping coordinates from the source map's "mappings" attribute. They
michael@0 143 // are lazily instantiated, accessed via the `_generatedMappings` and
michael@0 144 // `_originalMappings` getters respectively, and we only parse the mappings
michael@0 145 // and create these arrays once queried for a source location. We jump through
michael@0 146 // these hoops because there can be many thousands of mappings, and parsing
michael@0 147 // them is expensive, so we only want to do it if we must.
michael@0 148 //
michael@0 149 // Each object in the arrays is of the form:
michael@0 150 //
michael@0 151 // {
michael@0 152 // generatedLine: The line number in the generated code,
michael@0 153 // generatedColumn: The column number in the generated code,
michael@0 154 // source: The path to the original source file that generated this
michael@0 155 // chunk of code,
michael@0 156 // originalLine: The line number in the original source that
michael@0 157 // corresponds to this chunk of generated code,
michael@0 158 // originalColumn: The column number in the original source that
michael@0 159 // corresponds to this chunk of generated code,
michael@0 160 // name: The name of the original symbol which generated this chunk of
michael@0 161 // code.
michael@0 162 // }
michael@0 163 //
michael@0 164 // All properties except for `generatedLine` and `generatedColumn` can be
michael@0 165 // `null`.
michael@0 166 //
michael@0 167 // `_generatedMappings` is ordered by the generated positions.
michael@0 168 //
michael@0 169 // `_originalMappings` is ordered by the original positions.
michael@0 170
michael@0 171 SourceMapConsumer.prototype.__generatedMappings = null;
michael@0 172 Object.defineProperty(SourceMapConsumer.prototype, '_generatedMappings', {
michael@0 173 get: function () {
michael@0 174 if (!this.__generatedMappings) {
michael@0 175 this.__generatedMappings = [];
michael@0 176 this.__originalMappings = [];
michael@0 177 this._parseMappings(this._mappings, this.sourceRoot);
michael@0 178 }
michael@0 179
michael@0 180 return this.__generatedMappings;
michael@0 181 }
michael@0 182 });
michael@0 183
michael@0 184 SourceMapConsumer.prototype.__originalMappings = null;
michael@0 185 Object.defineProperty(SourceMapConsumer.prototype, '_originalMappings', {
michael@0 186 get: function () {
michael@0 187 if (!this.__originalMappings) {
michael@0 188 this.__generatedMappings = [];
michael@0 189 this.__originalMappings = [];
michael@0 190 this._parseMappings(this._mappings, this.sourceRoot);
michael@0 191 }
michael@0 192
michael@0 193 return this.__originalMappings;
michael@0 194 }
michael@0 195 });
michael@0 196
michael@0 197 /**
michael@0 198 * Parse the mappings in a string in to a data structure which we can easily
michael@0 199 * query (the ordered arrays in the `this.__generatedMappings` and
michael@0 200 * `this.__originalMappings` properties).
michael@0 201 */
michael@0 202 SourceMapConsumer.prototype._parseMappings =
michael@0 203 function SourceMapConsumer_parseMappings(aStr, aSourceRoot) {
michael@0 204 var generatedLine = 1;
michael@0 205 var previousGeneratedColumn = 0;
michael@0 206 var previousOriginalLine = 0;
michael@0 207 var previousOriginalColumn = 0;
michael@0 208 var previousSource = 0;
michael@0 209 var previousName = 0;
michael@0 210 var mappingSeparator = /^[,;]/;
michael@0 211 var str = aStr;
michael@0 212 var mapping;
michael@0 213 var temp;
michael@0 214
michael@0 215 while (str.length > 0) {
michael@0 216 if (str.charAt(0) === ';') {
michael@0 217 generatedLine++;
michael@0 218 str = str.slice(1);
michael@0 219 previousGeneratedColumn = 0;
michael@0 220 }
michael@0 221 else if (str.charAt(0) === ',') {
michael@0 222 str = str.slice(1);
michael@0 223 }
michael@0 224 else {
michael@0 225 mapping = {};
michael@0 226 mapping.generatedLine = generatedLine;
michael@0 227
michael@0 228 // Generated column.
michael@0 229 temp = base64VLQ.decode(str);
michael@0 230 mapping.generatedColumn = previousGeneratedColumn + temp.value;
michael@0 231 previousGeneratedColumn = mapping.generatedColumn;
michael@0 232 str = temp.rest;
michael@0 233
michael@0 234 if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) {
michael@0 235 // Original source.
michael@0 236 temp = base64VLQ.decode(str);
michael@0 237 mapping.source = this._sources.at(previousSource + temp.value);
michael@0 238 previousSource += temp.value;
michael@0 239 str = temp.rest;
michael@0 240 if (str.length === 0 || mappingSeparator.test(str.charAt(0))) {
michael@0 241 throw new Error('Found a source, but no line and column');
michael@0 242 }
michael@0 243
michael@0 244 // Original line.
michael@0 245 temp = base64VLQ.decode(str);
michael@0 246 mapping.originalLine = previousOriginalLine + temp.value;
michael@0 247 previousOriginalLine = mapping.originalLine;
michael@0 248 // Lines are stored 0-based
michael@0 249 mapping.originalLine += 1;
michael@0 250 str = temp.rest;
michael@0 251 if (str.length === 0 || mappingSeparator.test(str.charAt(0))) {
michael@0 252 throw new Error('Found a source and line, but no column');
michael@0 253 }
michael@0 254
michael@0 255 // Original column.
michael@0 256 temp = base64VLQ.decode(str);
michael@0 257 mapping.originalColumn = previousOriginalColumn + temp.value;
michael@0 258 previousOriginalColumn = mapping.originalColumn;
michael@0 259 str = temp.rest;
michael@0 260
michael@0 261 if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) {
michael@0 262 // Original name.
michael@0 263 temp = base64VLQ.decode(str);
michael@0 264 mapping.name = this._names.at(previousName + temp.value);
michael@0 265 previousName += temp.value;
michael@0 266 str = temp.rest;
michael@0 267 }
michael@0 268 }
michael@0 269
michael@0 270 this.__generatedMappings.push(mapping);
michael@0 271 if (typeof mapping.originalLine === 'number') {
michael@0 272 this.__originalMappings.push(mapping);
michael@0 273 }
michael@0 274 }
michael@0 275 }
michael@0 276
michael@0 277 this.__originalMappings.sort(util.compareByOriginalPositions);
michael@0 278 };
michael@0 279
michael@0 280 /**
michael@0 281 * Find the mapping that best matches the hypothetical "needle" mapping that
michael@0 282 * we are searching for in the given "haystack" of mappings.
michael@0 283 */
michael@0 284 SourceMapConsumer.prototype._findMapping =
michael@0 285 function SourceMapConsumer_findMapping(aNeedle, aMappings, aLineName,
michael@0 286 aColumnName, aComparator) {
michael@0 287 // To return the position we are searching for, we must first find the
michael@0 288 // mapping for the given position and then return the opposite position it
michael@0 289 // points to. Because the mappings are sorted, we can use binary search to
michael@0 290 // find the best mapping.
michael@0 291
michael@0 292 if (aNeedle[aLineName] <= 0) {
michael@0 293 throw new TypeError('Line must be greater than or equal to 1, got '
michael@0 294 + aNeedle[aLineName]);
michael@0 295 }
michael@0 296 if (aNeedle[aColumnName] < 0) {
michael@0 297 throw new TypeError('Column must be greater than or equal to 0, got '
michael@0 298 + aNeedle[aColumnName]);
michael@0 299 }
michael@0 300
michael@0 301 return binarySearch.search(aNeedle, aMappings, aComparator);
michael@0 302 };
michael@0 303
michael@0 304 /**
michael@0 305 * Returns the original source, line, and column information for the generated
michael@0 306 * source's line and column positions provided. The only argument is an object
michael@0 307 * with the following properties:
michael@0 308 *
michael@0 309 * - line: The line number in the generated source.
michael@0 310 * - column: The column number in the generated source.
michael@0 311 *
michael@0 312 * and an object is returned with the following properties:
michael@0 313 *
michael@0 314 * - source: The original source file, or null.
michael@0 315 * - line: The line number in the original source, or null.
michael@0 316 * - column: The column number in the original source, or null.
michael@0 317 * - name: The original identifier, or null.
michael@0 318 */
michael@0 319 SourceMapConsumer.prototype.originalPositionFor =
michael@0 320 function SourceMapConsumer_originalPositionFor(aArgs) {
michael@0 321 var needle = {
michael@0 322 generatedLine: util.getArg(aArgs, 'line'),
michael@0 323 generatedColumn: util.getArg(aArgs, 'column')
michael@0 324 };
michael@0 325
michael@0 326 var mapping = this._findMapping(needle,
michael@0 327 this._generatedMappings,
michael@0 328 "generatedLine",
michael@0 329 "generatedColumn",
michael@0 330 util.compareByGeneratedPositions);
michael@0 331
michael@0 332 if (mapping) {
michael@0 333 var source = util.getArg(mapping, 'source', null);
michael@0 334 if (source && this.sourceRoot) {
michael@0 335 source = util.join(this.sourceRoot, source);
michael@0 336 }
michael@0 337 return {
michael@0 338 source: source,
michael@0 339 line: util.getArg(mapping, 'originalLine', null),
michael@0 340 column: util.getArg(mapping, 'originalColumn', null),
michael@0 341 name: util.getArg(mapping, 'name', null)
michael@0 342 };
michael@0 343 }
michael@0 344
michael@0 345 return {
michael@0 346 source: null,
michael@0 347 line: null,
michael@0 348 column: null,
michael@0 349 name: null
michael@0 350 };
michael@0 351 };
michael@0 352
michael@0 353 /**
michael@0 354 * Returns the original source content. The only argument is the url of the
michael@0 355 * original source file. Returns null if no original source content is
michael@0 356 * availible.
michael@0 357 */
michael@0 358 SourceMapConsumer.prototype.sourceContentFor =
michael@0 359 function SourceMapConsumer_sourceContentFor(aSource) {
michael@0 360 if (!this.sourcesContent) {
michael@0 361 return null;
michael@0 362 }
michael@0 363
michael@0 364 if (this.sourceRoot) {
michael@0 365 aSource = util.relative(this.sourceRoot, aSource);
michael@0 366 }
michael@0 367
michael@0 368 if (this._sources.has(aSource)) {
michael@0 369 return this.sourcesContent[this._sources.indexOf(aSource)];
michael@0 370 }
michael@0 371
michael@0 372 var url;
michael@0 373 if (this.sourceRoot
michael@0 374 && (url = util.urlParse(this.sourceRoot))) {
michael@0 375 // XXX: file:// URIs and absolute paths lead to unexpected behavior for
michael@0 376 // many users. We can help them out when they expect file:// URIs to
michael@0 377 // behave like it would if they were running a local HTTP server. See
michael@0 378 // https://bugzilla.mozilla.org/show_bug.cgi?id=885597.
michael@0 379 var fileUriAbsPath = aSource.replace(/^file:\/\//, "");
michael@0 380 if (url.scheme == "file"
michael@0 381 && this._sources.has(fileUriAbsPath)) {
michael@0 382 return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)]
michael@0 383 }
michael@0 384
michael@0 385 if ((!url.path || url.path == "/")
michael@0 386 && this._sources.has("/" + aSource)) {
michael@0 387 return this.sourcesContent[this._sources.indexOf("/" + aSource)];
michael@0 388 }
michael@0 389 }
michael@0 390
michael@0 391 throw new Error('"' + aSource + '" is not in the SourceMap.');
michael@0 392 };
michael@0 393
michael@0 394 /**
michael@0 395 * Returns the generated line and column information for the original source,
michael@0 396 * line, and column positions provided. The only argument is an object with
michael@0 397 * the following properties:
michael@0 398 *
michael@0 399 * - source: The filename of the original source.
michael@0 400 * - line: The line number in the original source.
michael@0 401 * - column: The column number in the original source.
michael@0 402 *
michael@0 403 * and an object is returned with the following properties:
michael@0 404 *
michael@0 405 * - line: The line number in the generated source, or null.
michael@0 406 * - column: The column number in the generated source, or null.
michael@0 407 */
michael@0 408 SourceMapConsumer.prototype.generatedPositionFor =
michael@0 409 function SourceMapConsumer_generatedPositionFor(aArgs) {
michael@0 410 var needle = {
michael@0 411 source: util.getArg(aArgs, 'source'),
michael@0 412 originalLine: util.getArg(aArgs, 'line'),
michael@0 413 originalColumn: util.getArg(aArgs, 'column')
michael@0 414 };
michael@0 415
michael@0 416 if (this.sourceRoot) {
michael@0 417 needle.source = util.relative(this.sourceRoot, needle.source);
michael@0 418 }
michael@0 419
michael@0 420 var mapping = this._findMapping(needle,
michael@0 421 this._originalMappings,
michael@0 422 "originalLine",
michael@0 423 "originalColumn",
michael@0 424 util.compareByOriginalPositions);
michael@0 425
michael@0 426 if (mapping) {
michael@0 427 return {
michael@0 428 line: util.getArg(mapping, 'generatedLine', null),
michael@0 429 column: util.getArg(mapping, 'generatedColumn', null)
michael@0 430 };
michael@0 431 }
michael@0 432
michael@0 433 return {
michael@0 434 line: null,
michael@0 435 column: null
michael@0 436 };
michael@0 437 };
michael@0 438
michael@0 439 SourceMapConsumer.GENERATED_ORDER = 1;
michael@0 440 SourceMapConsumer.ORIGINAL_ORDER = 2;
michael@0 441
michael@0 442 /**
michael@0 443 * Iterate over each mapping between an original source/line/column and a
michael@0 444 * generated line/column in this source map.
michael@0 445 *
michael@0 446 * @param Function aCallback
michael@0 447 * The function that is called with each mapping.
michael@0 448 * @param Object aContext
michael@0 449 * Optional. If specified, this object will be the value of `this` every
michael@0 450 * time that `aCallback` is called.
michael@0 451 * @param aOrder
michael@0 452 * Either `SourceMapConsumer.GENERATED_ORDER` or
michael@0 453 * `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to
michael@0 454 * iterate over the mappings sorted by the generated file's line/column
michael@0 455 * order or the original's source/line/column order, respectively. Defaults to
michael@0 456 * `SourceMapConsumer.GENERATED_ORDER`.
michael@0 457 */
michael@0 458 SourceMapConsumer.prototype.eachMapping =
michael@0 459 function SourceMapConsumer_eachMapping(aCallback, aContext, aOrder) {
michael@0 460 var context = aContext || null;
michael@0 461 var order = aOrder || SourceMapConsumer.GENERATED_ORDER;
michael@0 462
michael@0 463 var mappings;
michael@0 464 switch (order) {
michael@0 465 case SourceMapConsumer.GENERATED_ORDER:
michael@0 466 mappings = this._generatedMappings;
michael@0 467 break;
michael@0 468 case SourceMapConsumer.ORIGINAL_ORDER:
michael@0 469 mappings = this._originalMappings;
michael@0 470 break;
michael@0 471 default:
michael@0 472 throw new Error("Unknown order of iteration.");
michael@0 473 }
michael@0 474
michael@0 475 var sourceRoot = this.sourceRoot;
michael@0 476 mappings.map(function (mapping) {
michael@0 477 var source = mapping.source;
michael@0 478 if (source && sourceRoot) {
michael@0 479 source = util.join(sourceRoot, source);
michael@0 480 }
michael@0 481 return {
michael@0 482 source: source,
michael@0 483 generatedLine: mapping.generatedLine,
michael@0 484 generatedColumn: mapping.generatedColumn,
michael@0 485 originalLine: mapping.originalLine,
michael@0 486 originalColumn: mapping.originalColumn,
michael@0 487 name: mapping.name
michael@0 488 };
michael@0 489 }).forEach(aCallback, context);
michael@0 490 };
michael@0 491
michael@0 492 exports.SourceMapConsumer = SourceMapConsumer;
michael@0 493
michael@0 494 });
michael@0 495 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 496 /*
michael@0 497 * Copyright 2011 Mozilla Foundation and contributors
michael@0 498 * Licensed under the New BSD license. See LICENSE or:
michael@0 499 * http://opensource.org/licenses/BSD-3-Clause
michael@0 500 */
michael@0 501 define('source-map/util', ['require', 'exports', 'module' , ], function(require, exports, module) {
michael@0 502
michael@0 503 /**
michael@0 504 * This is a helper function for getting values from parameter/options
michael@0 505 * objects.
michael@0 506 *
michael@0 507 * @param args The object we are extracting values from
michael@0 508 * @param name The name of the property we are getting.
michael@0 509 * @param defaultValue An optional value to return if the property is missing
michael@0 510 * from the object. If this is not specified and the property is missing, an
michael@0 511 * error will be thrown.
michael@0 512 */
michael@0 513 function getArg(aArgs, aName, aDefaultValue) {
michael@0 514 if (aName in aArgs) {
michael@0 515 return aArgs[aName];
michael@0 516 } else if (arguments.length === 3) {
michael@0 517 return aDefaultValue;
michael@0 518 } else {
michael@0 519 throw new Error('"' + aName + '" is a required argument.');
michael@0 520 }
michael@0 521 }
michael@0 522 exports.getArg = getArg;
michael@0 523
michael@0 524 var urlRegexp = /([\w+\-.]+):\/\/((\w+:\w+)@)?([\w.]+)?(:(\d+))?(\S+)?/;
michael@0 525 var dataUrlRegexp = /^data:.+\,.+/;
michael@0 526
michael@0 527 function urlParse(aUrl) {
michael@0 528 var match = aUrl.match(urlRegexp);
michael@0 529 if (!match) {
michael@0 530 return null;
michael@0 531 }
michael@0 532 return {
michael@0 533 scheme: match[1],
michael@0 534 auth: match[3],
michael@0 535 host: match[4],
michael@0 536 port: match[6],
michael@0 537 path: match[7]
michael@0 538 };
michael@0 539 }
michael@0 540 exports.urlParse = urlParse;
michael@0 541
michael@0 542 function urlGenerate(aParsedUrl) {
michael@0 543 var url = aParsedUrl.scheme + "://";
michael@0 544 if (aParsedUrl.auth) {
michael@0 545 url += aParsedUrl.auth + "@"
michael@0 546 }
michael@0 547 if (aParsedUrl.host) {
michael@0 548 url += aParsedUrl.host;
michael@0 549 }
michael@0 550 if (aParsedUrl.port) {
michael@0 551 url += ":" + aParsedUrl.port
michael@0 552 }
michael@0 553 if (aParsedUrl.path) {
michael@0 554 url += aParsedUrl.path;
michael@0 555 }
michael@0 556 return url;
michael@0 557 }
michael@0 558 exports.urlGenerate = urlGenerate;
michael@0 559
michael@0 560 function join(aRoot, aPath) {
michael@0 561 var url;
michael@0 562
michael@0 563 if (aPath.match(urlRegexp) || aPath.match(dataUrlRegexp)) {
michael@0 564 return aPath;
michael@0 565 }
michael@0 566
michael@0 567 if (aPath.charAt(0) === '/' && (url = urlParse(aRoot))) {
michael@0 568 url.path = aPath;
michael@0 569 return urlGenerate(url);
michael@0 570 }
michael@0 571
michael@0 572 return aRoot.replace(/\/$/, '') + '/' + aPath;
michael@0 573 }
michael@0 574 exports.join = join;
michael@0 575
michael@0 576 /**
michael@0 577 * Because behavior goes wacky when you set `__proto__` on objects, we
michael@0 578 * have to prefix all the strings in our set with an arbitrary character.
michael@0 579 *
michael@0 580 * See https://github.com/mozilla/source-map/pull/31 and
michael@0 581 * https://github.com/mozilla/source-map/issues/30
michael@0 582 *
michael@0 583 * @param String aStr
michael@0 584 */
michael@0 585 function toSetString(aStr) {
michael@0 586 return '$' + aStr;
michael@0 587 }
michael@0 588 exports.toSetString = toSetString;
michael@0 589
michael@0 590 function fromSetString(aStr) {
michael@0 591 return aStr.substr(1);
michael@0 592 }
michael@0 593 exports.fromSetString = fromSetString;
michael@0 594
michael@0 595 function relative(aRoot, aPath) {
michael@0 596 aRoot = aRoot.replace(/\/$/, '');
michael@0 597
michael@0 598 var url = urlParse(aRoot);
michael@0 599 if (aPath.charAt(0) == "/" && url && url.path == "/") {
michael@0 600 return aPath.slice(1);
michael@0 601 }
michael@0 602
michael@0 603 return aPath.indexOf(aRoot + '/') === 0
michael@0 604 ? aPath.substr(aRoot.length + 1)
michael@0 605 : aPath;
michael@0 606 }
michael@0 607 exports.relative = relative;
michael@0 608
michael@0 609 function strcmp(aStr1, aStr2) {
michael@0 610 var s1 = aStr1 || "";
michael@0 611 var s2 = aStr2 || "";
michael@0 612 return (s1 > s2) - (s1 < s2);
michael@0 613 }
michael@0 614
michael@0 615 /**
michael@0 616 * Comparator between two mappings where the original positions are compared.
michael@0 617 *
michael@0 618 * Optionally pass in `true` as `onlyCompareGenerated` to consider two
michael@0 619 * mappings with the same original source/line/column, but different generated
michael@0 620 * line and column the same. Useful when searching for a mapping with a
michael@0 621 * stubbed out mapping.
michael@0 622 */
michael@0 623 function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {
michael@0 624 var cmp;
michael@0 625
michael@0 626 cmp = strcmp(mappingA.source, mappingB.source);
michael@0 627 if (cmp) {
michael@0 628 return cmp;
michael@0 629 }
michael@0 630
michael@0 631 cmp = mappingA.originalLine - mappingB.originalLine;
michael@0 632 if (cmp) {
michael@0 633 return cmp;
michael@0 634 }
michael@0 635
michael@0 636 cmp = mappingA.originalColumn - mappingB.originalColumn;
michael@0 637 if (cmp || onlyCompareOriginal) {
michael@0 638 return cmp;
michael@0 639 }
michael@0 640
michael@0 641 cmp = strcmp(mappingA.name, mappingB.name);
michael@0 642 if (cmp) {
michael@0 643 return cmp;
michael@0 644 }
michael@0 645
michael@0 646 cmp = mappingA.generatedLine - mappingB.generatedLine;
michael@0 647 if (cmp) {
michael@0 648 return cmp;
michael@0 649 }
michael@0 650
michael@0 651 return mappingA.generatedColumn - mappingB.generatedColumn;
michael@0 652 };
michael@0 653 exports.compareByOriginalPositions = compareByOriginalPositions;
michael@0 654
michael@0 655 /**
michael@0 656 * Comparator between two mappings where the generated positions are
michael@0 657 * compared.
michael@0 658 *
michael@0 659 * Optionally pass in `true` as `onlyCompareGenerated` to consider two
michael@0 660 * mappings with the same generated line and column, but different
michael@0 661 * source/name/original line and column the same. Useful when searching for a
michael@0 662 * mapping with a stubbed out mapping.
michael@0 663 */
michael@0 664 function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) {
michael@0 665 var cmp;
michael@0 666
michael@0 667 cmp = mappingA.generatedLine - mappingB.generatedLine;
michael@0 668 if (cmp) {
michael@0 669 return cmp;
michael@0 670 }
michael@0 671
michael@0 672 cmp = mappingA.generatedColumn - mappingB.generatedColumn;
michael@0 673 if (cmp || onlyCompareGenerated) {
michael@0 674 return cmp;
michael@0 675 }
michael@0 676
michael@0 677 cmp = strcmp(mappingA.source, mappingB.source);
michael@0 678 if (cmp) {
michael@0 679 return cmp;
michael@0 680 }
michael@0 681
michael@0 682 cmp = mappingA.originalLine - mappingB.originalLine;
michael@0 683 if (cmp) {
michael@0 684 return cmp;
michael@0 685 }
michael@0 686
michael@0 687 cmp = mappingA.originalColumn - mappingB.originalColumn;
michael@0 688 if (cmp) {
michael@0 689 return cmp;
michael@0 690 }
michael@0 691
michael@0 692 return strcmp(mappingA.name, mappingB.name);
michael@0 693 };
michael@0 694 exports.compareByGeneratedPositions = compareByGeneratedPositions;
michael@0 695
michael@0 696 });
michael@0 697 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 698 /*
michael@0 699 * Copyright 2011 Mozilla Foundation and contributors
michael@0 700 * Licensed under the New BSD license. See LICENSE or:
michael@0 701 * http://opensource.org/licenses/BSD-3-Clause
michael@0 702 */
michael@0 703 define('source-map/binary-search', ['require', 'exports', 'module' , ], function(require, exports, module) {
michael@0 704
michael@0 705 /**
michael@0 706 * Recursive implementation of binary search.
michael@0 707 *
michael@0 708 * @param aLow Indices here and lower do not contain the needle.
michael@0 709 * @param aHigh Indices here and higher do not contain the needle.
michael@0 710 * @param aNeedle The element being searched for.
michael@0 711 * @param aHaystack The non-empty array being searched.
michael@0 712 * @param aCompare Function which takes two elements and returns -1, 0, or 1.
michael@0 713 */
michael@0 714 function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare) {
michael@0 715 // This function terminates when one of the following is true:
michael@0 716 //
michael@0 717 // 1. We find the exact element we are looking for.
michael@0 718 //
michael@0 719 // 2. We did not find the exact element, but we can return the next
michael@0 720 // closest element that is less than that element.
michael@0 721 //
michael@0 722 // 3. We did not find the exact element, and there is no next-closest
michael@0 723 // element which is less than the one we are searching for, so we
michael@0 724 // return null.
michael@0 725 var mid = Math.floor((aHigh - aLow) / 2) + aLow;
michael@0 726 var cmp = aCompare(aNeedle, aHaystack[mid], true);
michael@0 727 if (cmp === 0) {
michael@0 728 // Found the element we are looking for.
michael@0 729 return aHaystack[mid];
michael@0 730 }
michael@0 731 else if (cmp > 0) {
michael@0 732 // aHaystack[mid] is greater than our needle.
michael@0 733 if (aHigh - mid > 1) {
michael@0 734 // The element is in the upper half.
michael@0 735 return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare);
michael@0 736 }
michael@0 737 // We did not find an exact match, return the next closest one
michael@0 738 // (termination case 2).
michael@0 739 return aHaystack[mid];
michael@0 740 }
michael@0 741 else {
michael@0 742 // aHaystack[mid] is less than our needle.
michael@0 743 if (mid - aLow > 1) {
michael@0 744 // The element is in the lower half.
michael@0 745 return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare);
michael@0 746 }
michael@0 747 // The exact needle element was not found in this haystack. Determine if
michael@0 748 // we are in termination case (2) or (3) and return the appropriate thing.
michael@0 749 return aLow < 0
michael@0 750 ? null
michael@0 751 : aHaystack[aLow];
michael@0 752 }
michael@0 753 }
michael@0 754
michael@0 755 /**
michael@0 756 * This is an implementation of binary search which will always try and return
michael@0 757 * the next lowest value checked if there is no exact hit. This is because
michael@0 758 * mappings between original and generated line/col pairs are single points,
michael@0 759 * and there is an implicit region between each of them, so a miss just means
michael@0 760 * that you aren't on the very start of a region.
michael@0 761 *
michael@0 762 * @param aNeedle The element you are looking for.
michael@0 763 * @param aHaystack The array that is being searched.
michael@0 764 * @param aCompare A function which takes the needle and an element in the
michael@0 765 * array and returns -1, 0, or 1 depending on whether the needle is less
michael@0 766 * than, equal to, or greater than the element, respectively.
michael@0 767 */
michael@0 768 exports.search = function search(aNeedle, aHaystack, aCompare) {
michael@0 769 return aHaystack.length > 0
michael@0 770 ? recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare)
michael@0 771 : null;
michael@0 772 };
michael@0 773
michael@0 774 });
michael@0 775 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 776 /*
michael@0 777 * Copyright 2011 Mozilla Foundation and contributors
michael@0 778 * Licensed under the New BSD license. See LICENSE or:
michael@0 779 * http://opensource.org/licenses/BSD-3-Clause
michael@0 780 */
michael@0 781 define('source-map/array-set', ['require', 'exports', 'module' , 'source-map/util'], function(require, exports, module) {
michael@0 782
michael@0 783 var util = require('source-map/util');
michael@0 784
michael@0 785 /**
michael@0 786 * A data structure which is a combination of an array and a set. Adding a new
michael@0 787 * member is O(1), testing for membership is O(1), and finding the index of an
michael@0 788 * element is O(1). Removing elements from the set is not supported. Only
michael@0 789 * strings are supported for membership.
michael@0 790 */
michael@0 791 function ArraySet() {
michael@0 792 this._array = [];
michael@0 793 this._set = {};
michael@0 794 }
michael@0 795
michael@0 796 /**
michael@0 797 * Static method for creating ArraySet instances from an existing array.
michael@0 798 */
michael@0 799 ArraySet.fromArray = function ArraySet_fromArray(aArray, aAllowDuplicates) {
michael@0 800 var set = new ArraySet();
michael@0 801 for (var i = 0, len = aArray.length; i < len; i++) {
michael@0 802 set.add(aArray[i], aAllowDuplicates);
michael@0 803 }
michael@0 804 return set;
michael@0 805 };
michael@0 806
michael@0 807 /**
michael@0 808 * Add the given string to this set.
michael@0 809 *
michael@0 810 * @param String aStr
michael@0 811 */
michael@0 812 ArraySet.prototype.add = function ArraySet_add(aStr, aAllowDuplicates) {
michael@0 813 var isDuplicate = this.has(aStr);
michael@0 814 var idx = this._array.length;
michael@0 815 if (!isDuplicate || aAllowDuplicates) {
michael@0 816 this._array.push(aStr);
michael@0 817 }
michael@0 818 if (!isDuplicate) {
michael@0 819 this._set[util.toSetString(aStr)] = idx;
michael@0 820 }
michael@0 821 };
michael@0 822
michael@0 823 /**
michael@0 824 * Is the given string a member of this set?
michael@0 825 *
michael@0 826 * @param String aStr
michael@0 827 */
michael@0 828 ArraySet.prototype.has = function ArraySet_has(aStr) {
michael@0 829 return Object.prototype.hasOwnProperty.call(this._set,
michael@0 830 util.toSetString(aStr));
michael@0 831 };
michael@0 832
michael@0 833 /**
michael@0 834 * What is the index of the given string in the array?
michael@0 835 *
michael@0 836 * @param String aStr
michael@0 837 */
michael@0 838 ArraySet.prototype.indexOf = function ArraySet_indexOf(aStr) {
michael@0 839 if (this.has(aStr)) {
michael@0 840 return this._set[util.toSetString(aStr)];
michael@0 841 }
michael@0 842 throw new Error('"' + aStr + '" is not in the set.');
michael@0 843 };
michael@0 844
michael@0 845 /**
michael@0 846 * What is the element at the given index?
michael@0 847 *
michael@0 848 * @param Number aIdx
michael@0 849 */
michael@0 850 ArraySet.prototype.at = function ArraySet_at(aIdx) {
michael@0 851 if (aIdx >= 0 && aIdx < this._array.length) {
michael@0 852 return this._array[aIdx];
michael@0 853 }
michael@0 854 throw new Error('No element indexed by ' + aIdx);
michael@0 855 };
michael@0 856
michael@0 857 /**
michael@0 858 * Returns the array representation of this set (which has the proper indices
michael@0 859 * indicated by indexOf). Note that this is a copy of the internal array used
michael@0 860 * for storing the members so that no one can mess with internal state.
michael@0 861 */
michael@0 862 ArraySet.prototype.toArray = function ArraySet_toArray() {
michael@0 863 return this._array.slice();
michael@0 864 };
michael@0 865
michael@0 866 exports.ArraySet = ArraySet;
michael@0 867
michael@0 868 });
michael@0 869 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 870 /*
michael@0 871 * Copyright 2011 Mozilla Foundation and contributors
michael@0 872 * Licensed under the New BSD license. See LICENSE or:
michael@0 873 * http://opensource.org/licenses/BSD-3-Clause
michael@0 874 *
michael@0 875 * Based on the Base 64 VLQ implementation in Closure Compiler:
michael@0 876 * https://code.google.com/p/closure-compiler/source/browse/trunk/src/com/google/debugging/sourcemap/Base64VLQ.java
michael@0 877 *
michael@0 878 * Copyright 2011 The Closure Compiler Authors. All rights reserved.
michael@0 879 * Redistribution and use in source and binary forms, with or without
michael@0 880 * modification, are permitted provided that the following conditions are
michael@0 881 * met:
michael@0 882 *
michael@0 883 * * Redistributions of source code must retain the above copyright
michael@0 884 * notice, this list of conditions and the following disclaimer.
michael@0 885 * * Redistributions in binary form must reproduce the above
michael@0 886 * copyright notice, this list of conditions and the following
michael@0 887 * disclaimer in the documentation and/or other materials provided
michael@0 888 * with the distribution.
michael@0 889 * * Neither the name of Google Inc. nor the names of its
michael@0 890 * contributors may be used to endorse or promote products derived
michael@0 891 * from this software without specific prior written permission.
michael@0 892 *
michael@0 893 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 894 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 895 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 896 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 897 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 898 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 899 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 900 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 901 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 902 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 903 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 904 */
michael@0 905 define('source-map/base64-vlq', ['require', 'exports', 'module' , 'source-map/base64'], function(require, exports, module) {
michael@0 906
michael@0 907 var base64 = require('source-map/base64');
michael@0 908
michael@0 909 // A single base 64 digit can contain 6 bits of data. For the base 64 variable
michael@0 910 // length quantities we use in the source map spec, the first bit is the sign,
michael@0 911 // the next four bits are the actual value, and the 6th bit is the
michael@0 912 // continuation bit. The continuation bit tells us whether there are more
michael@0 913 // digits in this value following this digit.
michael@0 914 //
michael@0 915 // Continuation
michael@0 916 // | Sign
michael@0 917 // | |
michael@0 918 // V V
michael@0 919 // 101011
michael@0 920
michael@0 921 var VLQ_BASE_SHIFT = 5;
michael@0 922
michael@0 923 // binary: 100000
michael@0 924 var VLQ_BASE = 1 << VLQ_BASE_SHIFT;
michael@0 925
michael@0 926 // binary: 011111
michael@0 927 var VLQ_BASE_MASK = VLQ_BASE - 1;
michael@0 928
michael@0 929 // binary: 100000
michael@0 930 var VLQ_CONTINUATION_BIT = VLQ_BASE;
michael@0 931
michael@0 932 /**
michael@0 933 * Converts from a two-complement value to a value where the sign bit is
michael@0 934 * is placed in the least significant bit. For example, as decimals:
michael@0 935 * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
michael@0 936 * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
michael@0 937 */
michael@0 938 function toVLQSigned(aValue) {
michael@0 939 return aValue < 0
michael@0 940 ? ((-aValue) << 1) + 1
michael@0 941 : (aValue << 1) + 0;
michael@0 942 }
michael@0 943
michael@0 944 /**
michael@0 945 * Converts to a two-complement value from a value where the sign bit is
michael@0 946 * is placed in the least significant bit. For example, as decimals:
michael@0 947 * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1
michael@0 948 * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2
michael@0 949 */
michael@0 950 function fromVLQSigned(aValue) {
michael@0 951 var isNegative = (aValue & 1) === 1;
michael@0 952 var shifted = aValue >> 1;
michael@0 953 return isNegative
michael@0 954 ? -shifted
michael@0 955 : shifted;
michael@0 956 }
michael@0 957
michael@0 958 /**
michael@0 959 * Returns the base 64 VLQ encoded value.
michael@0 960 */
michael@0 961 exports.encode = function base64VLQ_encode(aValue) {
michael@0 962 var encoded = "";
michael@0 963 var digit;
michael@0 964
michael@0 965 var vlq = toVLQSigned(aValue);
michael@0 966
michael@0 967 do {
michael@0 968 digit = vlq & VLQ_BASE_MASK;
michael@0 969 vlq >>>= VLQ_BASE_SHIFT;
michael@0 970 if (vlq > 0) {
michael@0 971 // There are still more digits in this value, so we must make sure the
michael@0 972 // continuation bit is marked.
michael@0 973 digit |= VLQ_CONTINUATION_BIT;
michael@0 974 }
michael@0 975 encoded += base64.encode(digit);
michael@0 976 } while (vlq > 0);
michael@0 977
michael@0 978 return encoded;
michael@0 979 };
michael@0 980
michael@0 981 /**
michael@0 982 * Decodes the next base 64 VLQ value from the given string and returns the
michael@0 983 * value and the rest of the string.
michael@0 984 */
michael@0 985 exports.decode = function base64VLQ_decode(aStr) {
michael@0 986 var i = 0;
michael@0 987 var strLen = aStr.length;
michael@0 988 var result = 0;
michael@0 989 var shift = 0;
michael@0 990 var continuation, digit;
michael@0 991
michael@0 992 do {
michael@0 993 if (i >= strLen) {
michael@0 994 throw new Error("Expected more digits in base 64 VLQ value.");
michael@0 995 }
michael@0 996 digit = base64.decode(aStr.charAt(i++));
michael@0 997 continuation = !!(digit & VLQ_CONTINUATION_BIT);
michael@0 998 digit &= VLQ_BASE_MASK;
michael@0 999 result = result + (digit << shift);
michael@0 1000 shift += VLQ_BASE_SHIFT;
michael@0 1001 } while (continuation);
michael@0 1002
michael@0 1003 return {
michael@0 1004 value: fromVLQSigned(result),
michael@0 1005 rest: aStr.slice(i)
michael@0 1006 };
michael@0 1007 };
michael@0 1008
michael@0 1009 });
michael@0 1010 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 1011 /*
michael@0 1012 * Copyright 2011 Mozilla Foundation and contributors
michael@0 1013 * Licensed under the New BSD license. See LICENSE or:
michael@0 1014 * http://opensource.org/licenses/BSD-3-Clause
michael@0 1015 */
michael@0 1016 define('source-map/base64', ['require', 'exports', 'module' , ], function(require, exports, module) {
michael@0 1017
michael@0 1018 var charToIntMap = {};
michael@0 1019 var intToCharMap = {};
michael@0 1020
michael@0 1021 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
michael@0 1022 .split('')
michael@0 1023 .forEach(function (ch, index) {
michael@0 1024 charToIntMap[ch] = index;
michael@0 1025 intToCharMap[index] = ch;
michael@0 1026 });
michael@0 1027
michael@0 1028 /**
michael@0 1029 * Encode an integer in the range of 0 to 63 to a single base 64 digit.
michael@0 1030 */
michael@0 1031 exports.encode = function base64_encode(aNumber) {
michael@0 1032 if (aNumber in intToCharMap) {
michael@0 1033 return intToCharMap[aNumber];
michael@0 1034 }
michael@0 1035 throw new TypeError("Must be between 0 and 63: " + aNumber);
michael@0 1036 };
michael@0 1037
michael@0 1038 /**
michael@0 1039 * Decode a single base 64 digit to an integer.
michael@0 1040 */
michael@0 1041 exports.decode = function base64_decode(aChar) {
michael@0 1042 if (aChar in charToIntMap) {
michael@0 1043 return charToIntMap[aChar];
michael@0 1044 }
michael@0 1045 throw new TypeError("Not a valid base 64 digit: " + aChar);
michael@0 1046 };
michael@0 1047
michael@0 1048 });
michael@0 1049 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 1050 /*
michael@0 1051 * Copyright 2011 Mozilla Foundation and contributors
michael@0 1052 * Licensed under the New BSD license. See LICENSE or:
michael@0 1053 * http://opensource.org/licenses/BSD-3-Clause
michael@0 1054 */
michael@0 1055 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 1056
michael@0 1057 var base64VLQ = require('source-map/base64-vlq');
michael@0 1058 var util = require('source-map/util');
michael@0 1059 var ArraySet = require('source-map/array-set').ArraySet;
michael@0 1060
michael@0 1061 /**
michael@0 1062 * An instance of the SourceMapGenerator represents a source map which is
michael@0 1063 * being built incrementally. To create a new one, you must pass an object
michael@0 1064 * with the following properties:
michael@0 1065 *
michael@0 1066 * - file: The filename of the generated source.
michael@0 1067 * - sourceRoot: An optional root for all URLs in this source map.
michael@0 1068 */
michael@0 1069 function SourceMapGenerator(aArgs) {
michael@0 1070 this._file = util.getArg(aArgs, 'file');
michael@0 1071 this._sourceRoot = util.getArg(aArgs, 'sourceRoot', null);
michael@0 1072 this._sources = new ArraySet();
michael@0 1073 this._names = new ArraySet();
michael@0 1074 this._mappings = [];
michael@0 1075 this._sourcesContents = null;
michael@0 1076 }
michael@0 1077
michael@0 1078 SourceMapGenerator.prototype._version = 3;
michael@0 1079
michael@0 1080 /**
michael@0 1081 * Creates a new SourceMapGenerator based on a SourceMapConsumer
michael@0 1082 *
michael@0 1083 * @param aSourceMapConsumer The SourceMap.
michael@0 1084 */
michael@0 1085 SourceMapGenerator.fromSourceMap =
michael@0 1086 function SourceMapGenerator_fromSourceMap(aSourceMapConsumer) {
michael@0 1087 var sourceRoot = aSourceMapConsumer.sourceRoot;
michael@0 1088 var generator = new SourceMapGenerator({
michael@0 1089 file: aSourceMapConsumer.file,
michael@0 1090 sourceRoot: sourceRoot
michael@0 1091 });
michael@0 1092 aSourceMapConsumer.eachMapping(function (mapping) {
michael@0 1093 var newMapping = {
michael@0 1094 generated: {
michael@0 1095 line: mapping.generatedLine,
michael@0 1096 column: mapping.generatedColumn
michael@0 1097 }
michael@0 1098 };
michael@0 1099
michael@0 1100 if (mapping.source) {
michael@0 1101 newMapping.source = mapping.source;
michael@0 1102 if (sourceRoot) {
michael@0 1103 newMapping.source = util.relative(sourceRoot, newMapping.source);
michael@0 1104 }
michael@0 1105
michael@0 1106 newMapping.original = {
michael@0 1107 line: mapping.originalLine,
michael@0 1108 column: mapping.originalColumn
michael@0 1109 };
michael@0 1110
michael@0 1111 if (mapping.name) {
michael@0 1112 newMapping.name = mapping.name;
michael@0 1113 }
michael@0 1114 }
michael@0 1115
michael@0 1116 generator.addMapping(newMapping);
michael@0 1117 });
michael@0 1118 aSourceMapConsumer.sources.forEach(function (sourceFile) {
michael@0 1119 var content = aSourceMapConsumer.sourceContentFor(sourceFile);
michael@0 1120 if (content) {
michael@0 1121 generator.setSourceContent(sourceFile, content);
michael@0 1122 }
michael@0 1123 });
michael@0 1124 return generator;
michael@0 1125 };
michael@0 1126
michael@0 1127 /**
michael@0 1128 * Add a single mapping from original source line and column to the generated
michael@0 1129 * source's line and column for this source map being created. The mapping
michael@0 1130 * object should have the following properties:
michael@0 1131 *
michael@0 1132 * - generated: An object with the generated line and column positions.
michael@0 1133 * - original: An object with the original line and column positions.
michael@0 1134 * - source: The original source file (relative to the sourceRoot).
michael@0 1135 * - name: An optional original token name for this mapping.
michael@0 1136 */
michael@0 1137 SourceMapGenerator.prototype.addMapping =
michael@0 1138 function SourceMapGenerator_addMapping(aArgs) {
michael@0 1139 var generated = util.getArg(aArgs, 'generated');
michael@0 1140 var original = util.getArg(aArgs, 'original', null);
michael@0 1141 var source = util.getArg(aArgs, 'source', null);
michael@0 1142 var name = util.getArg(aArgs, 'name', null);
michael@0 1143
michael@0 1144 this._validateMapping(generated, original, source, name);
michael@0 1145
michael@0 1146 if (source && !this._sources.has(source)) {
michael@0 1147 this._sources.add(source);
michael@0 1148 }
michael@0 1149
michael@0 1150 if (name && !this._names.has(name)) {
michael@0 1151 this._names.add(name);
michael@0 1152 }
michael@0 1153
michael@0 1154 this._mappings.push({
michael@0 1155 generatedLine: generated.line,
michael@0 1156 generatedColumn: generated.column,
michael@0 1157 originalLine: original != null && original.line,
michael@0 1158 originalColumn: original != null && original.column,
michael@0 1159 source: source,
michael@0 1160 name: name
michael@0 1161 });
michael@0 1162 };
michael@0 1163
michael@0 1164 /**
michael@0 1165 * Set the source content for a source file.
michael@0 1166 */
michael@0 1167 SourceMapGenerator.prototype.setSourceContent =
michael@0 1168 function SourceMapGenerator_setSourceContent(aSourceFile, aSourceContent) {
michael@0 1169 var source = aSourceFile;
michael@0 1170 if (this._sourceRoot) {
michael@0 1171 source = util.relative(this._sourceRoot, source);
michael@0 1172 }
michael@0 1173
michael@0 1174 if (aSourceContent !== null) {
michael@0 1175 // Add the source content to the _sourcesContents map.
michael@0 1176 // Create a new _sourcesContents map if the property is null.
michael@0 1177 if (!this._sourcesContents) {
michael@0 1178 this._sourcesContents = {};
michael@0 1179 }
michael@0 1180 this._sourcesContents[util.toSetString(source)] = aSourceContent;
michael@0 1181 } else {
michael@0 1182 // Remove the source file from the _sourcesContents map.
michael@0 1183 // If the _sourcesContents map is empty, set the property to null.
michael@0 1184 delete this._sourcesContents[util.toSetString(source)];
michael@0 1185 if (Object.keys(this._sourcesContents).length === 0) {
michael@0 1186 this._sourcesContents = null;
michael@0 1187 }
michael@0 1188 }
michael@0 1189 };
michael@0 1190
michael@0 1191 /**
michael@0 1192 * Applies the mappings of a sub-source-map for a specific source file to the
michael@0 1193 * source map being generated. Each mapping to the supplied source file is
michael@0 1194 * rewritten using the supplied source map. Note: The resolution for the
michael@0 1195 * resulting mappings is the minimium of this map and the supplied map.
michael@0 1196 *
michael@0 1197 * @param aSourceMapConsumer The source map to be applied.
michael@0 1198 * @param aSourceFile Optional. The filename of the source file.
michael@0 1199 * If omitted, SourceMapConsumer's file property will be used.
michael@0 1200 */
michael@0 1201 SourceMapGenerator.prototype.applySourceMap =
michael@0 1202 function SourceMapGenerator_applySourceMap(aSourceMapConsumer, aSourceFile) {
michael@0 1203 // If aSourceFile is omitted, we will use the file property of the SourceMap
michael@0 1204 if (!aSourceFile) {
michael@0 1205 aSourceFile = aSourceMapConsumer.file;
michael@0 1206 }
michael@0 1207 var sourceRoot = this._sourceRoot;
michael@0 1208 // Make "aSourceFile" relative if an absolute Url is passed.
michael@0 1209 if (sourceRoot) {
michael@0 1210 aSourceFile = util.relative(sourceRoot, aSourceFile);
michael@0 1211 }
michael@0 1212 // Applying the SourceMap can add and remove items from the sources and
michael@0 1213 // the names array.
michael@0 1214 var newSources = new ArraySet();
michael@0 1215 var newNames = new ArraySet();
michael@0 1216
michael@0 1217 // Find mappings for the "aSourceFile"
michael@0 1218 this._mappings.forEach(function (mapping) {
michael@0 1219 if (mapping.source === aSourceFile && mapping.originalLine) {
michael@0 1220 // Check if it can be mapped by the source map, then update the mapping.
michael@0 1221 var original = aSourceMapConsumer.originalPositionFor({
michael@0 1222 line: mapping.originalLine,
michael@0 1223 column: mapping.originalColumn
michael@0 1224 });
michael@0 1225 if (original.source !== null) {
michael@0 1226 // Copy mapping
michael@0 1227 if (sourceRoot) {
michael@0 1228 mapping.source = util.relative(sourceRoot, original.source);
michael@0 1229 } else {
michael@0 1230 mapping.source = original.source;
michael@0 1231 }
michael@0 1232 mapping.originalLine = original.line;
michael@0 1233 mapping.originalColumn = original.column;
michael@0 1234 if (original.name !== null && mapping.name !== null) {
michael@0 1235 // Only use the identifier name if it's an identifier
michael@0 1236 // in both SourceMaps
michael@0 1237 mapping.name = original.name;
michael@0 1238 }
michael@0 1239 }
michael@0 1240 }
michael@0 1241
michael@0 1242 var source = mapping.source;
michael@0 1243 if (source && !newSources.has(source)) {
michael@0 1244 newSources.add(source);
michael@0 1245 }
michael@0 1246
michael@0 1247 var name = mapping.name;
michael@0 1248 if (name && !newNames.has(name)) {
michael@0 1249 newNames.add(name);
michael@0 1250 }
michael@0 1251
michael@0 1252 }, this);
michael@0 1253 this._sources = newSources;
michael@0 1254 this._names = newNames;
michael@0 1255
michael@0 1256 // Copy sourcesContents of applied map.
michael@0 1257 aSourceMapConsumer.sources.forEach(function (sourceFile) {
michael@0 1258 var content = aSourceMapConsumer.sourceContentFor(sourceFile);
michael@0 1259 if (content) {
michael@0 1260 if (sourceRoot) {
michael@0 1261 sourceFile = util.relative(sourceRoot, sourceFile);
michael@0 1262 }
michael@0 1263 this.setSourceContent(sourceFile, content);
michael@0 1264 }
michael@0 1265 }, this);
michael@0 1266 };
michael@0 1267
michael@0 1268 /**
michael@0 1269 * A mapping can have one of the three levels of data:
michael@0 1270 *
michael@0 1271 * 1. Just the generated position.
michael@0 1272 * 2. The Generated position, original position, and original source.
michael@0 1273 * 3. Generated and original position, original source, as well as a name
michael@0 1274 * token.
michael@0 1275 *
michael@0 1276 * To maintain consistency, we validate that any new mapping being added falls
michael@0 1277 * in to one of these categories.
michael@0 1278 */
michael@0 1279 SourceMapGenerator.prototype._validateMapping =
michael@0 1280 function SourceMapGenerator_validateMapping(aGenerated, aOriginal, aSource,
michael@0 1281 aName) {
michael@0 1282 if (aGenerated && 'line' in aGenerated && 'column' in aGenerated
michael@0 1283 && aGenerated.line > 0 && aGenerated.column >= 0
michael@0 1284 && !aOriginal && !aSource && !aName) {
michael@0 1285 // Case 1.
michael@0 1286 return;
michael@0 1287 }
michael@0 1288 else if (aGenerated && 'line' in aGenerated && 'column' in aGenerated
michael@0 1289 && aOriginal && 'line' in aOriginal && 'column' in aOriginal
michael@0 1290 && aGenerated.line > 0 && aGenerated.column >= 0
michael@0 1291 && aOriginal.line > 0 && aOriginal.column >= 0
michael@0 1292 && aSource) {
michael@0 1293 // Cases 2 and 3.
michael@0 1294 return;
michael@0 1295 }
michael@0 1296 else {
michael@0 1297 throw new Error('Invalid mapping: ' + JSON.stringify({
michael@0 1298 generated: aGenerated,
michael@0 1299 source: aSource,
michael@0 1300 original: aOriginal,
michael@0 1301 name: aName
michael@0 1302 }));
michael@0 1303 }
michael@0 1304 };
michael@0 1305
michael@0 1306 /**
michael@0 1307 * Serialize the accumulated mappings in to the stream of base 64 VLQs
michael@0 1308 * specified by the source map format.
michael@0 1309 */
michael@0 1310 SourceMapGenerator.prototype._serializeMappings =
michael@0 1311 function SourceMapGenerator_serializeMappings() {
michael@0 1312 var previousGeneratedColumn = 0;
michael@0 1313 var previousGeneratedLine = 1;
michael@0 1314 var previousOriginalColumn = 0;
michael@0 1315 var previousOriginalLine = 0;
michael@0 1316 var previousName = 0;
michael@0 1317 var previousSource = 0;
michael@0 1318 var result = '';
michael@0 1319 var mapping;
michael@0 1320
michael@0 1321 // The mappings must be guaranteed to be in sorted order before we start
michael@0 1322 // serializing them or else the generated line numbers (which are defined
michael@0 1323 // via the ';' separators) will be all messed up. Note: it might be more
michael@0 1324 // performant to maintain the sorting as we insert them, rather than as we
michael@0 1325 // serialize them, but the big O is the same either way.
michael@0 1326 this._mappings.sort(util.compareByGeneratedPositions);
michael@0 1327
michael@0 1328 for (var i = 0, len = this._mappings.length; i < len; i++) {
michael@0 1329 mapping = this._mappings[i];
michael@0 1330
michael@0 1331 if (mapping.generatedLine !== previousGeneratedLine) {
michael@0 1332 previousGeneratedColumn = 0;
michael@0 1333 while (mapping.generatedLine !== previousGeneratedLine) {
michael@0 1334 result += ';';
michael@0 1335 previousGeneratedLine++;
michael@0 1336 }
michael@0 1337 }
michael@0 1338 else {
michael@0 1339 if (i > 0) {
michael@0 1340 if (!util.compareByGeneratedPositions(mapping, this._mappings[i - 1])) {
michael@0 1341 continue;
michael@0 1342 }
michael@0 1343 result += ',';
michael@0 1344 }
michael@0 1345 }
michael@0 1346
michael@0 1347 result += base64VLQ.encode(mapping.generatedColumn
michael@0 1348 - previousGeneratedColumn);
michael@0 1349 previousGeneratedColumn = mapping.generatedColumn;
michael@0 1350
michael@0 1351 if (mapping.source) {
michael@0 1352 result += base64VLQ.encode(this._sources.indexOf(mapping.source)
michael@0 1353 - previousSource);
michael@0 1354 previousSource = this._sources.indexOf(mapping.source);
michael@0 1355
michael@0 1356 // lines are stored 0-based in SourceMap spec version 3
michael@0 1357 result += base64VLQ.encode(mapping.originalLine - 1
michael@0 1358 - previousOriginalLine);
michael@0 1359 previousOriginalLine = mapping.originalLine - 1;
michael@0 1360
michael@0 1361 result += base64VLQ.encode(mapping.originalColumn
michael@0 1362 - previousOriginalColumn);
michael@0 1363 previousOriginalColumn = mapping.originalColumn;
michael@0 1364
michael@0 1365 if (mapping.name) {
michael@0 1366 result += base64VLQ.encode(this._names.indexOf(mapping.name)
michael@0 1367 - previousName);
michael@0 1368 previousName = this._names.indexOf(mapping.name);
michael@0 1369 }
michael@0 1370 }
michael@0 1371 }
michael@0 1372
michael@0 1373 return result;
michael@0 1374 };
michael@0 1375
michael@0 1376 SourceMapGenerator.prototype._generateSourcesContent =
michael@0 1377 function SourceMapGenerator_generateSourcesContent(aSources, aSourceRoot) {
michael@0 1378 return aSources.map(function (source) {
michael@0 1379 if (!this._sourcesContents) {
michael@0 1380 return null;
michael@0 1381 }
michael@0 1382 if (aSourceRoot) {
michael@0 1383 source = util.relative(aSourceRoot, source);
michael@0 1384 }
michael@0 1385 var key = util.toSetString(source);
michael@0 1386 return Object.prototype.hasOwnProperty.call(this._sourcesContents,
michael@0 1387 key)
michael@0 1388 ? this._sourcesContents[key]
michael@0 1389 : null;
michael@0 1390 }, this);
michael@0 1391 };
michael@0 1392
michael@0 1393 /**
michael@0 1394 * Externalize the source map.
michael@0 1395 */
michael@0 1396 SourceMapGenerator.prototype.toJSON =
michael@0 1397 function SourceMapGenerator_toJSON() {
michael@0 1398 var map = {
michael@0 1399 version: this._version,
michael@0 1400 file: this._file,
michael@0 1401 sources: this._sources.toArray(),
michael@0 1402 names: this._names.toArray(),
michael@0 1403 mappings: this._serializeMappings()
michael@0 1404 };
michael@0 1405 if (this._sourceRoot) {
michael@0 1406 map.sourceRoot = this._sourceRoot;
michael@0 1407 }
michael@0 1408 if (this._sourcesContents) {
michael@0 1409 map.sourcesContent = this._generateSourcesContent(map.sources, map.sourceRoot);
michael@0 1410 }
michael@0 1411
michael@0 1412 return map;
michael@0 1413 };
michael@0 1414
michael@0 1415 /**
michael@0 1416 * Render the source map being generated to a string.
michael@0 1417 */
michael@0 1418 SourceMapGenerator.prototype.toString =
michael@0 1419 function SourceMapGenerator_toString() {
michael@0 1420 return JSON.stringify(this);
michael@0 1421 };
michael@0 1422
michael@0 1423 exports.SourceMapGenerator = SourceMapGenerator;
michael@0 1424
michael@0 1425 });
michael@0 1426 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 1427 /*
michael@0 1428 * Copyright 2011 Mozilla Foundation and contributors
michael@0 1429 * Licensed under the New BSD license. See LICENSE or:
michael@0 1430 * http://opensource.org/licenses/BSD-3-Clause
michael@0 1431 */
michael@0 1432 define('source-map/source-node', ['require', 'exports', 'module' , 'source-map/source-map-generator', 'source-map/util'], function(require, exports, module) {
michael@0 1433
michael@0 1434 var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
michael@0 1435 var util = require('source-map/util');
michael@0 1436
michael@0 1437 /**
michael@0 1438 * SourceNodes provide a way to abstract over interpolating/concatenating
michael@0 1439 * snippets of generated JavaScript source code while maintaining the line and
michael@0 1440 * column information associated with the original source code.
michael@0 1441 *
michael@0 1442 * @param aLine The original line number.
michael@0 1443 * @param aColumn The original column number.
michael@0 1444 * @param aSource The original source's filename.
michael@0 1445 * @param aChunks Optional. An array of strings which are snippets of
michael@0 1446 * generated JS, or other SourceNodes.
michael@0 1447 * @param aName The original identifier.
michael@0 1448 */
michael@0 1449 function SourceNode(aLine, aColumn, aSource, aChunks, aName) {
michael@0 1450 this.children = [];
michael@0 1451 this.sourceContents = {};
michael@0 1452 this.line = aLine === undefined ? null : aLine;
michael@0 1453 this.column = aColumn === undefined ? null : aColumn;
michael@0 1454 this.source = aSource === undefined ? null : aSource;
michael@0 1455 this.name = aName === undefined ? null : aName;
michael@0 1456 if (aChunks != null) this.add(aChunks);
michael@0 1457 }
michael@0 1458
michael@0 1459 /**
michael@0 1460 * Creates a SourceNode from generated code and a SourceMapConsumer.
michael@0 1461 *
michael@0 1462 * @param aGeneratedCode The generated code
michael@0 1463 * @param aSourceMapConsumer The SourceMap for the generated code
michael@0 1464 */
michael@0 1465 SourceNode.fromStringWithSourceMap =
michael@0 1466 function SourceNode_fromStringWithSourceMap(aGeneratedCode, aSourceMapConsumer) {
michael@0 1467 // The SourceNode we want to fill with the generated code
michael@0 1468 // and the SourceMap
michael@0 1469 var node = new SourceNode();
michael@0 1470
michael@0 1471 // The generated code
michael@0 1472 // Processed fragments are removed from this array.
michael@0 1473 var remainingLines = aGeneratedCode.split('\n');
michael@0 1474
michael@0 1475 // We need to remember the position of "remainingLines"
michael@0 1476 var lastGeneratedLine = 1, lastGeneratedColumn = 0;
michael@0 1477
michael@0 1478 // The generate SourceNodes we need a code range.
michael@0 1479 // To extract it current and last mapping is used.
michael@0 1480 // Here we store the last mapping.
michael@0 1481 var lastMapping = null;
michael@0 1482
michael@0 1483 aSourceMapConsumer.eachMapping(function (mapping) {
michael@0 1484 if (lastMapping === null) {
michael@0 1485 // We add the generated code until the first mapping
michael@0 1486 // to the SourceNode without any mapping.
michael@0 1487 // Each line is added as separate string.
michael@0 1488 while (lastGeneratedLine < mapping.generatedLine) {
michael@0 1489 node.add(remainingLines.shift() + "\n");
michael@0 1490 lastGeneratedLine++;
michael@0 1491 }
michael@0 1492 if (lastGeneratedColumn < mapping.generatedColumn) {
michael@0 1493 var nextLine = remainingLines[0];
michael@0 1494 node.add(nextLine.substr(0, mapping.generatedColumn));
michael@0 1495 remainingLines[0] = nextLine.substr(mapping.generatedColumn);
michael@0 1496 lastGeneratedColumn = mapping.generatedColumn;
michael@0 1497 }
michael@0 1498 } else {
michael@0 1499 // We add the code from "lastMapping" to "mapping":
michael@0 1500 // First check if there is a new line in between.
michael@0 1501 if (lastGeneratedLine < mapping.generatedLine) {
michael@0 1502 var code = "";
michael@0 1503 // Associate full lines with "lastMapping"
michael@0 1504 do {
michael@0 1505 code += remainingLines.shift() + "\n";
michael@0 1506 lastGeneratedLine++;
michael@0 1507 lastGeneratedColumn = 0;
michael@0 1508 } while (lastGeneratedLine < mapping.generatedLine);
michael@0 1509 // When we reached the correct line, we add code until we
michael@0 1510 // reach the correct column too.
michael@0 1511 if (lastGeneratedColumn < mapping.generatedColumn) {
michael@0 1512 var nextLine = remainingLines[0];
michael@0 1513 code += nextLine.substr(0, mapping.generatedColumn);
michael@0 1514 remainingLines[0] = nextLine.substr(mapping.generatedColumn);
michael@0 1515 lastGeneratedColumn = mapping.generatedColumn;
michael@0 1516 }
michael@0 1517 // Create the SourceNode.
michael@0 1518 addMappingWithCode(lastMapping, code);
michael@0 1519 } else {
michael@0 1520 // There is no new line in between.
michael@0 1521 // Associate the code between "lastGeneratedColumn" and
michael@0 1522 // "mapping.generatedColumn" with "lastMapping"
michael@0 1523 var nextLine = remainingLines[0];
michael@0 1524 var code = nextLine.substr(0, mapping.generatedColumn -
michael@0 1525 lastGeneratedColumn);
michael@0 1526 remainingLines[0] = nextLine.substr(mapping.generatedColumn -
michael@0 1527 lastGeneratedColumn);
michael@0 1528 lastGeneratedColumn = mapping.generatedColumn;
michael@0 1529 addMappingWithCode(lastMapping, code);
michael@0 1530 }
michael@0 1531 }
michael@0 1532 lastMapping = mapping;
michael@0 1533 }, this);
michael@0 1534 // We have processed all mappings.
michael@0 1535 // Associate the remaining code in the current line with "lastMapping"
michael@0 1536 // and add the remaining lines without any mapping
michael@0 1537 addMappingWithCode(lastMapping, remainingLines.join("\n"));
michael@0 1538
michael@0 1539 // Copy sourcesContent into SourceNode
michael@0 1540 aSourceMapConsumer.sources.forEach(function (sourceFile) {
michael@0 1541 var content = aSourceMapConsumer.sourceContentFor(sourceFile);
michael@0 1542 if (content) {
michael@0 1543 node.setSourceContent(sourceFile, content);
michael@0 1544 }
michael@0 1545 });
michael@0 1546
michael@0 1547 return node;
michael@0 1548
michael@0 1549 function addMappingWithCode(mapping, code) {
michael@0 1550 if (mapping === null || mapping.source === undefined) {
michael@0 1551 node.add(code);
michael@0 1552 } else {
michael@0 1553 node.add(new SourceNode(mapping.originalLine,
michael@0 1554 mapping.originalColumn,
michael@0 1555 mapping.source,
michael@0 1556 code,
michael@0 1557 mapping.name));
michael@0 1558 }
michael@0 1559 }
michael@0 1560 };
michael@0 1561
michael@0 1562 /**
michael@0 1563 * Add a chunk of generated JS to this source node.
michael@0 1564 *
michael@0 1565 * @param aChunk A string snippet of generated JS code, another instance of
michael@0 1566 * SourceNode, or an array where each member is one of those things.
michael@0 1567 */
michael@0 1568 SourceNode.prototype.add = function SourceNode_add(aChunk) {
michael@0 1569 if (Array.isArray(aChunk)) {
michael@0 1570 aChunk.forEach(function (chunk) {
michael@0 1571 this.add(chunk);
michael@0 1572 }, this);
michael@0 1573 }
michael@0 1574 else if (aChunk instanceof SourceNode || typeof aChunk === "string") {
michael@0 1575 if (aChunk) {
michael@0 1576 this.children.push(aChunk);
michael@0 1577 }
michael@0 1578 }
michael@0 1579 else {
michael@0 1580 throw new TypeError(
michael@0 1581 "Expected a SourceNode, string, or an array of SourceNodes and strings. Got " + aChunk
michael@0 1582 );
michael@0 1583 }
michael@0 1584 return this;
michael@0 1585 };
michael@0 1586
michael@0 1587 /**
michael@0 1588 * Add a chunk of generated JS to the beginning of this source node.
michael@0 1589 *
michael@0 1590 * @param aChunk A string snippet of generated JS code, another instance of
michael@0 1591 * SourceNode, or an array where each member is one of those things.
michael@0 1592 */
michael@0 1593 SourceNode.prototype.prepend = function SourceNode_prepend(aChunk) {
michael@0 1594 if (Array.isArray(aChunk)) {
michael@0 1595 for (var i = aChunk.length-1; i >= 0; i--) {
michael@0 1596 this.prepend(aChunk[i]);
michael@0 1597 }
michael@0 1598 }
michael@0 1599 else if (aChunk instanceof SourceNode || typeof aChunk === "string") {
michael@0 1600 this.children.unshift(aChunk);
michael@0 1601 }
michael@0 1602 else {
michael@0 1603 throw new TypeError(
michael@0 1604 "Expected a SourceNode, string, or an array of SourceNodes and strings. Got " + aChunk
michael@0 1605 );
michael@0 1606 }
michael@0 1607 return this;
michael@0 1608 };
michael@0 1609
michael@0 1610 /**
michael@0 1611 * Walk over the tree of JS snippets in this node and its children. The
michael@0 1612 * walking function is called once for each snippet of JS and is passed that
michael@0 1613 * snippet and the its original associated source's line/column location.
michael@0 1614 *
michael@0 1615 * @param aFn The traversal function.
michael@0 1616 */
michael@0 1617 SourceNode.prototype.walk = function SourceNode_walk(aFn) {
michael@0 1618 var chunk;
michael@0 1619 for (var i = 0, len = this.children.length; i < len; i++) {
michael@0 1620 chunk = this.children[i];
michael@0 1621 if (chunk instanceof SourceNode) {
michael@0 1622 chunk.walk(aFn);
michael@0 1623 }
michael@0 1624 else {
michael@0 1625 if (chunk !== '') {
michael@0 1626 aFn(chunk, { source: this.source,
michael@0 1627 line: this.line,
michael@0 1628 column: this.column,
michael@0 1629 name: this.name });
michael@0 1630 }
michael@0 1631 }
michael@0 1632 }
michael@0 1633 };
michael@0 1634
michael@0 1635 /**
michael@0 1636 * Like `String.prototype.join` except for SourceNodes. Inserts `aStr` between
michael@0 1637 * each of `this.children`.
michael@0 1638 *
michael@0 1639 * @param aSep The separator.
michael@0 1640 */
michael@0 1641 SourceNode.prototype.join = function SourceNode_join(aSep) {
michael@0 1642 var newChildren;
michael@0 1643 var i;
michael@0 1644 var len = this.children.length;
michael@0 1645 if (len > 0) {
michael@0 1646 newChildren = [];
michael@0 1647 for (i = 0; i < len-1; i++) {
michael@0 1648 newChildren.push(this.children[i]);
michael@0 1649 newChildren.push(aSep);
michael@0 1650 }
michael@0 1651 newChildren.push(this.children[i]);
michael@0 1652 this.children = newChildren;
michael@0 1653 }
michael@0 1654 return this;
michael@0 1655 };
michael@0 1656
michael@0 1657 /**
michael@0 1658 * Call String.prototype.replace on the very right-most source snippet. Useful
michael@0 1659 * for trimming whitespace from the end of a source node, etc.
michael@0 1660 *
michael@0 1661 * @param aPattern The pattern to replace.
michael@0 1662 * @param aReplacement The thing to replace the pattern with.
michael@0 1663 */
michael@0 1664 SourceNode.prototype.replaceRight = function SourceNode_replaceRight(aPattern, aReplacement) {
michael@0 1665 var lastChild = this.children[this.children.length - 1];
michael@0 1666 if (lastChild instanceof SourceNode) {
michael@0 1667 lastChild.replaceRight(aPattern, aReplacement);
michael@0 1668 }
michael@0 1669 else if (typeof lastChild === 'string') {
michael@0 1670 this.children[this.children.length - 1] = lastChild.replace(aPattern, aReplacement);
michael@0 1671 }
michael@0 1672 else {
michael@0 1673 this.children.push(''.replace(aPattern, aReplacement));
michael@0 1674 }
michael@0 1675 return this;
michael@0 1676 };
michael@0 1677
michael@0 1678 /**
michael@0 1679 * Set the source content for a source file. This will be added to the SourceMapGenerator
michael@0 1680 * in the sourcesContent field.
michael@0 1681 *
michael@0 1682 * @param aSourceFile The filename of the source file
michael@0 1683 * @param aSourceContent The content of the source file
michael@0 1684 */
michael@0 1685 SourceNode.prototype.setSourceContent =
michael@0 1686 function SourceNode_setSourceContent(aSourceFile, aSourceContent) {
michael@0 1687 this.sourceContents[util.toSetString(aSourceFile)] = aSourceContent;
michael@0 1688 };
michael@0 1689
michael@0 1690 /**
michael@0 1691 * Walk over the tree of SourceNodes. The walking function is called for each
michael@0 1692 * source file content and is passed the filename and source content.
michael@0 1693 *
michael@0 1694 * @param aFn The traversal function.
michael@0 1695 */
michael@0 1696 SourceNode.prototype.walkSourceContents =
michael@0 1697 function SourceNode_walkSourceContents(aFn) {
michael@0 1698 for (var i = 0, len = this.children.length; i < len; i++) {
michael@0 1699 if (this.children[i] instanceof SourceNode) {
michael@0 1700 this.children[i].walkSourceContents(aFn);
michael@0 1701 }
michael@0 1702 }
michael@0 1703
michael@0 1704 var sources = Object.keys(this.sourceContents);
michael@0 1705 for (var i = 0, len = sources.length; i < len; i++) {
michael@0 1706 aFn(util.fromSetString(sources[i]), this.sourceContents[sources[i]]);
michael@0 1707 }
michael@0 1708 };
michael@0 1709
michael@0 1710 /**
michael@0 1711 * Return the string representation of this source node. Walks over the tree
michael@0 1712 * and concatenates all the various snippets together to one string.
michael@0 1713 */
michael@0 1714 SourceNode.prototype.toString = function SourceNode_toString() {
michael@0 1715 var str = "";
michael@0 1716 this.walk(function (chunk) {
michael@0 1717 str += chunk;
michael@0 1718 });
michael@0 1719 return str;
michael@0 1720 };
michael@0 1721
michael@0 1722 /**
michael@0 1723 * Returns the string representation of this source node along with a source
michael@0 1724 * map.
michael@0 1725 */
michael@0 1726 SourceNode.prototype.toStringWithSourceMap = function SourceNode_toStringWithSourceMap(aArgs) {
michael@0 1727 var generated = {
michael@0 1728 code: "",
michael@0 1729 line: 1,
michael@0 1730 column: 0
michael@0 1731 };
michael@0 1732 var map = new SourceMapGenerator(aArgs);
michael@0 1733 var sourceMappingActive = false;
michael@0 1734 var lastOriginalSource = null;
michael@0 1735 var lastOriginalLine = null;
michael@0 1736 var lastOriginalColumn = null;
michael@0 1737 var lastOriginalName = null;
michael@0 1738 this.walk(function (chunk, original) {
michael@0 1739 generated.code += chunk;
michael@0 1740 if (original.source !== null
michael@0 1741 && original.line !== null
michael@0 1742 && original.column !== null) {
michael@0 1743 if(lastOriginalSource !== original.source
michael@0 1744 || lastOriginalLine !== original.line
michael@0 1745 || lastOriginalColumn !== original.column
michael@0 1746 || lastOriginalName !== original.name) {
michael@0 1747 map.addMapping({
michael@0 1748 source: original.source,
michael@0 1749 original: {
michael@0 1750 line: original.line,
michael@0 1751 column: original.column
michael@0 1752 },
michael@0 1753 generated: {
michael@0 1754 line: generated.line,
michael@0 1755 column: generated.column
michael@0 1756 },
michael@0 1757 name: original.name
michael@0 1758 });
michael@0 1759 }
michael@0 1760 lastOriginalSource = original.source;
michael@0 1761 lastOriginalLine = original.line;
michael@0 1762 lastOriginalColumn = original.column;
michael@0 1763 lastOriginalName = original.name;
michael@0 1764 sourceMappingActive = true;
michael@0 1765 } else if (sourceMappingActive) {
michael@0 1766 map.addMapping({
michael@0 1767 generated: {
michael@0 1768 line: generated.line,
michael@0 1769 column: generated.column
michael@0 1770 }
michael@0 1771 });
michael@0 1772 lastOriginalSource = null;
michael@0 1773 sourceMappingActive = false;
michael@0 1774 }
michael@0 1775 chunk.split('').forEach(function (ch) {
michael@0 1776 if (ch === '\n') {
michael@0 1777 generated.line++;
michael@0 1778 generated.column = 0;
michael@0 1779 } else {
michael@0 1780 generated.column++;
michael@0 1781 }
michael@0 1782 });
michael@0 1783 });
michael@0 1784 this.walkSourceContents(function (sourceFile, sourceContent) {
michael@0 1785 map.setSourceContent(sourceFile, sourceContent);
michael@0 1786 });
michael@0 1787
michael@0 1788 return { code: generated.code, map: map };
michael@0 1789 };
michael@0 1790
michael@0 1791 exports.SourceNode = SourceNode;
michael@0 1792
michael@0 1793 });
michael@0 1794 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 1795 ///////////////////////////////////////////////////////////////////////////////
michael@0 1796
michael@0 1797 this.SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;
michael@0 1798 this.SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
michael@0 1799 this.SourceNode = require('source-map/source-node').SourceNode;

mercurial