Skip to content

Commit

Permalink
Merge pull request #351 from bustlelabs/fix-ff-up-down
Browse files Browse the repository at this point in the history
[BUGFIX] up/down arrows in Firefox should not update mobiledoc
  • Loading branch information
bantic committed Mar 24, 2016
2 parents 734b334 + 81e1cb0 commit 678921a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/js/utils/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ const Key = class Key {
return MODIFIERS.ALT & this.modifierMask;
}

isChar(string) {
return this.keyCode === string.toUpperCase().charCodeAt(0);
}

/**
* See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode#Printable_keys_in_standard_position
* and http://stackoverflow.com/a/12467610/137784
Expand All @@ -172,13 +168,16 @@ const Key = class Key {
return false;
}

if (this.toString().length) {
return true;
}

const {keyCode:code} = this;

// Firefox calls keypress events for arrow keys, but they should not be
// considered printable
if (this.isArrow()) {
return false;
}

return (
this.toString().length > 0 ||
(code >= Keycodes['0'] && code <= Keycodes['9']) || // number keys
this.isSpace() ||
this.isTab() ||
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/utils/key-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Helpers from '../../test-helpers';
import Key from 'mobiledoc-kit/utils/key';
import { MODIFIERS } from 'mobiledoc-kit/utils/key';
import Keycodes from 'mobiledoc-kit/utils/keycodes';

const {module, test} = Helpers;

Expand Down Expand Up @@ -37,3 +38,15 @@ test('#hasModifier with SHIFT', (assert) => {
assert.ok(!key.hasModifier(MODIFIERS.CTRL), "CTRL not pressed");
assert.ok(key.hasModifier(MODIFIERS.SHIFT), "SHIFT pressed");
});

// Firefox will fire keypress events for up/down arrow keys,
// they should not be considered printable
test('firefox arrow keypress is not printable', (assert) => {
let element = $('#qunit-fixture')[0];
let event = Helpers.dom.createMockEvent('keypress', element, {
keyCode: Keycodes.DOWN,
charCode: 0
});
let key = Key.fromEvent(event);
assert.ok(!key.isPrintable());
});

0 comments on commit 678921a

Please sign in to comment.