Skip to content

Commit 5adb634

Browse files
committed
fix(shared): keep skin tones and marks with base
1 parent e4e34ce commit 5adb634

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

‎packages/shared/src/String.test.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ describe("truncate", () => {
1818

1919
it("never cuts a ZWJ emoji sequence in half", () => {
2020
const family = "👨‍👩‍👧‍👦";
21-
expect(truncate(`${family} x`, 4)).toBe("👨...");
22-
expect(truncate(`${family} x`, 6)).toBe("👨‍👩...");
23-
expect(truncate(`${family} x`, 11)).toBe(`${family}...`);
21+
expect(truncate(`${family} x`, 4)).toBe("...");
22+
expect(truncate(`${family} x`, 6)).toBe("...");
23+
expect(truncate(`ab${family} x`, 13)).toBe(`ab${family}...`);
24+
expect(truncate(`ab${family} x`, 12)).toBe("ab...");
25+
});
26+
27+
it("keeps a skin tone, variation selector or combining mark with its base", () => {
28+
expect(truncate("abc👍🏽 z", 5)).toBe("abc...");
29+
expect(truncate("abc👍🏽 z", 7)).toBe("abc👍🏽...");
30+
expect(truncate("abc❤️ z", 4)).toBe("abc...");
31+
expect(truncate("abc1️⃣ z", 5)).toBe("abc...");
32+
expect(truncate("cafe\u0301 au lait", 4)).toBe("caf...");
2433
});
2534

2635
it("never splits a flag emoji", () => {

‎packages/shared/src/String.ts‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ export function truncate(text: string, maxLength = 50): string {
44
return trimmed;
55
}
66

7-
// Back off to a grapheme boundary so the cut never splits a surrogate
8-
// pair, a ZWJ sequence, or a flag pair in half.
7+
// Back off to a grapheme boundary so the cut never splits a surrogate pair,
8+
// drops a skin tone, variation selector or combining mark, breaks a ZWJ
9+
// sequence, or separates the two halves of a flag.
910
let end = maxLength;
1011
while (isMidCluster(trimmed, end)) {
1112
end = codePointStart(trimmed, end);
@@ -15,35 +16,54 @@ export function truncate(text: string, maxLength = 50): string {
1516

1617
const isHighSurrogate = (code: number): boolean => code >= 0xd800 && code <= 0xdbff;
1718
const isLowSurrogate = (code: number): boolean => code >= 0xdc00 && code <= 0xdfff;
18-
const isRegionalIndicator = (code: number): boolean => code >= 0x1f1e6 && code <= 0x1f1ff;
19+
const isRegionalIndicator = (code: number | undefined): boolean =>
20+
code !== undefined && code >= 0x1f1e6 && code <= 0x1f1ff;
21+
22+
// Code points that attach to the one before them: combining marks (which
23+
// include variation selectors and the keycap mark), ZWJ, skin-tone modifiers
24+
// and emoji tag characters. Intl.Segmenter would cover these, but Hermes lacks it.
25+
function attachesToPrevious(code: number | undefined): boolean {
26+
if (code === undefined) return false;
27+
return (
28+
code === 0x200d ||
29+
(code >= 0x1f3fb && code <= 0x1f3ff) ||
30+
(code >= 0xe0020 && code <= 0xe007f) ||
31+
/\p{M}/u.test(String.fromCodePoint(code))
32+
);
33+
}
1934

2035
// Start index of the code point ending at text[index - 1].
2136
function codePointStart(text: string, index: number): number {
22-
return index > 1 && isLowSurrogate(text.charCodeAt(index - 1)) && isHighSurrogate(text.charCodeAt(index - 2))
37+
return index > 1 &&
38+
isLowSurrogate(text.charCodeAt(index - 1)) &&
39+
isHighSurrogate(text.charCodeAt(index - 2))
2340
? index - 2
2441
: index - 1;
2542
}
2643

2744
// True when a cut at `index` falls inside a grapheme cluster: between the
28-
// halves of a surrogate pair, after a ZWJ whose partner is cut off, or
29-
// between the two regional indicators of a flag.
45+
// halves of a surrogate pair, before a code point that attaches to the
46+
// previous one, after a ZWJ, or between the two regional indicators of a flag.
3047
function isMidCluster(text: string, index: number): boolean {
3148
if (index <= 0 || index >= text.length) {
3249
return false;
3350
}
3451
if (isHighSurrogate(text.charCodeAt(index - 1)) && isLowSurrogate(text.charCodeAt(index))) {
3552
return true;
3653
}
54+
if (attachesToPrevious(text.codePointAt(index))) {
55+
return true;
56+
}
3757
const start = codePointStart(text, index);
3858
const code = text.codePointAt(start);
3959
if (code === 0x200d) {
4060
return true;
4161
}
42-
if (isRegionalIndicator(code)) {
62+
if (isRegionalIndicator(code) && isRegionalIndicator(text.codePointAt(index))) {
4363
// RIs pair from the start of their run: an odd number of RIs before this
4464
// one means it completes a flag, so the cut after it is safe.
4565
let count = 0;
46-
for (let i = start; i > 0; ) {
66+
for (let i = start; i > 0;) {
4767
i = codePointStart(text, i);
4868
if (!isRegionalIndicator(text.codePointAt(i))) break;
4969
count += 1;

0 commit comments

Comments
 (0)