Skip to content

Commit

Permalink
Escape path for the file list (#22741) (#22757)
Browse files Browse the repository at this point in the history
Backport #22741
Fix #22740
  • Loading branch information
wxiaoguang authored Feb 6, 2023
1 parent 965376d commit 03c644c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion web_src/js/features/repo-findfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export function filterRepoFilesWeighted(files, filter) {
return filterResult;
}

export function escapePath(s) {
return s.split('/').map(encodeURIComponent).join('/');
}

function filterRepoFiles(filter) {
const treeLink = $repoFindFileInput.attr('data-url-tree-link');
$repoFindFileTableBody.empty();
Expand All @@ -83,7 +87,7 @@ function filterRepoFiles(filter) {
for (const r of filterResult) {
const $row = $(tmplRow);
const $a = $row.find('a');
$a.attr('href', `${treeLink}/${r.matchResult.join('')}`);
$a.attr('href', `${treeLink}/${escapePath(r.matchResult.join(''))}`);
const $octiconFile = $(svg('octicon-file')).addClass('mr-3');
$a.append($octiconFile);
// if the target file path is "abc/xyz", to search "bx", then the matchResult is ['a', 'b', 'c/', 'x', 'yz']
Expand Down
7 changes: 6 additions & 1 deletion web_src/js/features/repo-findfile.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, expect, test} from 'vitest';
import {strSubMatch, calcMatchedWeight, filterRepoFilesWeighted} from './repo-findfile.js';
import {strSubMatch, calcMatchedWeight, filterRepoFilesWeighted, escapePath} from './repo-findfile.js';

describe('Repo Find Files', () => {
test('strSubMatch', () => {
Expand Down Expand Up @@ -32,4 +32,9 @@ describe('Repo Find Files', () => {
expect(res).toHaveLength(2);
expect(res[0].matchResult).toEqual(['', 'word', '.txt']);
});

test('escapePath', () => {
expect(escapePath('a/b/c')).toEqual('a/b/c');
expect(escapePath('a/b/ c')).toEqual('a/b/%20c');
});
});

0 comments on commit 03c644c

Please sign in to comment.