Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
fe76827f
Commit
fe76827f
authored
Nov 30, 2018
by
Thomas Holder
Committed by
Phil Hughes
Nov 30, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Resolve "mergeUrlParams wrong with fragment url"
parent
b6e70c8a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
19 deletions
+48
-19
app/assets/javascripts/lib/utils/url_utility.js
app/assets/javascripts/lib/utils/url_utility.js
+20
-18
changelogs/unreleased/54218-fix-mergeUrlParams.yml
changelogs/unreleased/54218-fix-mergeUrlParams.yml
+5
-0
spec/javascripts/lib/utils/url_utility_spec.js
spec/javascripts/lib/utils/url_utility_spec.js
+23
-1
No files found.
app/assets/javascripts/lib/utils/url_utility.js
View file @
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
)
{
...
...
changelogs/unreleased/54218-fix-mergeUrlParams.yml
0 → 100644
View file @
fe76827f
---
title
:
"
Fix
mergeUrlParams
with
fragment
URL"
merge_request
:
54218
author
:
Thomas Holder
type
:
fixed
spec/javascripts/lib/utils/url_utility_spec.js
View file @
fe76827f
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
'
);
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment