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

Commit bfab09e

Browse files
authored
Use a more correct test for emoji (#7685)
1 parent 1899536 commit bfab09e

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/HtmlUtils.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const MEDIA_API_MXC_REGEX = /\/_matrix\/media\/r0\/(?:download|thumbnail)\/(.+?)
9090
* positives, but useful for fast-path testing strings to see if they
9191
* need emojification.
9292
*/
93-
export function mightContainEmoji(str: string): boolean {
93+
function mightContainEmoji(str: string): boolean {
9494
return SURROGATE_PAIR_PATTERN.test(str) || SYMBOL_PATTERN.test(str);
9595
}
9696

@@ -432,7 +432,7 @@ function formatEmojis(message: string, isHtmlMessage: boolean): (JSX.Element | s
432432

433433
// We use lodash's grapheme splitter to avoid breaking apart compound emojis
434434
for (const char of split(message, '')) {
435-
if (mightContainEmoji(char)) {
435+
if (EMOJIBASE_REGEX.test(char)) {
436436
if (text) {
437437
result.push(text);
438438
text = '';

src/editor/parts.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
*/
1717

1818
import { split } from "lodash";
19+
import EMOJIBASE_REGEX from "emojibase-regex";
1920
import { MatrixClient } from "matrix-js-sdk/src/client";
2021
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
2122
import { Room } from "matrix-js-sdk/src/models/room";
@@ -25,7 +26,7 @@ import AutocompleteWrapperModel, {
2526
UpdateCallback,
2627
UpdateQuery,
2728
} from "./autocomplete";
28-
import { mightContainEmoji, unicodeToShortcode } from "../HtmlUtils";
29+
import { unicodeToShortcode } from "../HtmlUtils";
2930
import * as Avatar from "../Avatar";
3031
import defaultDispatcher from "../dispatcher/dispatcher";
3132
import { Action } from "../dispatcher/actions";
@@ -191,7 +192,7 @@ abstract class BasePart {
191192

192193
abstract class PlainBasePart extends BasePart {
193194
protected acceptsInsertion(chr: string, offset: number, inputType: string): boolean {
194-
if (chr === "\n" || mightContainEmoji(chr)) {
195+
if (chr === "\n" || EMOJIBASE_REGEX.test(chr)) {
195196
return false;
196197
}
197198
// when not pasting or dropping text, reject characters that should start a pill candidate
@@ -361,7 +362,7 @@ class NewlinePart extends BasePart implements IBasePart {
361362

362363
class EmojiPart extends BasePart implements IBasePart {
363364
protected acceptsInsertion(chr: string, offset: number): boolean {
364-
return mightContainEmoji(chr);
365+
return EMOJIBASE_REGEX.test(chr);
365366
}
366367

367368
protected acceptsRemoval(position: number, chr: string): boolean {
@@ -553,7 +554,7 @@ export class PartCreator {
553554
case "\n":
554555
return new NewlinePart();
555556
default:
556-
if (mightContainEmoji(input[0])) {
557+
if (EMOJIBASE_REGEX.test(input[0])) {
557558
return new EmojiPart();
558559
}
559560
return new PlainPart();
@@ -627,7 +628,7 @@ export class PartCreator {
627628

628629
// We use lodash's grapheme splitter to avoid breaking apart compound emojis
629630
for (const char of split(text, "")) {
630-
if (mightContainEmoji(char)) {
631+
if (EMOJIBASE_REGEX.test(char)) {
631632
if (plainText) {
632633
parts.push(this.plain(plainText));
633634
plainText = "";

0 commit comments

Comments
 (0)