Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/components/search_bar/query/default_syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,33 @@ describe('defaultSyntax', () => {
expect(printedQuery).toBe(query);
});

describe('phrases with special characters', () => {
const wrappedSpecialChars = "~`!@#$%^&+={}[];',.?".split('');

wrappedSpecialChars.forEach((char) => {
test(`${char} should be wrapped in quotes`, () => {
const query = `user:("foo${char}bar")`;

const ast = defaultSyntax.parse(query);
const printedQuery = defaultSyntax.print(ast);
expect(printedQuery).toBe(query);
});
});

const unwrappedSpecialChars = ['\\"', '(', ')', '_', '-', ':', '*', '\\'];

unwrappedSpecialChars.forEach((char) => {
test(`${char} should not be wrapped in quotes`, () => {
const query = `user:("foo${char}bar")`;
const expected = `user:(foo${char.replaceAll('\\', '')}bar)`;

const ast = defaultSyntax.parse(query);
const printedQuery = defaultSyntax.print(ast);
expect(printedQuery).toBe(expected);
});
});
});

test('single term or expression', () => {
const query = 'f:(foo)';
const ast = defaultSyntax.parse(query);
Expand Down
6 changes: 5 additions & 1 deletion src/components/search_bar/query/default_syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,11 @@ const printValue = (value: Value, options: ParseOptions) => {
return value.toString();
}

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

Expand Down
1 change: 1 addition & 0 deletions upcoming_changelogs/6356.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `EuiSearchBar` now automatically wraps special characters not used by query syntax in quotes