Skip to content

Commit 305b1a2

Browse files
JBYoshigpoitch
andauthored
Fix emoji input on iOS (#764)
* 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>
1 parent 083112d commit 305b1a2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/js/utils/key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default class Key {
8989
if (this.isTab()) {
9090
return TAB
9191
}
92-
return String.fromCharCode(this.charCode)
92+
return String.fromCodePoint(this.charCode)
9393
}
9494

9595
// See https://caniuse.com/#feat=keyboardevent-key for browser support.

tests/unit/utils/key-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,15 @@ test('uses keyCode as a fallback if key is not supported', assert => {
7575
keyInstance = Key.fromEvent(event)
7676
assert.ok(keyInstance.isSpace(), 'keyCode is used if key is not supported')
7777
})
78+
79+
test('properly handles UTF-16 characters', assert => {
80+
let element = $('#qunit-fixture')[0]
81+
82+
let event = Helpers.dom.createMockEvent('keypress', element, {
83+
key: '😀',
84+
keyCode: 128512,
85+
charCode: 128512,
86+
})
87+
let keyInstance = Key.fromEvent(event)
88+
assert.equal('😀', keyInstance.toString(), 'emoji was not properly decoded')
89+
})

0 commit comments

Comments
 (0)