From 81e1cb0f37c2bcdba41c2deff9beccbe736f919a Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Thu, 24 Mar 2016 16:29:36 -0400 Subject: [PATCH] [BUGFIX] up/down arrows in Firefox should not update mobiledoc --- src/js/utils/key.js | 15 +++++++-------- tests/unit/utils/key-test.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/js/utils/key.js b/src/js/utils/key.js index 02770eda8..1a657ff82 100644 --- a/src/js/utils/key.js +++ b/src/js/utils/key.js @@ -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 @@ -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() || diff --git a/tests/unit/utils/key-test.js b/tests/unit/utils/key-test.js index e994980c3..557e7498f 100644 --- a/tests/unit/utils/key-test.js +++ b/tests/unit/utils/key-test.js @@ -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; @@ -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()); +});