mirror of
https://github.com/mastodon/mastodon.git
synced 2024-08-20 21:08:15 -07:00
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.
This commit is contained in:
parent
3c7ccb2e62
commit
2236a16d55
1 changed files with 29 additions and 16 deletions
|
@ -1,6 +1,12 @@
|
||||||
// This code is largely borrowed from:
|
// This code is largely borrowed from:
|
||||||
// https://github.com/missive/emoji-mart/blob/5f2ffcc/src/utils/index.js
|
// 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 type { Emoji } from 'emoji-mart/dist-es/utils/data';
|
||||||
|
|
||||||
import * as data from './emoji_mart_data_light';
|
import * as data from './emoji_mart_data_light';
|
||||||
|
@ -46,22 +52,27 @@ function unifiedToNative(unified: Emoji['unified']) {
|
||||||
return String.fromCodePoint(...codePoints);
|
return String.fromCodePoint(...codePoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* eslint-disable */
|
function sanitize(
|
||||||
|
emoji: BaseEmoji &
|
||||||
// @ts-expect-error
|
CustomEmoji &
|
||||||
function sanitize(emoji) {
|
Pick<Emoji, 'skin_variations'> &
|
||||||
let {
|
Pick<PickerProps, 'custom'> & { skin_tone?: EmojiSkin },
|
||||||
name,
|
):
|
||||||
short_names,
|
| BaseEmoji
|
||||||
|
| (Omit<CustomEmoji, 'short_names'> & Pick<PickerProps, 'custom'>) {
|
||||||
|
const {
|
||||||
|
name = '',
|
||||||
|
short_names = [],
|
||||||
skin_tone,
|
skin_tone,
|
||||||
skin_variations,
|
skin_variations,
|
||||||
emoticons,
|
emoticons = [],
|
||||||
unified,
|
unified = '',
|
||||||
custom,
|
custom,
|
||||||
imageUrl,
|
imageUrl,
|
||||||
} = emoji,
|
} = emoji;
|
||||||
id = emoji.id || short_names[0],
|
const id = emoji.id || short_names[0];
|
||||||
colons = `:${id}:`;
|
|
||||||
|
let colons = `:${id}:`;
|
||||||
|
|
||||||
if (custom) {
|
if (custom) {
|
||||||
return {
|
return {
|
||||||
|
@ -84,11 +95,13 @@ function sanitize(emoji) {
|
||||||
colons,
|
colons,
|
||||||
emoticons,
|
emoticons,
|
||||||
unified: unified.toLowerCase(),
|
unified: unified.toLowerCase(),
|
||||||
skin: skin_tone || (skin_variations ? 1 : null),
|
skin: skin_tone ?? (skin_variations ? 1 : null),
|
||||||
native: unifiedToNative(unified),
|
native: unifiedToNative(unified),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* eslint-disable */
|
||||||
|
|
||||||
function getSanitizedData() {
|
function getSanitizedData() {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
return sanitize(getData(...arguments));
|
return sanitize(getData(...arguments));
|
||||||
|
|
Loading…
Reference in a new issue