[KQL] Fix handling of backslashes when autocompleting values (#85457)

* [KQL] Fix handling of backslashes when autocompleting values

* Revert change to test

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Lukas Olson 2020-12-16 11:21:51 -07:00 committed by GitHub
parent 4b2c9de638
commit 272b2d0628
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View file

@ -14,6 +14,13 @@ describe('Kuery escape', () => {
expect(escapeQuotes(value)).toBe(expected);
});
test('should escape backslashes and quotes', () => {
const value = 'Backslashes \\" in the middle and ends with quotes \\"';
const expected = 'Backslashes \\\\\\" in the middle and ends with quotes \\\\\\"';
expect(escapeQuotes(value)).toBe(expected);
});
test('should escape special characters', () => {
const value = `This \\ has (a lot of) <special> characters, don't you *think*? "Yes."`;
const expected = `This \\\\ has \\(a lot of\\) \\<special\\> characters, don't you \\*think\\*? \\"Yes.\\"`;

View file

@ -6,8 +6,12 @@
import { flow } from 'lodash';
/**
* Escapes backslashes and double-quotes. (Useful when putting a string in quotes to use as a value
* in a KQL expression. See the QuotedCharacter rule in kuery.peg.)
*/
export function escapeQuotes(str: string) {
return str.replace(/"/g, '\\"');
return str.replace(/[\\"]/g, '\\$&');
}
export const escapeKuery = flow(escapeSpecialCharacters, escapeAndOr, escapeNot, escapeWhitespace);