summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/url_utility.js
blob: f16ff188edbaace03c0a2911f0b24ff9fdd7effb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
export const DASH_SCOPE = '-';

export const PATH_SEPARATOR = '/';
const PATH_SEPARATOR_LEADING_REGEX = new RegExp(`^${PATH_SEPARATOR}+`);
const PATH_SEPARATOR_ENDING_REGEX = new RegExp(`${PATH_SEPARATOR}+$`);
const SHA_REGEX = /[\da-f]{40}/gi;

// GitLab default domain (override in jh)
export const DOMAIN = 'gitlab.com';

// About GitLab default host (overwrite in jh)
export const PROMO_HOST = `about.${DOMAIN}`; // about.gitlab.com

// About Gitlab default url (overwrite in jh)
export const PROMO_URL = `https://${PROMO_HOST}`;

// Reset the cursor in a Regex so that multiple uses before a recompile don't fail
function resetRegExp(regex) {
  regex.lastIndex = 0; /* eslint-disable-line no-param-reassign */

  return regex;
}

/**
 * Returns the absolute pathname for a relative or absolute URL string.
 *
 * A few examples of inputs and outputs:
 * 1) 'http://a.com/b/c/d' => '/b/c/d'
 * 2) '/b/c/d' => '/b/c/d'
 * 3) 'b/c/d' => '/b/c/d' or '[path]/b/c/d' depending of the current path of the
 *    document.location
 */
export const parseUrlPathname = (url) => {
  const { pathname } = new URL(url, document.location.href);
  return pathname;
};

// Returns a decoded url parameter value
// - Treats '+' as '%20'
function decodeUrlParameter(val) {
  return decodeURIComponent(val.replace(/\+/g, '%20'));
}

/**
 * Safely encodes a string to be used as a path
 *
 * Note: This function DOES encode typical URL parts like ?, =, &, #, and +
 * If you need to use search parameters or URL fragments, they should be
 *     added AFTER calling this function, not before.
 *
 * Usecase: An image filename is stored verbatim, and you need to load it in
 *     the browser.
 *
 * Example: /some_path/file #1.jpg      ==> /some_path/file%20%231.jpg
 * Example: /some-path/file! Final!.jpg ==> /some-path/file%21%20Final%21.jpg
 *
 * Essentially, if a character *could* present a problem in a URL, it's escaped
 *     to the hexadecimal representation instead. This means it's a bit more
 *     aggressive than encodeURIComponent: that built-in function doesn't
 *     encode some characters that *could* be problematic, so this function
 *     adds them (#, !, ~, *, ', (, and )).
 *     Additionally, encodeURIComponent *does* encode `/`, but we want safer
 *     URLs, not non-functional URLs, so this function DEcodes slashes ('%2F').
 *
 * @param {String} potentiallyUnsafePath
 * @returns {String}
 */
export function encodeSaferUrl(potentiallyUnsafePath) {
  const unencode = ['%2F'];
  const encode = ['#', '!', '~', '\\*', "'", '\\(', '\\)'];
  let saferPath = encodeURIComponent(potentiallyUnsafePath);

  unencode.forEach((code) => {
    saferPath = saferPath.replace(new RegExp(code, 'g'), decodeURIComponent(code));
  });
  encode.forEach((code) => {
    const encodedValue = code
      .codePointAt(code.length - 1)
      .toString(16)
      .toUpperCase();

    saferPath = saferPath.replace(new RegExp(code, 'g'), `%${encodedValue}`);
  });

  return saferPath;
}

export function cleanLeadingSeparator(path) {
  return path.replace(PATH_SEPARATOR_LEADING_REGEX, '');
}

export function cleanEndingSeparator(path) {
  return path.replace(PATH_SEPARATOR_ENDING_REGEX, '');
}

/**
 * Safely joins the given paths which might both start and end with a `/`
 *
 * Example:
 * - `joinPaths('abc/', '/def') === 'abc/def'`
 * - `joinPaths(null, 'abc/def', 'zoo) === 'abc/def/zoo'`
 *
 * @param  {...String} paths
 * @returns {String}
 */
export function joinPaths(...paths) {
  return paths.reduce((acc, path) => {
    if (!path) {
      return acc;
    }
    if (!acc) {
      return path;
    }

    return [cleanEndingSeparator(acc), PATH_SEPARATOR, cleanLeadingSeparator(path)].join('');
  }, '');
}

// Returns an array containing the value(s) of the
// of the key passed as an argument
export function getParameterValues(sParam, url = window.location) {
  const sPageURL = decodeURIComponent(new URL(url).search.substring(1));

  return sPageURL.split('&').reduce((acc, urlParam) => {
    const sParameterName = urlParam.split('=');

    if (sParameterName[0] === sParam) {
      acc.push(sParameterName[1].replace(/\+/g, ' '));
    }

    return acc;
  }, []);
}

/**
 * Merges a URL to a set of params replacing value for
 * those already present.
 *
 * Also removes `null` param values from the resulting URL.
 *
 * @param {Object} params - url keys and value to merge
 * @param {String} url
 * @param {Object} options
 * @param {Boolean} options.spreadArrays - split array values into separate key/value-pairs
 * @param {Boolean} options.sort - alphabetically sort params in the returned url (in asc order, i.e., a-z)
 */
export function mergeUrlParams(params, url, options = {}) {
  const { spreadArrays = false, sort = false } = options;
  const re = /^([^?#]*)(\?[^#]*)?(.*)/;
  let merged = {};
  const [, fullpath, query, fragment] = url.match(re);

  if (query) {
    merged = query
      .substr(1)
      .split('&')
      .reduce((memo, part) => {
        if (part.length) {
          const kv = part.split('=');
          let key = decodeUrlParameter(kv[0]);
          const value = decodeUrlParameter(kv.slice(1).join('='));
          if (spreadArrays && key.endsWith('[]')) {
            key = key.slice(0, -2);
            if (!Array.isArray(memo[key])) {
              return { ...memo, [key]: [value] };
            }
            memo[key].push(value);

            return memo;
          }

          return { ...memo, [key]: value };
        }

        return memo;
      }, {});
  }

  Object.assign(merged, params);

  const mergedKeys = sort ? Object.keys(merged).sort() : Object.keys(merged);

  const newQuery = mergedKeys
    .filter((key) => merged[key] !== null && merged[key] !== undefined)
    .map((key) => {
      let value = merged[key];
      const encodedKey = encodeURIComponent(key);
      if (spreadArrays && Array.isArray(value)) {
        value = merged[key]
          .map((arrayValue) => encodeURIComponent(arrayValue))
          .join(`&${encodedKey}[]=`);
        return `${encodedKey}[]=${value}`;
      }
      return `${encodedKey}=${encodeURIComponent(value)}`;
    })
    .join('&');

  if (newQuery) {
    return `${fullpath}?${newQuery}${fragment}`;
  }
  return `${fullpath}${fragment}`;
}

/**
 * Removes specified query params from the url by returning a new url string that no longer
 * includes the param/value pair. If no url is provided, `window.location.href` is used as
 * the default value.
 *
 * @param {string[]} params - the query param names to remove
 * @param {string} [url=windowLocation().href] - url from which the query param will be removed
 * @param {boolean} skipEncoding - set to true when the url does not require encoding
 * @returns {string} A copy of the original url but without the query param
 */
export function removeParams(params, url = window.location.href, skipEncoding = false) {
  const [rootAndQuery, fragment] = url.split('#');
  const [root, query] = rootAndQuery.split('?');

  if (!query) {
    return url;
  }

  const removableParams = skipEncoding ? params : params.map((param) => encodeURIComponent(param));

  const updatedQuery = query
    .split('&')
    .filter((paramPair) => {
      const [foundParam] = paramPair.split('=');
      return removableParams.indexOf(foundParam) < 0;
    })
    .join('&');

  const writableQuery = updatedQuery.length > 0 ? `?${updatedQuery}` : '';
  const writableFragment = fragment ? `#${fragment}` : '';
  return `${root}${writableQuery}${writableFragment}`;
}

export const getLocationHash = (hash = window.location.hash) => hash.split('#')[1];

/**
 * Returns a boolean indicating whether the URL hash contains the given string value
 */
export function doesHashExistInUrl(hashName) {
  const hash = getLocationHash();
  return hash && hash.includes(hashName);
}

export function urlContainsSha({ url = String(window.location) } = {}) {
  return resetRegExp(SHA_REGEX).test(url);
}

export function getShaFromUrl({ url = String(window.location) } = {}) {
  let sha = null;

  if (urlContainsSha({ url })) {
    [sha] = url.match(resetRegExp(SHA_REGEX));
  }

  return sha;
}

/**
 * Apply the fragment to the given url by returning a new url string that includes
 * the fragment. If the given url already contains a fragment, the original fragment
 * will be removed.
 *
 * @param {string} url - url to which the fragment will be applied
 * @param {string} fragment - fragment to append
 */
export const setUrlFragment = (url, fragment) => {
  const [rootUrl] = url.split('#');
  const encodedFragment = encodeURIComponent(fragment.replace(/^#/, ''));
  return `${rootUrl}#${encodedFragment}`;
};

/**
 * Navigates to a URL
 * @param {*} url - url to navigate to
 * @param {*} external - if true, open a new page or tab
 */
export function visitUrl(url, external = false) {
  if (external) {
    // Simulate `target="_blank" rel="noopener noreferrer"`
    // See https://mathiasbynens.github.io/rel-noopener/
    const otherWindow = window.open();
    otherWindow.opener = null;
    otherWindow.location = url;
  } else {
    window.location.href = url;
  }
}

export function refreshCurrentPage() {
  visitUrl(window.location.href);
}

/**
 * Navigates to a URL
 * @deprecated Use visitUrl from ~/lib/utils/url_utility.js instead
 * @param {*} url
 */
export function redirectTo(url) {
  return window.location.assign(url);
}

export function updateHistory({ state = {}, title = '', url, replace = false, win = window } = {}) {
  if (win.history) {
    if (replace) {
      win.history.replaceState(state, title, url);
    } else {
      win.history.pushState(state, title, url);
    }
  }
}

export const escapeFileUrl = (fileUrl) => encodeURIComponent(fileUrl).replace(/%2F/g, '/');

export function webIDEUrl(route = undefined) {
  let returnUrl = `${gon.relative_url_root || ''}/-/ide/`;
  if (route) {
    returnUrl += `project${route.replace(new RegExp(`^${gon.relative_url_root || ''}`), '')}`;
  }
  return escapeFileUrl(returnUrl);
}

/**
 * Returns current base URL
 */
export function getBaseURL() {
  const { protocol, host } = window.location;
  return `${protocol}//${host}`;
}

/**
 * Returns true if url is an absolute URL
 *
 * @param {String} url
 */
export function isAbsolute(url) {
  return /^https?:\/\//.test(url);
}

/**
 * Returns true if url is a root-relative URL
 *
 * @param {String} url
 */
export function isRootRelative(url) {
  return /^\/(?!\/)/.test(url);
}

/**
 * Returns true if url is a base64 data URL
 *
 * @param {String} url
 */
export function isBase64DataUrl(url) {
  return /^data:[.\w+-]+\/[.\w+-]+;base64,/.test(url);
}

/**
 * Returns true if url is a blob: type url
 *
 * @param {String} url
 */
export function isBlobUrl(url) {
  return /^blob:/.test(url);
}

/**
 * Returns true if url is an absolute or root-relative URL
 *
 * @param {String} url
 */
export function isAbsoluteOrRootRelative(url) {
  return isAbsolute(url) || isRootRelative(url);
}

/**
 * Returns true if url is an external URL
 *
 * @param {String} url
 * @returns {Boolean}
 */
export function isExternal(url) {
  if (isRootRelative(url)) {
    return false;
  }

  return !url.includes(gon.gitlab_url);
}

/**
 * Converts a relative path to an absolute or a root relative path depending
 * on what is passed as a basePath.
 *
 * @param {String} path       Relative path, eg. ../img/img.png
 * @param {String} basePath   Absolute or root relative path, eg. /user/project or
 *                            https://gitlab.com/user/project
 */
export function relativePathToAbsolute(path, basePath) {
  const absolute = isAbsolute(basePath);
  const base = absolute ? basePath : `file:///${basePath}`;
  const url = new URL(path, base);
  url.pathname = url.pathname.replace(/\/\/+/g, '/');
  return absolute ? url.href : decodeURIComponent(url.pathname);
}

/**
 * Checks if the provided URL is a safe URL (absolute http(s) or root-relative URL)
 *
 * @param {String} url that will be checked
 * @returns {Boolean}
 */
export function isSafeURL(url) {
  if (!isAbsoluteOrRootRelative(url)) {
    return false;
  }

  try {
    const parsedUrl = new URL(url, getBaseURL());
    return ['http:', 'https:'].includes(parsedUrl.protocol);
  } catch (e) {
    return false;
  }
}

/**
 * Returns the sanitized url when not safe
 *
 * @param {String} url
 * @returns {String}
 */
export function sanitizeUrl(url) {
  if (!isSafeURL(url)) {
    return 'about:blank';
  }
  return url;
}

/**
 * Returns a normalized url
 *
 * https://gitlab.com/foo/../baz => https://gitlab.com/baz
 *
 * @param {String} url - URL to be transformed
 * @param {String?} baseUrl - current base URL
 * @returns {String}
 */
export const getNormalizedURL = (url, baseUrl) => {
  const base = baseUrl || getBaseURL();
  try {
    return new URL(url, base).href;
  } catch (e) {
    return '';
  }
};

export function getWebSocketProtocol() {
  return window.location.protocol.replace('http', 'ws');
}

export function getWebSocketUrl(path) {
  return `${getWebSocketProtocol()}//${joinPaths(window.location.host, path)}`;
}

const splitPath = (path = '') => path.replace(/^\?/, '').split('&');

export const urlParamsToArray = (path = '') =>
  splitPath(path)
    .filter((param) => param.length > 0)
    .map((param) => {
      const split = param.split('=');
      return [decodeURI(split[0]), split[1]].join('=');
    });

export const getUrlParamsArray = () => urlParamsToArray(window.location.search);

/**
 * Convert search query into an object
 *
 * @param {String} query from "document.location.search"
 * @param {Object} options
 * @param {Boolean?} options.gatherArrays - gather array values into an Array
 * @param {Boolean?} options.legacySpacesDecode - (deprecated) plus symbols (+) are not replaced with spaces, false by default
 * @returns {Object}
 *
 * ex: "?one=1&two=2" into {one: 1, two: 2}
 */
export function queryToObject(query, { gatherArrays = false, legacySpacesDecode = false } = {}) {
  const removeQuestionMarkFromQuery = String(query).startsWith('?') ? query.slice(1) : query;
  return removeQuestionMarkFromQuery.split('&').reduce((accumulator, curr) => {
    const [key, value] = curr.split('=');
    if (value === undefined) {
      return accumulator;
    }

    const decodedValue = legacySpacesDecode ? decodeURIComponent(value) : decodeUrlParameter(value);
    const decodedKey = legacySpacesDecode ? decodeURIComponent(key) : decodeUrlParameter(key);

    if (gatherArrays && decodedKey.endsWith('[]')) {
      const decodedArrayKey = decodedKey.slice(0, -2);

      if (!Array.isArray(accumulator[decodedArrayKey])) {
        accumulator[decodedArrayKey] = [];
      }

      accumulator[decodedArrayKey].push(decodedValue);
    } else {
      accumulator[decodedKey] = decodedValue;
    }

    return accumulator;
  }, {});
}

/**
 * This function accepts the `name` of the param to parse in the url
 * if the name does not exist this function will return `null`
 * otherwise it will return the value of the param key provided
 *
 * @param {String} name
 * @param {String?} urlToParse
 * @returns value of the parameter as string
 */
export const getParameterByName = (name, query = window.location.search) => {
  return queryToObject(query)[name] || null;
};

/**
 * Convert search query object back into a search query
 *
 * @param {Object?} params that needs to be converted
 * @returns {String}
 *
 * ex: {one: 1, two: 2} into "one=1&two=2"
 *
 */
export function objectToQuery(params = {}) {
  return Object.keys(params)
    .map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
    .join('&');
}

/**
 * Sets query params for a given URL
 * It adds new query params, updates existing params with a new value and removes params with value null/undefined
 *
 * @param {Object} params The query params to be set/updated
 * @param {String} url The url to be operated on
 * @param {Boolean} clearParams Indicates whether existing query params should be removed or not
 * @param {Boolean} railsArraySyntax When enabled, changes the array syntax from `keys=` to `keys[]=` according to Rails conventions
 * @returns {String} A copy of the original with the updated query params
 */
export const setUrlParams = (
  params,
  url = window.location.href,
  clearParams = false,
  railsArraySyntax = false,
  decodeParams = false,
) => {
  const urlObj = new URL(url);
  const queryString = urlObj.search;
  const searchParams = clearParams ? new URLSearchParams('') : new URLSearchParams(queryString);

  Object.keys(params).forEach((key) => {
    if (params[key] === null || params[key] === undefined) {
      searchParams.delete(key);
    } else if (Array.isArray(params[key])) {
      const keyName = railsArraySyntax ? `${key}[]` : key;
      if (params[key].length === 0) {
        searchParams.delete(keyName);
      } else {
        params[key].forEach((val, idx) => {
          if (idx === 0) {
            searchParams.set(keyName, val);
          } else {
            searchParams.append(keyName, val);
          }
        });
      }
    } else {
      searchParams.set(key, params[key]);
    }
  });

  urlObj.search = decodeParams
    ? decodeURIComponent(searchParams.toString())
    : searchParams.toString();

  return urlObj.toString();
};

export function urlIsDifferent(url, compare = String(window.location)) {
  return url !== compare;
}

export function getHTTPProtocol(url) {
  if (!url) {
    return window.location.protocol.slice(0, -1);
  }
  const protocol = url.split(':');
  return protocol.length > 1 ? protocol[0] : undefined;
}

/**
 * Strips the filename from the given path by removing every non-slash character from the end of the
 * passed parameter.
 * @param {string} path
 */
export function stripPathTail(path = '') {
  return path.replace(/[^/]+$/, '');
}

export function getURLOrigin(url) {
  if (!url) {
    return window.location.origin;
  }

  try {
    return new URL(url).origin;
  } catch (e) {
    return null;
  }
}

/**
 * Returns `true` if the given `url` resolves to the same origin the page is served
 * from; otherwise, returns `false`.
 *
 * The `url` may be absolute or relative.
 *
 * @param {string} url The URL to check.
 * @returns {boolean}
 */
export function isSameOriginUrl(url) {
  if (typeof url !== 'string') {
    return false;
  }

  const { origin } = window.location;

  try {
    return new URL(url, origin).origin === origin;
  } catch {
    // Invalid URLs cannot have the same origin
    return false;
  }
}

/**
 * Returns a URL to WebIDE considering the current user's position in
 * repository's tree. If not MR `iid` has been passed, the URL is fetched
 * from the global `gl.webIDEPath`.
 *
 * @param sourceProjectFullPath Source project's full path. Used in MRs
 * @param targetProjectFullPath Target project's full path. Used in MRs
 * @param iid                   MR iid
 * @returns {string}
 */

export function constructWebIDEPath({
  sourceProjectFullPath,
  targetProjectFullPath = '',
  iid,
} = {}) {
  if (!iid || !sourceProjectFullPath) {
    return window.gl?.webIDEPath;
  }
  return mergeUrlParams(
    {
      target_project: sourceProjectFullPath !== targetProjectFullPath ? targetProjectFullPath : '',
    },
    webIDEUrl(`/${sourceProjectFullPath}/merge_requests/${iid}`),
  );
}

/**
 * Examples
 *
 * http://gitlab.com => gitlab.com
 * https://gitlab.com => gitlab.com
 *
 * @param {String} url
 * @returns A url without a protocol / scheme
 */
export const removeUrlProtocol = (url) => url.replace(/^\w+:\/?\/?/, '');

/**
 * Examples
 *
 * https://www.gitlab.com/path/ => https://www.gitlab.com/path
 * https://www.gitlab.com/?query=search => https://www.gitlab.com?query=search
 * https://www.gitlab.com/#fragment => https://www.gitlab.com#fragment
 *
 * @param {String} url
 * @returns A URL that does not have a path that ends with slash
 */
export const removeLastSlashInUrlPath = (url) =>
  url.replace(/\/$/, '').replace(/\/(\?|#){1}([^/]*)$/, '$1$2');