Skip to content

Commit

Permalink
[EuiSearchBar] relaxed syntax around hyphens (#581)
Browse files Browse the repository at this point in the history
Due to their commonality in object names, made it possible to use hyphens without escaping. The only caveat here is that a search term or a field name starts with a hyphen, then it'll have to be escaped with `\`.

Fixes: #577
  • Loading branch information
uboness authored Mar 27, 2018
1 parent 616541c commit 1b2f34b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `0.0.35`.
- Relaxed query syntax of `EuiSearchBar` to allow usage of hyphens without escaping ([#581](https://github.com/elastic/eui/pull/581))

# [`0.0.35`](https://github.com/elastic/eui/tree/v0.0.35)

- Modified `link` and all buttons to support both href and onClick ([554](https://github.com/elastic/eui/pull/554))
- Added `color` prop to `EuiIconTip` ([580](https://github.com/elastic/eui/pull/580))
- Modified `link` and all buttons to support both href and onClick ([#554](https://github.com/elastic/eui/pull/554))
- Added `color` prop to `EuiIconTip` ([#580](https://github.com/elastic/eui/pull/580))

# [`0.0.34`](https://github.com/elastic/eui/tree/v0.0.34)

Expand Down
4 changes: 3 additions & 1 deletion src/components/search_bar/query/default_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fieldName "field name"
fieldChar
= alnum
/ [-]
/ escapedChar
fieldValue "field value"
Expand All @@ -80,13 +81,14 @@ word
valueChar
= alnum
/ [-]
/ escapedChar
escapedChar
= "\\\\" reservedChar
reservedChar
= [:\\-\\\\]
= [\-:\\\\]
alnum "alpha numeric"
= [a-zA-Z0-9]
Expand Down
54 changes: 48 additions & 6 deletions src/components/search_bar/query/default_syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,63 @@ describe('defaultSyntax', () => {
expect(printedQuery).toBe(query);
});

test('escaped chars as default clauses', () => {
const query = '\\- -\\: \\\\';
test('hyphen fields and values', () => {
const query = 'name-1:dash-1 -name-2:dash-2 \\-name-3:dash-3 term-1 -term-2 \\-term-3';
const ast = defaultSyntax.parse(query);

expect(ast).toBeDefined();
expect(ast.clauses).toBeDefined();
expect(ast.clauses).toHaveLength(3);
expect(ast.clauses).toHaveLength(6);

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

let clause = ast.getTermClause('-');
clause = ast.getTermClause('term-2');
expect(clause).toBeDefined();
expect(AST.Term.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(false);
expect(clause.value).toBe('term-2');

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

clause = ast.getSimpleFieldClause('name-1', 'dash-1');
expect(clause).toBeDefined();
expect(AST.Field.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(true);
expect(clause.field).toBe('name-1');
expect(clause.value).toBe('dash-1');

clause = ast.getSimpleFieldClause('name-2', 'dash-2');
expect(clause).toBeDefined();
expect(AST.Field.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(false);
expect(clause.field).toBe('name-2');
expect(clause.value).toBe('dash-2');

clause = ast.getSimpleFieldClause('-name-3', 'dash-3');
expect(clause).toBeDefined();
expect(AST.Field.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(true);
expect(clause.value).toBe('-');
expect(clause.field).toBe('-name-3');
expect(clause.value).toBe('dash-3');
});

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

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

clause = ast.getTermClause(':');
let clause = ast.getTermClause(':');
expect(clause).toBeDefined();
expect(AST.Term.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(false);
Expand Down

0 comments on commit 1b2f34b

Please sign in to comment.