fix(Read/Write Files from Disk Node): Escape square brackets in file path selector (#31706)

This commit is contained in:
Michael Kret
2026-06-09 03:13:51 +00:00
committed by GitHub
parent 7e5c5c4eac
commit 89ae45f6ec
2 changed files with 22 additions and 2 deletions
@@ -35,8 +35,8 @@ export function errorMapper(
}
export function escapeSpecialCharacters(str: string) {
// Escape parentheses
str = str.replace(/[()]/g, '\\$&');
// Escape parentheses and square brackets (glob metacharacters)
str = str.replace(/[()[\]]/g, '\\$&');
return str;
}
@@ -13,6 +13,12 @@ describe('Read/Write Files from Disk', () => {
const expectedOutput = '/home/michael/Desktop/test.txt';
expect(escapeSpecialCharacters(input)).toBe(expectedOutput);
});
it('should escape square brackets in a string', () => {
const input = '/home/michael/Desktop/[release]/test.txt';
const expectedOutput = '/home/michael/Desktop/\\[release\\]/test.txt';
expect(escapeSpecialCharacters(input)).toBe(expectedOutput);
});
});
describe('normalizeFileSelector', () => {
@@ -39,5 +45,19 @@ describe('Read/Write Files from Disk', () => {
const expectedOutput = 'C:/Users/michael/Desktop/test.txt';
expect(normalizeFileSelector(input)).toBe(expectedOutput);
});
it('should escape square brackets in Windows file selector', () => {
const input =
'C:\\Users\\Administrator\\Desktop\\Manga\\Complete\\VTuber Legend [J-Novel Club]\\list.txt';
const expectedOutput =
'C:/Users/Administrator/Desktop/Manga/Complete/VTuber Legend \\[J-Novel Club\\]/list.txt';
expect(normalizeFileSelector(input)).toBe(expectedOutput);
});
it('should escape square brackets in UNIX file selector', () => {
const input = '/home/user/VTuber Legend [J-Novel Club]/list.txt';
const expectedOutput = '/home/user/VTuber Legend \\[J-Novel Club\\]/list.txt';
expect(normalizeFileSelector(input)).toBe(expectedOutput);
});
});
});