Skip to content

Commit ae595aa

Browse files
GiteaBotwxiaoguang
andauthored
Improve Open-with URL encoding (#33666) (#33680)
Backport #33666 by wxiaoguang Fix #33665 Co-authored-by: wxiaoguang <[email protected]>
1 parent aeeccc9 commit ae595aa

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {substituteRepoOpenWithUrl} from './repo-common.ts';
2+
3+
test('substituteRepoOpenWithUrl', () => {
4+
// For example: "x-github-client://openRepo/https://github.com/go-gitea/gitea"
5+
expect(substituteRepoOpenWithUrl('proto://a/{url}', 'https://gitea')).toEqual('proto://a/https://gitea');
6+
expect(substituteRepoOpenWithUrl('proto://a?link={url}', 'https://gitea')).toEqual('proto://a?link=https%3A%2F%2Fgitea');
7+
});

web_src/js/features/repo-common.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ export function initRepoActivityTopAuthorsChart() {
4242
}
4343
}
4444

45+
export function substituteRepoOpenWithUrl(tmpl: string, url: string): string {
46+
const pos = tmpl.indexOf('{url}');
47+
if (pos === -1) return tmpl;
48+
const posQuestionMark = tmpl.indexOf('?');
49+
const needEncode = posQuestionMark >= 0 && posQuestionMark < pos;
50+
return tmpl.replace('{url}', needEncode ? encodeURIComponent(url) : url);
51+
}
52+
4553
function initCloneSchemeUrlSelection(parent: Element) {
4654
const elCloneUrlInput = parent.querySelector<HTMLInputElement>('.repo-clone-url');
4755

@@ -70,7 +78,7 @@ function initCloneSchemeUrlSelection(parent: Element) {
7078
}
7179
}
7280
for (const el of parent.querySelectorAll<HTMLAnchorElement>('.js-clone-url-editor')) {
73-
el.href = el.getAttribute('data-href-template').replace('{url}', encodeURIComponent(link));
81+
el.href = substituteRepoOpenWithUrl(el.getAttribute('data-href-template'), link);
7482
}
7583
};
7684

0 commit comments

Comments
 (0)