Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit a3e5d76

Browse files
authored
Merge pull request #6397 from matrix-org/t3chguy/fix/18040
Respect compound emojis in default avatar initial generation
2 parents 96ddfef + 096d0a7 commit a3e5d76

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

src/Avatar.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { RoomMember } from "matrix-js-sdk/src/models/room-member";
1818
import { User } from "matrix-js-sdk/src/models/user";
1919
import { Room } from "matrix-js-sdk/src/models/room";
2020
import { ResizeMethod } from "matrix-js-sdk/src/@types/partials";
21+
import { split } from "lodash";
2122

2223
import DMRoomMap from './utils/DMRoomMap';
2324
import { mediaFromMxc } from "./customisations/Media";
@@ -122,27 +123,13 @@ export function getInitialLetter(name: string): string {
122123
return undefined;
123124
}
124125

125-
let idx = 0;
126126
const initial = name[0];
127127
if ((initial === '@' || initial === '#' || initial === '+') && name[1]) {
128-
idx++;
128+
name = name.substring(1);
129129
}
130130

131-
// string.codePointAt(0) would do this, but that isn't supported by
132-
// some browsers (notably PhantomJS).
133-
let chars = 1;
134-
const first = name.charCodeAt(idx);
135-
136-
// check if it’s the start of a surrogate pair
137-
if (first >= 0xD800 && first <= 0xDBFF && name[idx+1]) {
138-
const second = name.charCodeAt(idx+1);
139-
if (second >= 0xDC00 && second <= 0xDFFF) {
140-
chars++;
141-
}
142-
}
143-
144-
const firstChar = name.substring(idx, idx+chars);
145-
return firstChar.toUpperCase();
131+
// rely on the grapheme cluster splitter in lodash so that we don't break apart compound emojis
132+
return split(name, "", 1)[0];
146133
}
147134

148135
export function avatarUrlForRoom(room: Room, width: number, height: number, resizeMethod?: ResizeMethod) {

0 commit comments

Comments
 (0)