Skip to content

Commit

Permalink
[Management] Ensure non alphanumeric characters do not affect index p…
Browse files Browse the repository at this point in the history
…attern searching (#14973) (#14999)

* - Fix issue with non alphanumeric characters affecting index pattern search
- Restructure and add tests

* More tests
  • Loading branch information
chrisronline authored Nov 16, 2017
1 parent 8938dd2 commit 3d68d3f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { uiModules } from 'ui/modules';
import { appendWildcard } from './lib/append_wildcard';

const module = uiModules.get('apps/management');

Expand All @@ -15,33 +16,13 @@ module.directive('appendWildcard', function () {
require: 'ngModel',
link: function ($scope, $elem, $attrs, $ctrl) {
$elem.on('keydown', (e) => {
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
// is not recommended so we need to rely on `key` but browser support
// is still spotty (https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
// so just bail if it's not supported
if (!e.key) {
return;
}

if (!/[a-z0-9]/i.test(e.key)) {
return;
}

// If the user is holding down ctrl/cmd, they are performing some shortcut
// and do not interpret literally
if (e.metaKey) {
return;
}

let indexPatternName = $elem.val() + e.key;
if (indexPatternName.length === 1 && indexPatternName !== '*') {
indexPatternName += '*';
const newIndexPattern = appendWildcard(e, $elem.val());
if (newIndexPattern) {
e.preventDefault();
$elem.val(indexPatternName);
$elem.val(newIndexPattern);
$elem[0].setSelectionRange(1, 1);
$ctrl.$setViewValue(newIndexPattern);
}

$ctrl.$setViewValue(indexPatternName);
});
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import expect from 'expect.js';

import { appendWildcard } from '../append_wildcard';

describe('append_wildcard', function () {
it('should add a wildcard for an alphabet input', () => {
[
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
].forEach(char => {
expect(appendWildcard({ key: char }, '')).to.be(`${char}*`);
expect(appendWildcard({ key: char.toUpperCase() }, '')).to.be(`${char.toUpperCase()}*`);
});
});

it('should add a wildcard for a number input', () => {
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].forEach(num => {
expect(appendWildcard({ key: num }, '')).to.be(`${num}*`);
});
});

it('should NOT add a wildcard for a non alphanumeric input', () => {
['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+'].forEach(char => {
expect(appendWildcard({ key: char }, '')).to.be(undefined);
});
});

it('should NOT add a wildcard for multi-length input', () => {
expect(appendWildcard({ key: 'Tab' }, '')).to.be(undefined);
});

it('should NOT add a wildcard if the value is longer than 1 character', () => {
expect(appendWildcard({ key: 'a' }, 'b')).to.be(undefined);
});

it('should NOT add a wildcard if the input is a wildcard', () => {
expect(appendWildcard({ key: '*' }, '')).to.be(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const appendWildcard = (keyboardEvent, value) => {
const { key: keyPressed, metaKey } = keyboardEvent;

// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
// is not recommended so we need to rely on `key` but browser support
// is still spotty (https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
// so just bail if it's not supported
if (!keyPressed) {
return;
}

// If the user is holding down ctrl/cmd, they are performing some shortcut
// and do not interpret literally
if (metaKey) {
return;
}

// If it's not a letter, number or is something longer, reject it
if (!/[a-z0-9]/i.test(keyPressed) || keyPressed.length !== 1) {
return;
}

let newValue = value + keyPressed;
if (newValue.length !== 1 || newValue === '*') {
return;
}

newValue += '*';
return newValue;
};

0 comments on commit 3d68d3f

Please sign in to comment.