debian-mirror-gitlab/app/assets/javascripts/lib/utils/url_utility.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

696 lines
19 KiB
JavaScript
Raw Normal View History

2021-11-18 22:05:49 +05:30
export const DASH_SCOPE = '-';
2022-01-26 12:08:38 +05:30
export const PATH_SEPARATOR = '/';
2020-01-01 13:55:28 +05:30
const PATH_SEPARATOR_LEADING_REGEX = new RegExp(`^${PATH_SEPARATOR}+`);
const PATH_SEPARATOR_ENDING_REGEX = new RegExp(`${PATH_SEPARATOR}+$`);
2020-03-13 15:44:24 +05:30
const SHA_REGEX = /[\da-f]{40}/gi;
2021-12-11 22:18:48 +05:30
// About GitLab default host (overwrite in jh)
export const PROMO_HOST = 'about.gitlab.com';
// About Gitlab default url (overwrite in jh)
export const PROMO_URL = `https://${PROMO_HOST}`;
2020-03-13 15:44:24 +05:30
// 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;
}
2019-09-04 21:01:54 +05:30
2022-05-07 20:08:51 +05:30
/**
* 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;
};
2019-12-04 20:38:33 +05:30
// Returns a decoded url parameter value
// - Treats '+' as '%20'
function decodeUrlParameter(val) {
return decodeURIComponent(val.replace(/\+/g, '%20'));
}
2021-03-11 19:13:27 +05:30
/**
* 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;
}
2021-01-03 14:25:43 +05:30
export function cleanLeadingSeparator(path) {
2020-01-01 13:55:28 +05:30
return path.replace(PATH_SEPARATOR_LEADING_REGEX, '');
}
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('');
}, '');
}
2017-08-17 22:00:37 +05:30
// Returns an array containing the value(s) of the
// of the key passed as an argument
2019-10-12 21:52:04 +05:30
export function getParameterValues(sParam, url = window.location) {
const sPageURL = decodeURIComponent(new URL(url).search.substring(1));
2018-03-17 18:26:18 +05:30
return sPageURL.split('&').reduce((acc, urlParam) => {
const sParameterName = urlParam.split('=');
2017-08-17 22:00:37 +05:30
if (sParameterName[0] === sParam) {
2018-03-17 18:26:18 +05:30
acc.push(sParameterName[1].replace(/\+/g, ' '));
2016-09-13 17:45:13 +05:30
}
2018-03-17 18:26:18 +05:30
return acc;
}, []);
}
2020-05-24 23:13:21 +05:30
/**
* 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
2020-10-24 23:57:45 +05:30
* @param {Object} options
* @param {Boolean} options.spreadArrays - split array values into separate key/value-pairs
2021-01-03 14:25:43 +05:30
* @param {Boolean} options.sort - alphabetically sort params in the returned url (in asc order, i.e., a-z)
2020-05-24 23:13:21 +05:30
*/
2020-10-24 23:57:45 +05:30
export function mergeUrlParams(params, url, options = {}) {
2020-11-24 15:15:51 +05:30
const { spreadArrays = false, sort = false } = options;
2019-02-15 15:39:39 +05:30
const re = /^([^?#]*)(\?[^#]*)?(.*)/;
2020-10-24 23:57:45 +05:30
let merged = {};
2020-05-24 23:13:21 +05:30
const [, fullpath, query, fragment] = url.match(re);
2019-02-15 15:39:39 +05:30
2020-05-24 23:13:21 +05:30
if (query) {
2020-10-24 23:57:45 +05:30
merged = query
2019-02-15 15:39:39 +05:30
.substr(1)
.split('&')
2020-10-24 23:57:45 +05:30
.reduce((memo, part) => {
2019-02-15 15:39:39 +05:30
if (part.length) {
const kv = part.split('=');
2020-10-24 23:57:45 +05:30
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 };
2019-02-15 15:39:39 +05:30
}
2020-10-24 23:57:45 +05:30
return memo;
}, {});
2019-02-15 15:39:39 +05:30
}
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
Object.assign(merged, params);
2019-01-03 12:48:30 +05:30
2020-11-24 15:15:51 +05:30
const mergedKeys = sort ? Object.keys(merged).sort() : Object.keys(merged);
const newQuery = mergedKeys
2023-01-13 00:05:48 +05:30
.filter((key) => merged[key] !== null && merged[key] !== undefined)
2021-03-08 18:12:59 +05:30
.map((key) => {
2020-10-24 23:57:45 +05:30
let value = merged[key];
const encodedKey = encodeURIComponent(key);
if (spreadArrays && Array.isArray(value)) {
value = merged[key]
2021-03-08 18:12:59 +05:30
.map((arrayValue) => encodeURIComponent(arrayValue))
2020-10-24 23:57:45 +05:30
.join(`&${encodedKey}[]=`);
return `${encodedKey}[]=${value}`;
}
return `${encodedKey}=${encodeURIComponent(value)}`;
})
2019-02-15 15:39:39 +05:30
.join('&');
2018-03-17 18:26:18 +05:30
2020-05-24 23:13:21 +05:30
if (newQuery) {
return `${fullpath}?${newQuery}${fragment}`;
}
return `${fullpath}${fragment}`;
2018-03-17 18:26:18 +05:30
}
2019-03-02 22:35:43 +05:30
/**
* 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
2020-06-23 00:09:42 +05:30
* @param {boolean} skipEncoding - set to true when the url does not require encoding
2019-03-02 22:35:43 +05:30
* @returns {string} A copy of the original url but without the query param
*/
2020-06-23 00:09:42 +05:30
export function removeParams(params, url = window.location.href, skipEncoding = false) {
2019-03-02 22:35:43 +05:30
const [rootAndQuery, fragment] = url.split('#');
const [root, query] = rootAndQuery.split('?');
if (!query) {
return url;
}
2018-03-17 18:26:18 +05:30
2021-03-08 18:12:59 +05:30
const removableParams = skipEncoding ? params : params.map((param) => encodeURIComponent(param));
2020-06-23 00:09:42 +05:30
2019-03-02 22:35:43 +05:30
const updatedQuery = query
.split('&')
2021-03-08 18:12:59 +05:30
.filter((paramPair) => {
2019-03-02 22:35:43 +05:30
const [foundParam] = paramPair.split('=');
2020-06-23 00:09:42 +05:30
return removableParams.indexOf(foundParam) < 0;
2019-03-02 22:35:43 +05:30
})
.join('&');
2018-03-17 18:26:18 +05:30
2019-03-02 22:35:43 +05:30
const writableQuery = updatedQuery.length > 0 ? `?${updatedQuery}` : '';
const writableFragment = fragment ? `#${fragment}` : '';
return `${root}${writableQuery}${writableFragment}`;
2018-03-17 18:26:18 +05:30
}
2021-09-30 23:02:18 +05:30
export const getLocationHash = (hash = window.location.hash) => hash.split('#')[1];
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
/**
* 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);
}
2020-03-13 15:44:24 +05:30
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;
}
2019-03-02 22:35:43 +05:30
/**
* 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}`;
};
2018-03-17 18:26:18 +05:30
export function visitUrl(url, external = false) {
if (external) {
2020-03-13 15:44:24 +05:30
// Simulate `target="_blank" rel="noopener noreferrer"`
2018-03-17 18:26:18 +05:30
// See https://mathiasbynens.github.io/rel-noopener/
const otherWindow = window.open();
otherWindow.opener = null;
otherWindow.location = url;
} else {
window.location.href = url;
}
}
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
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);
}
}
}
2018-03-17 18:26:18 +05:30
export function refreshCurrentPage() {
visitUrl(window.location.href);
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
export function redirectTo(url) {
return window.location.assign(url);
}
2018-05-09 12:01:36 +05:30
2021-03-08 18:12:59 +05:30
export const escapeFileUrl = (fileUrl) => encodeURIComponent(fileUrl).replace(/%2F/g, '/');
2020-03-13 15:44:24 +05:30
2018-05-09 12:01:36 +05:30
export function webIDEUrl(route = undefined) {
2018-11-08 19:23:39 +05:30
let returnUrl = `${gon.relative_url_root || ''}/-/ide/`;
2018-05-09 12:01:36 +05:30
if (route) {
2018-11-08 19:23:39 +05:30
returnUrl += `project${route.replace(new RegExp(`^${gon.relative_url_root || ''}`), '')}`;
2018-05-09 12:01:36 +05:30
}
2020-03-13 15:44:24 +05:30
return escapeFileUrl(returnUrl);
2018-05-09 12:01:36 +05:30
}
2019-07-31 22:56:46 +05:30
2019-09-04 21:01:54 +05:30
/**
* Returns current base URL
*/
export function getBaseURL() {
const { protocol, host } = window.location;
return `${protocol}//${host}`;
}
2021-01-03 14:25:43 +05:30
/**
* Takes a URL and returns content from the start until the final '/'
*
* @param {String} url - full url, including protocol and host
*/
export function stripFinalUrlSegment(url) {
return new URL('.', url).href;
}
2020-05-24 23:13:21 +05:30
/**
* 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) {
2021-09-04 01:27:46 +05:30
return /^\/(?!\/)/.test(url);
2020-05-24 23:13:21 +05:30
}
2020-06-23 00:09:42 +05:30
/**
* Returns true if url is a base64 data URL
*
* @param {String} url
*/
export function isBase64DataUrl(url) {
return /^data:[.\w+-]+\/[.\w+-]+;base64,/.test(url);
}
2021-01-03 14:25:43 +05:30
/**
* Returns true if url is a blob: type url
*
* @param {String} url
*/
export function isBlobUrl(url) {
return /^blob:/.test(url);
}
2019-09-04 21:01:54 +05:30
/**
* Returns true if url is an absolute or root-relative URL
*
* @param {String} url
*/
export function isAbsoluteOrRootRelative(url) {
2020-05-24 23:13:21 +05:30
return isAbsolute(url) || isRootRelative(url);
}
2021-03-11 19:13:27 +05:30
/**
* 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);
}
2020-05-24 23:13:21 +05:30
/**
* 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);
2022-08-27 11:52:29 +05:30
url.pathname = url.pathname.replace(/\/\/+/g, '/');
2020-05-24 23:13:21 +05:30
return absolute ? url.href : decodeURIComponent(url.pathname);
2019-09-04 21:01:54 +05:30
}
/**
* 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;
}
}
2022-07-16 23:28:13 +05:30
/**
* Returns the sanitized url when not safe
*
* @param {String} url
* @returns {String}
*/
export function sanitizeUrl(url) {
if (!isSafeURL(url)) {
return 'about:blank';
}
return url;
}
2021-10-29 20:43:33 +05:30
/**
* 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 '';
}
};
2019-09-04 21:01:54 +05:30
export function getWebSocketProtocol() {
return window.location.protocol.replace('http', 'ws');
}
export function getWebSocketUrl(path) {
return `${getWebSocketProtocol()}//${joinPaths(window.location.host, path)}`;
}
2021-09-30 23:02:18 +05:30
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);
2020-01-01 13:55:28 +05:30
/**
* Convert search query into an object
*
* @param {String} query from "document.location.search"
2020-11-24 15:15:51 +05:30
* @param {Object} options
2021-09-04 01:27:46 +05:30
* @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
2020-01-01 13:55:28 +05:30
* @returns {Object}
*
* ex: "?one=1&two=2" into {one: 1, two: 2}
*/
2021-09-04 01:27:46 +05:30
export function queryToObject(query, { gatherArrays = false, legacySpacesDecode = false } = {}) {
2020-01-01 13:55:28 +05:30
const removeQuestionMarkFromQuery = String(query).startsWith('?') ? query.slice(1) : query;
return removeQuestionMarkFromQuery.split('&').reduce((accumulator, curr) => {
2020-05-24 23:13:21 +05:30
const [key, value] = curr.split('=');
2020-11-24 15:15:51 +05:30
if (value === undefined) {
return accumulator;
2020-05-24 23:13:21 +05:30
}
2021-09-04 01:27:46 +05:30
const decodedValue = legacySpacesDecode ? decodeURIComponent(value) : decodeUrlParameter(value);
2021-10-27 15:23:28 +05:30
const decodedKey = legacySpacesDecode ? decodeURIComponent(key) : decodeUrlParameter(key);
2020-11-24 15:15:51 +05:30
2021-10-27 15:23:28 +05:30
if (gatherArrays && decodedKey.endsWith('[]')) {
const decodedArrayKey = decodedKey.slice(0, -2);
2021-09-04 01:27:46 +05:30
2021-10-27 15:23:28 +05:30
if (!Array.isArray(accumulator[decodedArrayKey])) {
accumulator[decodedArrayKey] = [];
2020-11-24 15:15:51 +05:30
}
2021-09-04 01:27:46 +05:30
2021-10-27 15:23:28 +05:30
accumulator[decodedArrayKey].push(decodedValue);
} else {
2021-09-04 01:27:46 +05:30
accumulator[decodedKey] = decodedValue;
2020-11-24 15:15:51 +05:30
}
2020-01-01 13:55:28 +05:30
return accumulator;
}, {});
}
2021-09-30 23:02:18 +05:30
/**
* 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;
};
2020-01-01 13:55:28 +05:30
/**
* Convert search query object back into a search query
*
2021-09-30 23:02:18 +05:30
* @param {Object?} params that needs to be converted
2020-01-01 13:55:28 +05:30
* @returns {String}
*
* ex: {one: 1, two: 2} into "one=1&two=2"
*
*/
2021-09-30 23:02:18 +05:30
export function objectToQuery(params = {}) {
return Object.keys(params)
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
2020-01-01 13:55:28 +05:30
.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
2020-07-28 23:09:34 +05:30
* @param {Boolean} railsArraySyntax When enabled, changes the array syntax from `keys=` to `keys[]=` according to Rails conventions
2020-01-01 13:55:28 +05:30
* @returns {String} A copy of the original with the updated query params
*/
2020-07-28 23:09:34 +05:30
export const setUrlParams = (
params,
url = window.location.href,
clearParams = false,
railsArraySyntax = false,
2021-04-17 20:07:23 +05:30
decodeParams = false,
2020-07-28 23:09:34 +05:30
) => {
2020-01-01 13:55:28 +05:30
const urlObj = new URL(url);
const queryString = urlObj.search;
const searchParams = clearParams ? new URLSearchParams('') : new URLSearchParams(queryString);
2021-03-08 18:12:59 +05:30
Object.keys(params).forEach((key) => {
2020-01-01 13:55:28 +05:30
if (params[key] === null || params[key] === undefined) {
searchParams.delete(key);
} else if (Array.isArray(params[key])) {
2020-07-28 23:09:34 +05:30
const keyName = railsArraySyntax ? `${key}[]` : key;
2021-09-04 01:27:46 +05:30
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);
}
});
}
2020-01-01 13:55:28 +05:30
} else {
searchParams.set(key, params[key]);
}
});
2021-04-17 20:07:23 +05:30
urlObj.search = decodeParams
? decodeURIComponent(searchParams.toString())
: searchParams.toString();
2020-01-01 13:55:28 +05:30
return urlObj.toString();
};
2020-03-13 15:44:24 +05:30
export function urlIsDifferent(url, compare = String(window.location)) {
return url !== compare;
}
2020-04-22 19:07:51 +05:30
export function getHTTPProtocol(url) {
if (!url) {
return window.location.protocol.slice(0, -1);
}
const protocol = url.split(':');
return protocol.length > 1 ? protocol[0] : undefined;
}
2021-01-03 14:25:43 +05:30
/**
* 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;
}
}
2021-07-02 01:05:55 +05:30
/**
* 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;
}
}
2021-11-18 22:05:49 +05:30
/**
* 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}`),
);
}
2022-08-27 11:52:29 +05:30
/**
* 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');