Skip to content

Commit b51bf07

Browse files
wxiaoguangyardenshoham
authored andcommitted
Escape path for the file list (go-gitea#22741)
Fix go-gitea#22740
1 parent 2e12161 commit b51bf07

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Diff for: web_src/js/features/repo-findfile.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export function filterRepoFilesWeighted(files, filter) {
7272
return filterResult;
7373
}
7474

75+
export function escapePath(s) {
76+
return s.split('/').map(encodeURIComponent).join('/');
77+
}
78+
7579
function filterRepoFiles(filter) {
7680
const treeLink = $repoFindFileInput.attr('data-url-tree-link');
7781
$repoFindFileTableBody.empty();
@@ -83,7 +87,7 @@ function filterRepoFiles(filter) {
8387
for (const r of filterResult) {
8488
const $row = $(tmplRow);
8589
const $a = $row.find('a');
86-
$a.attr('href', `${treeLink}/${r.matchResult.join('')}`);
90+
$a.attr('href', `${treeLink}/${escapePath(r.matchResult.join(''))}`);
8791
const $octiconFile = $(svg('octicon-file')).addClass('mr-3');
8892
$a.append($octiconFile);
8993
// if the target file path is "abc/xyz", to search "bx", then the matchResult is ['a', 'b', 'c/', 'x', 'yz']

Diff for: web_src/js/features/repo-findfile.test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {describe, expect, test} from 'vitest';
2-
import {strSubMatch, calcMatchedWeight, filterRepoFilesWeighted} from './repo-findfile.js';
2+
import {strSubMatch, calcMatchedWeight, filterRepoFilesWeighted, escapePath} from './repo-findfile.js';
33

44
describe('Repo Find Files', () => {
55
test('strSubMatch', () => {
@@ -32,4 +32,9 @@ describe('Repo Find Files', () => {
3232
expect(res).toHaveLength(2);
3333
expect(res[0].matchResult).toEqual(['', 'word', '.txt']);
3434
});
35+
36+
test('escapePath', () => {
37+
expect(escapePath('a/b/c')).toEqual('a/b/c');
38+
expect(escapePath('a/b/ c')).toEqual('a/b/%20c');
39+
});
3540
});

0 commit comments

Comments
 (0)