Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parenthesis in EuiSearchBar values #2791

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 5 additions & 4 deletions src/components/search_bar/query/default_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ containsValue

phrase
= '"' space? phrase:(
phraseWord? (space phraseWord)* { return unescapeValue(text()); }
(phraseWord+)? (space phraseWord+)* { return unescapeValue(text()); }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanding phraseWord (below) with parenthesis support, instead of adding them to wordChar, means that the parenthesis is grouped in a different phraseWord than the rest of the phrase; this line's change allows multiple phaseWords to exist next to each other without spaces separating

) space? '"' { return Exp.string(phrase, location()); }

phraseWord
= orWord
/ word
/ [()] // adding parens directly to "wordChar" makes it too aggresive as it consumes the closing paren

word
= wordChar+ {
Expand Down Expand Up @@ -175,7 +176,7 @@ escapedChar
= "\\\\" reservedChar

reservedChar
= [\-:\\\\]
= [\-:\\\\()]

orWord
= ([oO][rR])
Expand Down Expand Up @@ -214,11 +215,11 @@ space "whitespace"
`;

const unescapeValue = value => {
return value.replace(/\\([:\-\\])/g, '$1');
return value.replace(/\\([:\-\\()])/g, '$1');
};

const escapeValue = value => {
return value.replace(/([:\-\\])/g, '\\$1');
return value.replace(/([:\-\\()])/g, '\\$1');
};

const escapeFieldValue = value => {
Expand Down
24 changes: 18 additions & 6 deletions src/components/search_bar/query/default_syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ describe('defaultSyntax', () => {
});

test('escaped chars as default clauses', () => {
const query = '-\\: \\\\';
const query = '-\\: \\\\ \\( \\)';
const ast = defaultSyntax.parse(query);

expect(ast).toBeDefined();
expect(ast.clauses).toBeDefined();
expect(ast.clauses).toHaveLength(2);
expect(ast.clauses).toHaveLength(4);

let clause = ast.getTermClause(':');
expect(clause).toBeDefined();
Expand All @@ -110,6 +110,18 @@ describe('defaultSyntax', () => {
expect(AST.Match.isMustClause(clause)).toBe(true);
expect(clause.value).toBe('\\');

clause = ast.getTermClause('(');
expect(clause).toBeDefined();
expect(AST.Term.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(true);
expect(clause.value).toBe('(');

clause = ast.getTermClause(')');
expect(clause).toBeDefined();
expect(AST.Term.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(true);
expect(clause.value).toBe(')');

const printedQuery = defaultSyntax.print(ast);
expect(printedQuery).toBe(query);
});
Expand Down Expand Up @@ -424,20 +436,20 @@ describe('defaultSyntax', () => {
});

test('term phrases', () => {
const query = '"foo bar"';
const query = '"foo (bar)"';
const ast = defaultSyntax.parse(query);

expect(ast).toBeDefined();
expect(ast.clauses).toHaveLength(1);

const clause = ast.getTermClause('foo bar');
const clause = ast.getTermClause('foo (bar)');
expect(clause).toBeDefined();
expect(AST.Term.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(true);
expect(clause.value).toBe('foo bar');
expect(clause.value).toBe('foo (bar)');

const printedQuery = defaultSyntax.print(ast);
expect(printedQuery).toBe(query);
expect(printedQuery).toBe('"foo \\(bar\\)"');
});

test('field phrases', () => {
Expand Down