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
Boxiang Sun
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
Show 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) {
// @param {Object} params - url keys and value to merge
// @param {String} url
export
function
mergeUrlParams
(
params
,
url
)
{
let
newUrl
=
Object
.
keys
(
params
).
reduce
((
acc
,
paramName
)
=>
{
const
paramValue
=
encodeURIComponent
(
params
[
paramName
]);
const
pattern
=
new
RegExp
(
`\\b(
${
paramName
}
=).*?(&|$)`
);
if
(
paramValue
===
null
)
{
return
acc
.
replace
(
pattern
,
''
);
}
else
if
(
url
.
search
(
pattern
)
!==
-
1
)
{
return
acc
.
replace
(
pattern
,
`$1
${
paramValue
}
$2`
);
const
re
=
/^
([^
?#
]
*
)(\?[^
#
]
*
)?(
.*
)
/
;
const
merged
=
{};
const
urlparts
=
url
.
match
(
re
);
if
(
urlparts
[
2
])
{
urlparts
[
2
]
.
substr
(
1
)
.
split
(
'
&
'
)
.
forEach
(
part
=>
{
if
(
part
.
length
)
{
const
kv
=
part
.
split
(
'
=
'
);
merged
[
decodeURIComponent
(
kv
[
0
])]
=
decodeURIComponent
(
kv
.
slice
(
1
).
join
(
'
=
'
));
}
});
}
return
`
${
acc
}${
acc
.
indexOf
(
'
?
'
)
>
0
?
'
&
'
:
'
?
'
}${
paramName
}
=
${
paramValue
}
`
;
},
decodeURIComponent
(
url
));
// Remove a trailing ampersand
const
lastChar
=
newUrl
[
newUrl
.
length
-
1
];
Object
.
assign
(
merged
,
params
);
if
(
lastChar
===
'
&
'
)
{
newUrl
=
newUrl
.
slice
(
0
,
-
1
);
}
const
query
=
Object
.
keys
(
merged
)
.
map
(
key
=>
`
${
encodeURIComponent
(
key
)}
=
${
encodeURIComponent
(
merged
[
key
])}
`
)
.
join
(
'
&
'
);
return
newUrl
;
return
`
${
urlparts
[
1
]}
?
${
query
}${
urlparts
[
3
]}
`
;
}
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
(
'
webIDEUrl
'
,
()
=>
{
...
...
@@ -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