Improve Open-with URL encoding (#33666)

Fix #33665
pull/33206/head^2
wxiaoguang 3 days ago committed by GitHub
parent a61014ea47
commit ead716d204
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,7 @@
import {substituteRepoOpenWithUrl} from './repo-common.ts';
test('substituteRepoOpenWithUrl', () => {
// For example: "x-github-client://openRepo/https://github.com/go-gitea/gitea"
expect(substituteRepoOpenWithUrl('proto://a/{url}', 'https://gitea')).toEqual('proto://a/https://gitea');
expect(substituteRepoOpenWithUrl('proto://a?link={url}', 'https://gitea')).toEqual('proto://a?link=https%3A%2F%2Fgitea');
});

@ -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) { function initCloneSchemeUrlSelection(parent: Element) {
const elCloneUrlInput = parent.querySelector<HTMLInputElement>('.repo-clone-url'); 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')) { 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);
} }
}; };

Loading…
Cancel
Save