Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / source-map / lib / source-map / util.js
1 /* -*- Mode: js; js-indent-level: 2; -*- */
2 /*
3  * Copyright 2011 Mozilla Foundation and contributors
4  * Licensed under the New BSD license. See LICENSE or:
5  * http://opensource.org/licenses/BSD-3-Clause
6  */
7 if (typeof define !== 'function') {
8     var define = require('amdefine')(module, require);
9 }
10 define(function (require, exports, module) {
11
12   /**
13    * This is a helper function for getting values from parameter/options
14    * objects.
15    *
16    * @param args The object we are extracting values from
17    * @param name The name of the property we are getting.
18    * @param defaultValue An optional value to return if the property is missing
19    * from the object. If this is not specified and the property is missing, an
20    * error will be thrown.
21    */
22   function getArg(aArgs, aName, aDefaultValue) {
23     if (aName in aArgs) {
24       return aArgs[aName];
25     } else if (arguments.length === 3) {
26       return aDefaultValue;
27     } else {
28       throw new Error('"' + aName + '" is a required argument.');
29     }
30   }
31   exports.getArg = getArg;
32
33   var urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.]*)(?::(\d+))?(\S*)$/;
34   var dataUrlRegexp = /^data:.+\,.+$/;
35
36   function urlParse(aUrl) {
37     var match = aUrl.match(urlRegexp);
38     if (!match) {
39       return null;
40     }
41     return {
42       scheme: match[1],
43       auth: match[2],
44       host: match[3],
45       port: match[4],
46       path: match[5]
47     };
48   }
49   exports.urlParse = urlParse;
50
51   function urlGenerate(aParsedUrl) {
52     var url = '';
53     if (aParsedUrl.scheme) {
54       url += aParsedUrl.scheme + ':';
55     }
56     url += '//';
57     if (aParsedUrl.auth) {
58       url += aParsedUrl.auth + '@';
59     }
60     if (aParsedUrl.host) {
61       url += aParsedUrl.host;
62     }
63     if (aParsedUrl.port) {
64       url += ":" + aParsedUrl.port
65     }
66     if (aParsedUrl.path) {
67       url += aParsedUrl.path;
68     }
69     return url;
70   }
71   exports.urlGenerate = urlGenerate;
72
73   /**
74    * Normalizes a path, or the path portion of a URL:
75    *
76    * - Replaces consequtive slashes with one slash.
77    * - Removes unnecessary '.' parts.
78    * - Removes unnecessary '<dir>/..' parts.
79    *
80    * Based on code in the Node.js 'path' core module.
81    *
82    * @param aPath The path or url to normalize.
83    */
84   function normalize(aPath) {
85     var path = aPath;
86     var url = urlParse(aPath);
87     if (url) {
88       if (!url.path) {
89         return aPath;
90       }
91       path = url.path;
92     }
93     var isAbsolute = (path.charAt(0) === '/');
94
95     var parts = path.split(/\/+/);
96     for (var part, up = 0, i = parts.length - 1; i >= 0; i--) {
97       part = parts[i];
98       if (part === '.') {
99         parts.splice(i, 1);
100       } else if (part === '..') {
101         up++;
102       } else if (up > 0) {
103         if (part === '') {
104           // The first part is blank if the path is absolute. Trying to go
105           // above the root is a no-op. Therefore we can remove all '..' parts
106           // directly after the root.
107           parts.splice(i + 1, up);
108           up = 0;
109         } else {
110           parts.splice(i, 2);
111           up--;
112         }
113       }
114     }
115     path = parts.join('/');
116
117     if (path === '') {
118       path = isAbsolute ? '/' : '.';
119     }
120
121     if (url) {
122       url.path = path;
123       return urlGenerate(url);
124     }
125     return path;
126   }
127   exports.normalize = normalize;
128
129   /**
130    * Joins two paths/URLs.
131    *
132    * @param aRoot The root path or URL.
133    * @param aPath The path or URL to be joined with the root.
134    *
135    * - If aPath is a URL or a data URI, aPath is returned, unless aPath is a
136    *   scheme-relative URL: Then the scheme of aRoot, if any, is prepended
137    *   first.
138    * - Otherwise aPath is a path. If aRoot is a URL, then its path portion
139    *   is updated with the result and aRoot is returned. Otherwise the result
140    *   is returned.
141    *   - If aPath is absolute, the result is aPath.
142    *   - Otherwise the two paths are joined with a slash.
143    * - Joining for example 'http://' and 'www.example.com' is also supported.
144    */
145   function join(aRoot, aPath) {
146     if (aRoot === "") {
147       aRoot = ".";
148     }
149     if (aPath === "") {
150       aPath = ".";
151     }
152     var aPathUrl = urlParse(aPath);
153     var aRootUrl = urlParse(aRoot);
154     if (aRootUrl) {
155       aRoot = aRootUrl.path || '/';
156     }
157
158     // `join(foo, '//www.example.org')`
159     if (aPathUrl && !aPathUrl.scheme) {
160       if (aRootUrl) {
161         aPathUrl.scheme = aRootUrl.scheme;
162       }
163       return urlGenerate(aPathUrl);
164     }
165
166     if (aPathUrl || aPath.match(dataUrlRegexp)) {
167       return aPath;
168     }
169
170     // `join('http://', 'www.example.com')`
171     if (aRootUrl && !aRootUrl.host && !aRootUrl.path) {
172       aRootUrl.host = aPath;
173       return urlGenerate(aRootUrl);
174     }
175
176     var joined = aPath.charAt(0) === '/'
177       ? aPath
178       : normalize(aRoot.replace(/\/+$/, '') + '/' + aPath);
179
180     if (aRootUrl) {
181       aRootUrl.path = joined;
182       return urlGenerate(aRootUrl);
183     }
184     return joined;
185   }
186   exports.join = join;
187
188   /**
189    * Make a path relative to a URL or another path.
190    *
191    * @param aRoot The root path or URL.
192    * @param aPath The path or URL to be made relative to aRoot.
193    */
194   function relative(aRoot, aPath) {
195     if (aRoot === "") {
196       aRoot = ".";
197     }
198
199     aRoot = aRoot.replace(/\/$/, '');
200
201     // It is possible for the path to be above the root. In this case, simply
202     // checking whether the root is a prefix of the path won't work. Instead, we
203     // need to remove components from the root one by one, until either we find
204     // a prefix that fits, or we run out of components to remove.
205     var level = 0;
206     while (aPath.indexOf(aRoot + '/') !== 0) {
207       var index = aRoot.lastIndexOf("/");
208       if (index < 0) {
209         return aPath;
210       }
211
212       // If the only part of the root that is left is the scheme (i.e. http://,
213       // file:///, etc.), one or more slashes (/), or simply nothing at all, we
214       // have exhausted all components, so the path is not relative to the root.
215       aRoot = aRoot.slice(0, index);
216       if (aRoot.match(/^([^\/]+:\/)?\/*$/)) {
217         return aPath;
218       }
219
220       ++level;
221     }
222
223     // Make sure we add a "../" for each component we removed from the root.
224     return Array(level + 1).join("../") + aPath.substr(aRoot.length + 1);
225   }
226   exports.relative = relative;
227
228   /**
229    * Because behavior goes wacky when you set `__proto__` on objects, we
230    * have to prefix all the strings in our set with an arbitrary character.
231    *
232    * See https://github.com/mozilla/source-map/pull/31 and
233    * https://github.com/mozilla/source-map/issues/30
234    *
235    * @param String aStr
236    */
237   function toSetString(aStr) {
238     return '$' + aStr;
239   }
240   exports.toSetString = toSetString;
241
242   function fromSetString(aStr) {
243     return aStr.substr(1);
244   }
245   exports.fromSetString = fromSetString;
246
247   /**
248    * Comparator between two mappings where the original positions are compared.
249    *
250    * Optionally pass in `true` as `onlyCompareGenerated` to consider two
251    * mappings with the same original source/line/column, but different generated
252    * line and column the same. Useful when searching for a mapping with a
253    * stubbed out mapping.
254    */
255   function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {
256     var cmp = mappingA.source - mappingB.source;
257     if (cmp !== 0) {
258       return cmp;
259     }
260
261     cmp = mappingA.originalLine - mappingB.originalLine;
262     if (cmp !== 0) {
263       return cmp;
264     }
265
266     cmp = mappingA.originalColumn - mappingB.originalColumn;
267     if (cmp !== 0 || onlyCompareOriginal) {
268       return cmp;
269     }
270
271     cmp = mappingA.generatedColumn - mappingB.generatedColumn;
272     if (cmp !== 0) {
273       return cmp;
274     }
275
276     cmp = mappingA.generatedLine - mappingB.generatedLine;
277     if (cmp !== 0) {
278       return cmp;
279     }
280
281     return mappingA.name - mappingB.name;
282   };
283   exports.compareByOriginalPositions = compareByOriginalPositions;
284
285   /**
286    * Comparator between two mappings with deflated source and name indices where
287    * the generated positions are compared.
288    *
289    * Optionally pass in `true` as `onlyCompareGenerated` to consider two
290    * mappings with the same generated line and column, but different
291    * source/name/original line and column the same. Useful when searching for a
292    * mapping with a stubbed out mapping.
293    */
294   function compareByGeneratedPositionsDeflated(mappingA, mappingB, onlyCompareGenerated) {
295     var cmp = mappingA.generatedLine - mappingB.generatedLine;
296     if (cmp !== 0) {
297       return cmp;
298     }
299
300     cmp = mappingA.generatedColumn - mappingB.generatedColumn;
301     if (cmp !== 0 || onlyCompareGenerated) {
302       return cmp;
303     }
304
305     cmp = mappingA.source - mappingB.source;
306     if (cmp !== 0) {
307       return cmp;
308     }
309
310     cmp = mappingA.originalLine - mappingB.originalLine;
311     if (cmp !== 0) {
312       return cmp;
313     }
314
315     cmp = mappingA.originalColumn - mappingB.originalColumn;
316     if (cmp !== 0) {
317       return cmp;
318     }
319
320     return mappingA.name - mappingB.name;
321   };
322   exports.compareByGeneratedPositionsDeflated = compareByGeneratedPositionsDeflated;
323
324   function strcmp(aStr1, aStr2) {
325     if (aStr1 === aStr2) {
326       return 0;
327     }
328
329     if (aStr1 > aStr2) {
330       return 1;
331     }
332
333     return -1;
334   }
335
336   /**
337    * Comparator between two mappings with inflated source and name strings where
338    * the generated positions are compared.
339    */
340   function compareByGeneratedPositionsInflated(mappingA, mappingB) {
341     var cmp = mappingA.generatedLine - mappingB.generatedLine;
342     if (cmp !== 0) {
343       return cmp;
344     }
345
346     cmp = mappingA.generatedColumn - mappingB.generatedColumn;
347     if (cmp !== 0) {
348       return cmp;
349     }
350
351     cmp = strcmp(mappingA.source, mappingB.source);
352     if (cmp !== 0) {
353       return cmp;
354     }
355
356     cmp = mappingA.originalLine - mappingB.originalLine;
357     if (cmp !== 0) {
358       return cmp;
359     }
360
361     cmp = mappingA.originalColumn - mappingB.originalColumn;
362     if (cmp !== 0) {
363       return cmp;
364     }
365
366     return strcmp(mappingA.name, mappingB.name);
367   };
368   exports.compareByGeneratedPositionsInflated = compareByGeneratedPositionsInflated;
369
370 });