From 2236a16d55fb0803aae90aba5916bd2a65e8b49b Mon Sep 17 00:00:00 2001 From: taichi221228 Date: Wed, 24 Apr 2024 10:04:33 +0900 Subject: [PATCH] Refactor and improve emoji sanitization function The emoji sanitization function has been refactored for better handling of emoji variations. New types have been imported from 'emoji-mart', allowing for a more precise type assignment for the emoji input and output. Bound checking operations have been adjusted to better accommodate and handle custom emojis and skin variations. --- .../mastodon/features/emoji/emoji_utils.ts | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/app/javascript/mastodon/features/emoji/emoji_utils.ts b/app/javascript/mastodon/features/emoji/emoji_utils.ts index 3dc193b2abb..a4b93d1a82b 100644 --- a/app/javascript/mastodon/features/emoji/emoji_utils.ts +++ b/app/javascript/mastodon/features/emoji/emoji_utils.ts @@ -1,6 +1,12 @@ // This code is largely borrowed from: // https://github.com/missive/emoji-mart/blob/5f2ffcc/src/utils/index.js +import type { + BaseEmoji, + CustomEmoji, + EmojiSkin, + PickerProps, +} from 'emoji-mart'; import type { Emoji } from 'emoji-mart/dist-es/utils/data'; import * as data from './emoji_mart_data_light'; @@ -46,22 +52,27 @@ function unifiedToNative(unified: Emoji['unified']) { return String.fromCodePoint(...codePoints); } -/* eslint-disable */ +function sanitize( + emoji: BaseEmoji & + CustomEmoji & + Pick & + Pick & { skin_tone?: EmojiSkin }, +): + | BaseEmoji + | (Omit & Pick) { + const { + name = '', + short_names = [], + skin_tone, + skin_variations, + emoticons = [], + unified = '', + custom, + imageUrl, + } = emoji; + const id = emoji.id || short_names[0]; -// @ts-expect-error -function sanitize(emoji) { - let { - name, - short_names, - skin_tone, - skin_variations, - emoticons, - unified, - custom, - imageUrl, - } = emoji, - id = emoji.id || short_names[0], - colons = `:${id}:`; + let colons = `:${id}:`; if (custom) { return { @@ -84,11 +95,13 @@ function sanitize(emoji) { colons, emoticons, unified: unified.toLowerCase(), - skin: skin_tone || (skin_variations ? 1 : null), + skin: skin_tone ?? (skin_variations ? 1 : null), native: unifiedToNative(unified), }; } +/* eslint-disable */ + function getSanitizedData() { // @ts-expect-error return sanitize(getData(...arguments));