Improve Open-with URL encoding (#33666)

Fix #33665
This commit is contained in:
wxiaoguang 2025-02-22 01:46:05 +08:00 committed by GitHub
parent a61014ea47
commit ead716d204
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View file

@ -42,6 +42,14 @@ export function initRepoActivityTopAuthorsChart() {
}
}
export function substituteRepoOpenWithUrl(tmpl: string, url: string): string {
const pos = tmpl.indexOf('{url}');
if (pos === -1) return tmpl;
const posQuestionMark = tmpl.indexOf('?');
const needEncode = posQuestionMark >= 0 && posQuestionMark < pos;
return tmpl.replace('{url}', needEncode ? encodeURIComponent(url) : url);
}
function initCloneSchemeUrlSelection(parent: Element) {
const elCloneUrlInput = parent.querySelector<HTMLInputElement>('.repo-clone-url');
@ -70,7 +78,7 @@ function initCloneSchemeUrlSelection(parent: Element) {
}
}
for (const el of parent.querySelectorAll<HTMLAnchorElement>('.js-clone-url-editor')) {
el.href = el.getAttribute('data-href-template').replace('{url}', encodeURIComponent(link));
el.href = substituteRepoOpenWithUrl(el.getAttribute('data-href-template'), link);
}
};