Skip to content

Commit 2426084

Browse files
committed
fix(web): handle quoted string escapes correctly
1 parent ae3ba70 commit 2426084

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎packages/web/src/features/search/parser.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ describe('parseQuerySyntaxIntoIR', () => {
7272
});
7373
});
7474

75+
it('unescapes a backslash-escaped newline', async () => {
76+
const ir = await parseQuerySyntaxIntoIR({
77+
query: '"line1\\\nline2"',
78+
options: {},
79+
prisma,
80+
});
81+
82+
expect(ir).toMatchObject({
83+
query: 'substring',
84+
substring: { pattern: 'line1\nline2' },
85+
});
86+
});
87+
88+
it('preserves backslashes before other characters', async () => {
89+
const ir = await parseQuerySyntaxIntoIR({
90+
query: '"C:\\temp"',
91+
options: {},
92+
prisma,
93+
});
94+
95+
expect(ir).toMatchObject({
96+
query: 'substring',
97+
substring: { pattern: 'C:\\temp' },
98+
});
99+
});
100+
75101
it('unescapes a quoted content: value', async () => {
76102
const ir = await parseQuerySyntaxIntoIR({
77103
query: 'content:"say \\"hi\\""',

‎packages/web/src/features/search/parser.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ const findLinguistLanguage = (value: string): string => {
7272
}
7373

7474
/**
75-
* Quoted strings may contain backslash escapes (e.g. `\"`). Keyword search
76-
* matches patterns literally, so the escapes are resolved here. In regex mode
77-
* they are left in place for the regex engine to interpret.
75+
* Quoted strings may escape quotes, backslashes, and line terminators. Keyword
76+
* search matches patterns literally, so those escapes are resolved here. Other
77+
* backslashes remain literal (e.g. in Windows paths). In regex mode escapes
78+
* are left in place for the regex engine to interpret.
7879
*/
7980
const unescapeQuotedString = (value: string): string => {
80-
return value.replace(/\\(.)/g, '$1');
81+
return value.replace(/\\(["\\\r\n])/g, '$1');
8182
}
8283

8384
/**

0 commit comments

Comments
 (0)