Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
fe76827f
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fe76827f
编写于
11月 30, 2018
作者:
T
Thomas Holder
提交者:
Phil Hughes
11月 30, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Resolve "mergeUrlParams wrong with fragment url"
上级
b6e70c8a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
48 addition
and
19 deletion
+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
未找到文件。
app/assets/javascripts/lib/utils/url_utility.js
浏览文件 @
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`
);
}
return
`
${
acc
}${
acc
.
indexOf
(
'
?
'
)
>
0
?
'
&
'
:
'
?
'
}${
paramName
}
=
${
paramValue
}
`
;
},
decodeURIComponent
(
url
));
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
(
'
=
'
));
}
});
}
// 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
浏览文件 @
fe76827f
---
title
:
"
Fix
mergeUrlParams
with
fragment
URL"
merge_request
:
54218
author
:
Thomas Holder
type
:
fixed
spec/javascripts/lib/utils/url_utility_spec.js
浏览文件 @
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
'
);
});
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录