Skip to content

Commit

Permalink
[KQL] Fix handling of backslashes when autocompleting values (#85457)
Browse files Browse the repository at this point in the history
* [KQL] Fix handling of backslashes when autocompleting values

* Revert change to test

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
lukasolson and kibanamachine authored Dec 16, 2020
1 parent 4b2c9de commit 272b2d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.\\"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 272b2d0

Please sign in to comment.