mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[Management] Ensure non alphanumeric characters do not affect index pattern searching (#14973)
* - Fix issue with non alphanumeric characters affecting index pattern search - Restructure and add tests * More tests
This commit is contained in:
parent
b1ae3f6125
commit
b20bdae627
3 changed files with 75 additions and 24 deletions
|
@ -1,4 +1,5 @@
|
|||
import { uiModules } from 'ui/modules';
|
||||
import { appendWildcard } from './lib/append_wildcard';
|
||||
|
||||
const module = uiModules.get('apps/management');
|
||||
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue