Skip to content

Commit

Permalink
Fix emoji input on iOS (#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
JBYoshi and gpoitch committed Aug 11, 2022
1 parent 083112d commit 305b1a2
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.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class Key {
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 @@ -75,3 +75,15 @@ test('uses keyCode as a fallback if key is not supported', assert => {
keyInstance = Key.fromEvent(event)
assert.ok(keyInstance.isSpace(), '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 305b1a2

Please sign in to comment.