1
0
Fork 0

Escape path for the file list (#22741)

Fix #22740
This commit is contained in:
wxiaoguang 2023-02-04 18:30:55 +08:00 committed by GitHub
parent 2741546bed
commit ea13b23349
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -72,6 +72,10 @@ export function filterRepoFilesWeighted(files, filter) {
return filterResult; return filterResult;
} }
export function escapePath(s) {
return s.split('/').map(encodeURIComponent).join('/');
}
function filterRepoFiles(filter) { function filterRepoFiles(filter) {
const treeLink = $repoFindFileInput.attr('data-url-tree-link'); const treeLink = $repoFindFileInput.attr('data-url-tree-link');
$repoFindFileTableBody.empty(); $repoFindFileTableBody.empty();
@ -83,7 +87,7 @@ function filterRepoFiles(filter) {
for (const r of filterResult) { for (const r of filterResult) {
const $row = $(tmplRow); const $row = $(tmplRow);
const $a = $row.find('a'); 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'); const $octiconFile = $(svg('octicon-file')).addClass('mr-3');
$a.append($octiconFile); $a.append($octiconFile);
// if the target file path is "abc/xyz", to search "bx", then the matchResult is ['a', 'b', 'c/', 'x', 'yz'] // if the target file path is "abc/xyz", to search "bx", then the matchResult is ['a', 'b', 'c/', 'x', 'yz']

View file

@ -1,5 +1,5 @@
import {describe, expect, test} from 'vitest'; 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', () => { describe('Repo Find Files', () => {
test('strSubMatch', () => { test('strSubMatch', () => {
@ -32,4 +32,9 @@ describe('Repo Find Files', () => {
expect(res).toHaveLength(2); expect(res).toHaveLength(2);
expect(res[0].matchResult).toEqual(['', 'word', '.txt']); 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');
});
}); });