mirror of
https://github.com/n8n-io/n8n.git
synced 2026-09-24 23:22:38 +08:00
fix(Read/Write Files from Disk Node): Escape square brackets in file path selector (#31706)
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user