Commit 9f967ba2 authored by Phil Hughes's avatar Phil Hughes

Merge branch '54218-fix-mergeUrlParams' into 'master'

Resolve "mergeUrlParams wrong with fragment url"

Closes #54218

See merge request gitlab-org/gitlab-ce!23193
parents b6e70c8a fe76827f
...@@ -17,27 +17,29 @@ export function getParameterValues(sParam) { ...@@ -17,27 +17,29 @@ export function getParameterValues(sParam) {
// @param {Object} params - url keys and value to merge // @param {Object} params - url keys and value to merge
// @param {String} url // @param {String} url
export function mergeUrlParams(params, url) { export function mergeUrlParams(params, url) {
let newUrl = Object.keys(params).reduce((acc, paramName) => { const re = /^([^?#]*)(\?[^#]*)?(.*)/;
const paramValue = encodeURIComponent(params[paramName]); const merged = {};
const pattern = new RegExp(`\\b(${paramName}=).*?(&|$)`); const urlparts = url.match(re);
if (paramValue === null) { if (urlparts[2]) {
return acc.replace(pattern, ''); urlparts[2]
} else if (url.search(pattern) !== -1) { .substr(1)
return acc.replace(pattern, `$1${paramValue}$2`); .split('&')
} .forEach(part => {
if (part.length) {
return `${acc}${acc.indexOf('?') > 0 ? '&' : '?'}${paramName}=${paramValue}`; const kv = part.split('=');
}, decodeURIComponent(url)); merged[decodeURIComponent(kv[0])] = decodeURIComponent(kv.slice(1).join('='));
}
});
}
// Remove a trailing ampersand Object.assign(merged, params);
const lastChar = newUrl[newUrl.length - 1];
if (lastChar === '&') { const query = Object.keys(merged)
newUrl = newUrl.slice(0, -1); .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(merged[key])}`)
} .join('&');
return newUrl; return `${urlparts[1]}?${query}${urlparts[3]}`;
} }
export function removeParamQueryString(url, param) { export function removeParamQueryString(url, param) {
......
---
title: "Fix mergeUrlParams with fragment URL"
merge_request: 54218
author: Thomas Holder
type: fixed
import { webIDEUrl } from '~/lib/utils/url_utility'; import { webIDEUrl, mergeUrlParams } from '~/lib/utils/url_utility';
describe('URL utility', () => { describe('URL utility', () => {
describe('webIDEUrl', () => { describe('webIDEUrl', () => {
...@@ -26,4 +26,26 @@ describe('URL utility', () => { ...@@ -26,4 +26,26 @@ describe('URL utility', () => {
}); });
}); });
}); });
describe('mergeUrlParams', () => {
it('adds w', () => {
expect(mergeUrlParams({ w: 1 }, '#frag')).toBe('?w=1#frag');
expect(mergeUrlParams({ w: 1 }, '/path#frag')).toBe('/path?w=1#frag');
expect(mergeUrlParams({ w: 1 }, 'https://host/path')).toBe('https://host/path?w=1');
expect(mergeUrlParams({ w: 1 }, 'https://host/path#frag')).toBe('https://host/path?w=1#frag');
expect(mergeUrlParams({ w: 1 }, 'https://h/p?k1=v1#frag')).toBe('https://h/p?k1=v1&w=1#frag');
});
it('updates w', () => {
expect(mergeUrlParams({ w: 1 }, '?k1=v1&w=0#frag')).toBe('?k1=v1&w=1#frag');
});
it('adds multiple params', () => {
expect(mergeUrlParams({ a: 1, b: 2, c: 3 }, '#frag')).toBe('?a=1&b=2&c=3#frag');
});
it('adds and updates encoded params', () => {
expect(mergeUrlParams({ a: '&', q: '?' }, '?a=%23#frag')).toBe('?a=%26&q=%3F#frag');
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment