Skip to content

Commit cd3f835

Browse files
author
Constance
authored
[EuiSearchBar] Automatically wrap special characters (that aren't used in EQL syntax) in quotes (#6356)
* [REVERT ME] local docs for testing * Escape special characters that are not used for EQL syntax * changelog * Revert "[REVERT ME] local docs for testing" This reverts commit 0ce6140.
1 parent a447760 commit cd3f835

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/components/search_bar/query/default_syntax.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,33 @@ describe('defaultSyntax', () => {
639639
expect(printedQuery).toBe(query);
640640
});
641641

642+
describe('phrases with special characters', () => {
643+
const wrappedSpecialChars = "~`!@#$%^&+={}[];',.?".split('');
644+
645+
wrappedSpecialChars.forEach((char) => {
646+
test(`${char} should be wrapped in quotes`, () => {
647+
const query = `user:("foo${char}bar")`;
648+
649+
const ast = defaultSyntax.parse(query);
650+
const printedQuery = defaultSyntax.print(ast);
651+
expect(printedQuery).toBe(query);
652+
});
653+
});
654+
655+
const unwrappedSpecialChars = ['\\"', '(', ')', '_', '-', ':', '*', '\\'];
656+
657+
unwrappedSpecialChars.forEach((char) => {
658+
test(`${char} should not be wrapped in quotes`, () => {
659+
const query = `user:("foo${char}bar")`;
660+
const expected = `user:(foo${char.replaceAll('\\', '')}bar)`;
661+
662+
const ast = defaultSyntax.parse(query);
663+
const printedQuery = defaultSyntax.print(ast);
664+
expect(printedQuery).toBe(expected);
665+
});
666+
});
667+
});
668+
642669
test('single term or expression', () => {
643670
const query = 'f:(foo)';
644671
const ast = defaultSyntax.parse(query);

src/components/search_bar/query/default_syntax.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,11 @@ const printValue = (value: Value, options: ParseOptions) => {
451451
return value.toString();
452452
}
453453

454-
if (value.length === 0 || value.match(/\s/) || value.toLowerCase() === 'or') {
454+
if (
455+
value.length === 0 ||
456+
value.match(/[^\w\-_*:()"/\\]/) || // Escape spaces and special characters not used as syntax identifiers
457+
value.toLowerCase() === 'or'
458+
) {
455459
return `"${escapePhraseValue(value)}"`;
456460
}
457461

upcoming_changelogs/6356.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `EuiSearchBar` now automatically wraps special characters not used by query syntax in quotes

0 commit comments

Comments
 (0)