Keep emoji sequences intact when stripping invisible characters

This commit is contained in:
Kalle
2026-10-27 16:38:57 +02:00
parent 15b5906589
commit dc2070f005
3 changed files with 25 additions and 2 deletions

View File

@@ -113,6 +113,15 @@ describe("actuallyNonEmptyStringOrNull", () => {
expect(actuallyNonEmptyStringOrNull("\ufe0e")).toBeNull();
expect(actuallyNonEmptyStringOrNull("\ufe0e\ufe0e")).toBeNull();
});
test.each([
{ why: "rainbow flag", value: "she/her \ud83c\udff3\ufe0f\u200d\ud83c\udf08" },
{ why: "transgender flag", value: "\ud83c\udff3\ufe0f\u200d\u26a7\ufe0f" },
{ why: "family", value: "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67" },
{ why: "emoji presentation heart", value: "GG \u2764\ufe0f" },
])("keeps the joiners and selectors of an emoji ($why)", ({ value }) => {
expect(actuallyNonEmptyStringOrNull(value)).toBe(value);
});
});
describe("hexCodeWithoutAlpha", () => {

View File

@@ -438,7 +438,12 @@ const EMPTY_CHARACTERS = [
"\u2060",
"[\\uFE00-\\uFE0F]",
];
const EMPTY_CHARACTERS_REGEX = new RegExp(EMPTY_CHARACTERS.join("|"), "g");
const EMOJI_PART = "\\p{Extended_Pictographic}(?:\\uFE0F|\\p{Emoji_Modifier})?";
const EMOJI_SEQUENCE = `${EMOJI_PART}(?:\\u200D${EMOJI_PART})*`;
const EMOJI_SEQUENCE_OR_EMPTY_CHARACTER_REGEX = new RegExp(
`(${EMOJI_SEQUENCE})|${EMPTY_CHARACTERS.join("|")}`,
"gu",
);
const zalgoRe = /%CC%/;
export const hasZalgo = (txt: string) => zalgoRe.test(encodeURIComponent(txt));
@@ -479,7 +484,12 @@ export const safeNullableStringSchema = ({
export function actuallyNonEmptyStringOrNull(value: unknown) {
if (typeof value !== "string") return value;
const trimmed = value.replace(EMPTY_CHARACTERS_REGEX, "").trim();
const trimmed = value
.replace(
EMOJI_SEQUENCE_OR_EMPTY_CHARACTER_REGEX,
(_, emojiSequence?: string) => emojiSequence ?? "",
)
.trim();
return trimmed === "" ? null : trimmed;
}

View File

@@ -0,0 +1,4 @@
---
type: bug
---
Fixed combined emojis such as 🏳️‍🌈, 🏳️‍⚧️ and 👨‍👩‍👧 getting split into separate emojis when saved in text fields