Skip to content

Commit

Permalink
Fix emoji input on iOS (bustle#764)
Browse files Browse the repository at this point in the history
* Fix emoji inputs.

Emoji char codes are bigger than String.fromCharCode() supports. String.fromCodePoint() has a larger range.

Fixes TryGhost/Ghost#11541.

* Add a unit test.

Co-authored-by: Garth Poitras <411908+gpoitch@users.noreply.github.com>
  • Loading branch information
2 people authored and daniellockyer committed Aug 16, 2022
1 parent 854bc2d commit b1ac58c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/js/utils/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const Key = class Key {

toString() {
if (this.isTab()) { return TAB; }
return String.fromCharCode(this.charCode);
return String.fromCodePoint(this.charCode);
}

// See https://caniuse.com/#feat=keyboardevent-key for browser support.
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/utils/key-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ test('uses keyCode as a fallback if key is not supported', (assert) => {
'keyCode is used if key is not supported'
);
});

test('properly handles UTF-16 characters', assert => {
let element = $('#qunit-fixture')[0];

let event = Helpers.dom.createMockEvent('keypress', element, {
key: '😀',
keyCode: 128512,
charCode: 128512,
});
let keyInstance = Key.fromEvent(event);
assert.equal('😀', keyInstance.toString(), 'emoji was not properly decoded');
});

0 comments on commit b1ac58c

Please sign in to comment.