Browse Source

Fix shortnameToUnicode for multi-codepoint emoji (#730)

The `convert.fromCodePoint` function only converts the first codepoint in a list of dash-separated codepoints.

For multi-codepoint emoji, the codepoints need to be converted separately.
Danilo Bargen 6 years ago
parent
commit
305f426d3d
2 changed files with 10 additions and 3 deletions
  1. 6 3
      src/helpers/emoji.ts
  2. 4 0
      tests/ts/emoji_helpers.ts

+ 6 - 3
src/helpers/emoji.ts

@@ -3774,11 +3774,14 @@ export function emojify(text: string): string {
  * Translate a shortname to UTF8.
  */
 export function shortnameToUnicode(shortname: string): string | null {
-    const codepoint = shortnames[shortname];
-    if (codepoint === undefined) {
+    const codepoints = shortnames[shortname];
+    if (codepoints === undefined) {
         return null;
     }
-    return twemoji.convert.fromCodePoint(codepoint);
+    return codepoints
+        .split('-')
+        .map(twemoji.convert.fromCodePoint)
+        .join('');
 }
 
 /**

+ 4 - 0
tests/ts/emoji_helpers.ts

@@ -44,6 +44,10 @@ describe('Emoji Helpers', () => {
         it('returns null for unknown shortcodes', function() {
             expect(shortnameToUnicode('sömbsöp')).toBeNull();
         });
+
+        it('handles multi-codepoint emoji', function() {
+            expect(shortnameToUnicode('ch')).toEqual('\ud83c\udde8\ud83c\udded');
+        });
     });
 
     describe('enlargeSingleEmoji', function() {