Skip to content

Commit 7236902

Browse files
committed
fix
1 parent 9de6594 commit 7236902

8 files changed

Lines changed: 42 additions & 29 deletions

File tree

modules/templates/helper_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,10 @@ func TestQueryBuild(t *testing.T) {
168168
assert.Equal(t, "&a=b&c=d&e=f", string(QueryBuild("&a=b&c=d&e=f", "k", "")))
169169
})
170170
}
171+
172+
func TestQueryEscape(t *testing.T) {
173+
// this test is a reference for "urlQueryEscape" in JS
174+
in := "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" // all non-letter & non-number chars
175+
expected := "%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~"
176+
assert.Equal(t, expected, string(queryEscape(in)))
177+
}

options/locale/locale_en-US.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,7 @@
14901490
"repo.issues.filter_sort.feweststars": "Fewest stars",
14911491
"repo.issues.filter_sort.mostforks": "Most forks",
14921492
"repo.issues.filter_sort.fewestforks": "Fewest forks",
1493+
"repo.issues.quick_goto": "Goto issue",
14931494
"repo.issues.action_open": "Open",
14941495
"repo.issues.action_close": "Close",
14951496
"repo.issues.action_label": "Label",

templates/repo/issue/search.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{{end}}
1313
{{template "shared/search/input" dict "Value" .Keyword}}
1414
{{if .PageIsIssueList}}
15-
<button id="issue-list-quick-goto" class="ui small icon button tw-hidden" data-tooltip-content="{{ctx.Locale.Tr "explore.go_to"}}" data-repo-link="{{.RepoLink}}">{{svg "octicon-hash"}}</button>
15+
<button id="issue-list-quick-goto" type="button" class="ui small icon button tw-hidden tw-mr-[-1px]" data-repo-link="{{.RepoLink}}">{{svg "octicon-hash" 12}} {{ctx.Locale.Tr "repo.issues.quick_goto"}}</button>
1616
{{end}}
1717
{{template "shared/search/button"}}
1818
</div>

web_src/js/features/admin/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {checkAppUrl} from '../common-page.ts';
22
import {hideElem, queryElems, showElem, toggleElem} from '../../utils/dom.ts';
33
import {POST} from '../../modules/fetch.ts';
44
import {fomanticQuery} from '../../modules/fomantic/base.ts';
5+
import {urlQueryEscape} from "../../utils.ts";
56

67
const {appSubUrl} = window.config;
78

@@ -230,7 +231,7 @@ function initAdminAuthentication() {
230231
const elAuthName = document.querySelector<HTMLInputElement>('#auth_name')!;
231232
const onAuthNameChange = function () {
232233
// appSubUrl is either empty or is a path that starts with `/` and doesn't have a trailing slash.
233-
document.querySelector('#oauth2-callback-url')!.textContent = `${window.location.origin}${appSubUrl}/user/oauth2/${encodeURIComponent(elAuthName.value)}/callback`;
234+
document.querySelector('#oauth2-callback-url')!.textContent = `${window.location.origin}${appSubUrl}/user/oauth2/${urlQueryEscape(elAuthName.value)}/callback`;
234235
};
235236
elAuthName.addEventListener('input', onAuthNameChange);
236237
onAuthNameChange();

web_src/js/features/common-issue-list.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test('parseIssueListQuickGotoLink', () => {
55
expect(parseIssueListQuickGotoLink('/link', 'abc')).toEqual('');
66
expect(parseIssueListQuickGotoLink('/link', '123')).toEqual('/link/issues/123');
77
expect(parseIssueListQuickGotoLink('/link', '#123')).toEqual('/link/issues/123');
8-
expect(parseIssueListQuickGotoLink('/link', 'owner/repo#123')).toEqual('');
8+
expect(parseIssueListQuickGotoLink('/link', 'owner/repo#123')).toEqual('/owner/repo/issues/123');
99

1010
expect(parseIssueListQuickGotoLink('', '')).toEqual('');
1111
expect(parseIssueListQuickGotoLink('', 'abc')).toEqual('');

web_src/js/features/common-issue-list.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isElemVisible, onInputDebounce, submitEventSubmitter, toggleElem} from '../utils/dom.ts';
1+
import {onInputDebounce, toggleElem} from '../utils/dom.ts';
22
import {GET} from '../modules/fetch.ts';
33

44
const {appSubUrl} = window.config;
@@ -17,37 +17,25 @@ export function parseIssueListQuickGotoLink(repoLink: string, searchText: string
1717
} else if (reIssueSharpIndex.test(searchText)) {
1818
targetUrl = `${repoLink}/issues/${searchText.substring(1)}`;
1919
}
20-
} else {
21-
// try to parse it for a global search (eg: "owner/repo#123")
22-
const [_, owner, repo, index] = reIssueOwnerRepoIndex.exec(searchText) || [];
23-
if (owner) {
24-
targetUrl = `${appSubUrl}/${owner}/${repo}/issues/${index}`;
25-
}
20+
}
21+
// try to parse it for a global search (eg: "owner/repo#123")
22+
const [_, owner, repo, index] = reIssueOwnerRepoIndex.exec(searchText) || [];
23+
if (owner) {
24+
targetUrl = `${appSubUrl}/${owner}/${repo}/issues/${index}`;
2625
}
2726
return targetUrl;
2827
}
2928

3029
export function initCommonIssueListQuickGoto() {
31-
const goto = document.querySelector<HTMLElement>('#issue-list-quick-goto');
32-
if (!goto) return;
30+
const elGotoButton = document.querySelector<HTMLElement>('#issue-list-quick-goto');
31+
if (!elGotoButton) return;
3332

34-
const form = goto.closest('form')!;
33+
const form = elGotoButton.closest('form')!;
3534
const input = form.querySelector<HTMLInputElement>('input[name=q]')!;
36-
const repoLink = goto.getAttribute('data-repo-link')!;
35+
const repoLink = elGotoButton.getAttribute('data-repo-link') || '';
3736

38-
form.addEventListener('submit', (e) => {
39-
// if there is no goto button, or the form is submitted by non-quick-goto elements, submit the form directly
40-
let doQuickGoto = isElemVisible(goto);
41-
const submitter = submitEventSubmitter(e);
42-
if (submitter !== form && submitter !== input && submitter !== goto) doQuickGoto = false;
43-
if (!doQuickGoto) return;
44-
45-
// if there is a goto button, use its link
46-
e.preventDefault();
47-
const link = goto.getAttribute('data-issue-goto-link');
48-
if (link) {
49-
window.location.href = link;
50-
}
37+
elGotoButton.addEventListener('click', () => {
38+
window.location.href = elGotoButton.getAttribute('data-issue-goto-link')!;
5139
});
5240

5341
const onInput = async () => {
@@ -61,8 +49,8 @@ export function initCommonIssueListQuickGoto() {
6149
// if the input value has changed, then ignore the result
6250
if (input.value !== searchText) return;
6351

64-
toggleElem(goto, Boolean(targetUrl));
65-
goto.setAttribute('data-issue-goto-link', targetUrl);
52+
toggleElem(elGotoButton, Boolean(targetUrl));
53+
elGotoButton.setAttribute('data-issue-goto-link', targetUrl);
6654
};
6755

6856
input.addEventListener('input', onInputDebounce(onInput));

web_src/js/utils.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
dirname, basename, extname, isObject, stripTags, parseIssueHref,
33
parseUrl, translateMonth, translateDay, blobToDataURI,
44
toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64, isImageFile, isVideoFile, parseRepoOwnerPathInfo,
5+
urlQueryEscape,
56
} from './utils.ts';
67

78
test('dirname', () => {
@@ -33,6 +34,12 @@ test('stripTags', () => {
3334
expect(stripTags('<a>test</a>')).toEqual('test');
3435
});
3536

37+
test('urlQueryEscape', () => {
38+
const input = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
39+
const expected = '%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~';
40+
expect(urlQueryEscape(input)).toEqual(expected);
41+
});
42+
3643
test('parseIssueHref', () => {
3744
expect(parseIssueHref('/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
3845
expect(parseIssueHref('/owner/repo/pulls/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'});

web_src/js/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ export function stripTags(text: string): string {
4343
return text;
4444
}
4545

46+
export function urlQueryEscape(s: string) {
47+
// See "TestQueryEscape" in backend
48+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#encoding_for_rfc3986
49+
return encodeURIComponent(s).replace(
50+
/[!'()*]/g,
51+
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
52+
);
53+
}
54+
4655
export function parseIssueHref(href: string): IssuePathInfo {
4756
// FIXME: it should use pathname and trim the appSubUrl ahead
4857
const path = (href || '').replace(/[#?].*$/, '');

0 commit comments

Comments
 (0)