Merge pull request #7311 from allenve/master

fix: normalizeLink url处理问题
This commit is contained in:
hsm-lv 2023-06-30 10:06:22 +08:00 committed by GitHub
commit ad0a019080
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,13 +9,21 @@ export const normalizeLink = (to: string, location = window.location) => {
const idx = to.indexOf('?');
const idx2 = to.indexOf('#');
let pathname = ~idx
? to.substring(0, idx)
: ~idx2
? to.substring(0, idx2)
: to;
let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
let hash = ~idx2 ? to.substring(idx2) : location.hash;
let pathname = to;
let search = '';
let hash = location.hash;
// host?a=a#b 的情况
if (idx < idx2) {
pathname = ~idx ? to.substring(0, idx) : ~idx2 ? to.substring(0, idx2) : to;
hash = ~idx2 ? to.substring(idx2) : location.hash;
search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
}
// host#b?a=a 的情况
else if (idx > idx2) {
pathname = ~idx2 ? to.substring(0, idx2) : ~idx ? to.substring(0, idx) : to;
hash = ~idx2 ? to.substring(idx2, ~idx ? idx : undefined) : location.hash;
search = ~idx ? to.substring(idx) : '';
}
if (!pathname) {
pathname = location.pathname;
@ -33,5 +41,6 @@ export const normalizeLink = (to: string, location = window.location) => {
pathname = paths.concat(pathname).join('/');
}
return pathname + search + hash;
const rest = idx < idx2 ? search + hash : hash + search;
return pathname + rest;
};