Fix drag and drop escaping (#18965)

This commit is contained in:
Tommaso Sciortino
2026-02-12 18:27:56 -08:00
committed by GitHub
parent 00f73b73bc
commit d82f66973f
8 changed files with 492 additions and 399 deletions

View File

@@ -145,6 +145,7 @@ describe('handleAtCommand', () => {
afterEach(async () => {
abortController.abort();
await fsPromises.rm(testRootDir, { recursive: true, force: true });
vi.unstubAllGlobals();
});
it('should pass through query if no @ command is present', async () => {
@@ -319,6 +320,46 @@ describe('handleAtCommand', () => {
);
}, 10000);
it('should correctly handle double-quoted paths with spaces', async () => {
// Mock platform to win32 so unescapePath strips quotes
vi.stubGlobal(
'process',
Object.create(process, {
platform: {
get: () => 'win32',
},
}),
);
const fileContent = 'Content of file with spaces';
const filePath = await createTestFile(
path.join(testRootDir, 'my folder', 'my file.txt'),
fileContent,
);
// On Windows, the user might provide: @"path/to/my file.txt"
const query = `@"${filePath}"`;
const result = await handleAtCommand({
query,
config: mockConfig,
addItem: mockAddItem,
onDebugMessage: mockOnDebugMessage,
messageId: 126,
signal: abortController.signal,
});
const relativePath = getRelativePath(filePath);
expect(result).toEqual({
processedQuery: [
{ text: `@${relativePath}` },
{ text: '\n--- Content from referenced files ---' },
{ text: `\nContent from @${relativePath}:\n` },
{ text: fileContent },
{ text: '\n--- End of content ---' },
],
});
});
it('should correctly handle file paths with narrow non-breaking space (NNBSP)', async () => {
const nnbsp = '\u202F';
const fileContent = 'NNBSP file content.';

View File

@@ -31,12 +31,13 @@ const REF_CONTENT_FOOTER = `\n${REFERENCE_CONTENT_END}`;
* Regex source for the path/command part of an @ reference.
* It uses strict ASCII whitespace delimiters to allow Unicode characters like NNBSP in filenames.
*
* 1. \\. matches any escaped character (e.g., \ ).
* 2. [^ \t\n\r,;!?()\[\]{}.] matches any character that is NOT a delimiter and NOT a period.
* 3. \.(?!$|[ \t\n\r]) matches a period ONLY if it is NOT followed by whitespace or end-of-string.
* 1. "(?:[^"]*)" matches a double-quoted string (for Windows paths with spaces).
* 2. \\. matches any escaped character (e.g., \ ).
* 3. [^ \t\n\r,;!?()\[\]{}.] matches any character that is NOT a delimiter and NOT a period.
* 4. \.(?!$|[ \t\n\r]) matches a period ONLY if it is NOT followed by whitespace or end-of-string.
*/
export const AT_COMMAND_PATH_REGEX_SOURCE =
'(?:\\\\.|[^ \\t\\n\\r,;!?()\\[\\]{}.]|\\.(?!$|[ \\t\\n\\r]))+';
'(?:(?:"(?:[^"]*)")|(?:\\\\.|[^ \\t\\n\\r,;!?()\\[\\]{}.]|\\.(?!$|[ \\t\\n\\r])))+';
interface HandleAtCommandParams {
query: string;
@@ -85,8 +86,8 @@ function parseAllAtCommands(query: string): AtCommandPart[] {
});
}
// unescapePath expects the @ symbol to be present, and will handle it.
const atPath = unescapePath(fullMatch);
// We strip the @ before unescaping so that unescapePath can handle quoted paths correctly on Windows.
const atPath = '@' + unescapePath(fullMatch.substring(1));
parts.push({ type: 'atPath', content: atPath });
lastIndex = matchIndex + fullMatch.length;