Skip to content

Commit

Permalink
autocomplete.js: fix handling of comma key codes
Browse files Browse the repository at this point in the history
  • Loading branch information
OriHoch authored and tino097 committed Dec 4, 2018
1 parent 7efd51f commit 861685d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ckan/public/base/javascript/modules/autocomplete.js
Expand Up @@ -267,7 +267,7 @@ this.ckan.module('autocomplete', function (jQuery, _) {
* Returns nothing.
*/
_onKeydown: function (event) {
if (event.which === 188) {
if (typeof event.key !== 'undefined' ? event.key === ',' : event.which === 188) {
event.preventDefault();
setTimeout(function () {
var e = jQuery.Event("keydown", { which: 13 });
Expand Down
16 changes: 14 additions & 2 deletions ckan/public/base/test/spec/modules/autocomplete.spec.js
Expand Up @@ -276,7 +276,7 @@ describe('ckan.modules.AutocompleteModule()', function () {

describe('._onKeydown(event)', function () {
beforeEach(function () {
this.keyDownEvent = jQuery.Event("keydown", { which: 188 });
this.keyDownEvent = jQuery.Event("keydown", { key: ',', which: 188 });
this.fakeEvent = {};
this.clock = sinon.useFakeTimers();
this.jQuery = sinon.stub(jQuery.fn, 'init', jQuery.fn.init);
Expand All @@ -290,7 +290,7 @@ describe('ckan.modules.AutocompleteModule()', function () {
this.Event.restore();
this.trigger.restore();
});

it('should trigger fake "return" keypress if a comma is pressed', function () {
this.module._onKeydown(this.keyDownEvent);

Expand All @@ -303,6 +303,7 @@ describe('ckan.modules.AutocompleteModule()', function () {
});

it('should do nothing if another key is pressed', function () {
this.keyDownEvent.key = '╚';
this.keyDownEvent.which = 200;

this.module._onKeydown(this.keyDownEvent);
Expand All @@ -311,5 +312,16 @@ describe('ckan.modules.AutocompleteModule()', function () {

assert.notCalled(this.Event);
});

it('should do nothing if key is pressed which has the comma key-code but is not a comma', function () {
this.keyDownEvent.key = 'ת';
this.keyDownEvent.which = 188;

this.module._onKeydown(this.keyDownEvent);

this.clock.tick(100);

assert.notCalled(this.Event);
});
});
});

0 comments on commit 861685d

Please sign in to comment.