mirror of https://github.com/vector-im/riot-web
Use a more correct test for emoji (#7685)
parent
18995363d0
commit
bfab09e977
|
@ -90,7 +90,7 @@ const MEDIA_API_MXC_REGEX = /\/_matrix\/media\/r0\/(?:download|thumbnail)\/(.+?)
|
||||||
* positives, but useful for fast-path testing strings to see if they
|
* positives, but useful for fast-path testing strings to see if they
|
||||||
* need emojification.
|
* need emojification.
|
||||||
*/
|
*/
|
||||||
export function mightContainEmoji(str: string): boolean {
|
function mightContainEmoji(str: string): boolean {
|
||||||
return SURROGATE_PAIR_PATTERN.test(str) || SYMBOL_PATTERN.test(str);
|
return SURROGATE_PAIR_PATTERN.test(str) || SYMBOL_PATTERN.test(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -432,7 +432,7 @@ function formatEmojis(message: string, isHtmlMessage: boolean): (JSX.Element | s
|
||||||
|
|
||||||
// We use lodash's grapheme splitter to avoid breaking apart compound emojis
|
// We use lodash's grapheme splitter to avoid breaking apart compound emojis
|
||||||
for (const char of split(message, '')) {
|
for (const char of split(message, '')) {
|
||||||
if (mightContainEmoji(char)) {
|
if (EMOJIBASE_REGEX.test(char)) {
|
||||||
if (text) {
|
if (text) {
|
||||||
result.push(text);
|
result.push(text);
|
||||||
text = '';
|
text = '';
|
||||||
|
|
|
@ -16,6 +16,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { split } from "lodash";
|
import { split } from "lodash";
|
||||||
|
import EMOJIBASE_REGEX from "emojibase-regex";
|
||||||
import { MatrixClient } from "matrix-js-sdk/src/client";
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
||||||
import { Room } from "matrix-js-sdk/src/models/room";
|
import { Room } from "matrix-js-sdk/src/models/room";
|
||||||
|
@ -25,7 +26,7 @@ import AutocompleteWrapperModel, {
|
||||||
UpdateCallback,
|
UpdateCallback,
|
||||||
UpdateQuery,
|
UpdateQuery,
|
||||||
} from "./autocomplete";
|
} from "./autocomplete";
|
||||||
import { mightContainEmoji, unicodeToShortcode } from "../HtmlUtils";
|
import { unicodeToShortcode } from "../HtmlUtils";
|
||||||
import * as Avatar from "../Avatar";
|
import * as Avatar from "../Avatar";
|
||||||
import defaultDispatcher from "../dispatcher/dispatcher";
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
||||||
import { Action } from "../dispatcher/actions";
|
import { Action } from "../dispatcher/actions";
|
||||||
|
@ -191,7 +192,7 @@ abstract class BasePart {
|
||||||
|
|
||||||
abstract class PlainBasePart extends BasePart {
|
abstract class PlainBasePart extends BasePart {
|
||||||
protected acceptsInsertion(chr: string, offset: number, inputType: string): boolean {
|
protected acceptsInsertion(chr: string, offset: number, inputType: string): boolean {
|
||||||
if (chr === "\n" || mightContainEmoji(chr)) {
|
if (chr === "\n" || EMOJIBASE_REGEX.test(chr)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// when not pasting or dropping text, reject characters that should start a pill candidate
|
// when not pasting or dropping text, reject characters that should start a pill candidate
|
||||||
|
@ -361,7 +362,7 @@ class NewlinePart extends BasePart implements IBasePart {
|
||||||
|
|
||||||
class EmojiPart extends BasePart implements IBasePart {
|
class EmojiPart extends BasePart implements IBasePart {
|
||||||
protected acceptsInsertion(chr: string, offset: number): boolean {
|
protected acceptsInsertion(chr: string, offset: number): boolean {
|
||||||
return mightContainEmoji(chr);
|
return EMOJIBASE_REGEX.test(chr);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected acceptsRemoval(position: number, chr: string): boolean {
|
protected acceptsRemoval(position: number, chr: string): boolean {
|
||||||
|
@ -553,7 +554,7 @@ export class PartCreator {
|
||||||
case "\n":
|
case "\n":
|
||||||
return new NewlinePart();
|
return new NewlinePart();
|
||||||
default:
|
default:
|
||||||
if (mightContainEmoji(input[0])) {
|
if (EMOJIBASE_REGEX.test(input[0])) {
|
||||||
return new EmojiPart();
|
return new EmojiPart();
|
||||||
}
|
}
|
||||||
return new PlainPart();
|
return new PlainPart();
|
||||||
|
@ -627,7 +628,7 @@ export class PartCreator {
|
||||||
|
|
||||||
// We use lodash's grapheme splitter to avoid breaking apart compound emojis
|
// We use lodash's grapheme splitter to avoid breaking apart compound emojis
|
||||||
for (const char of split(text, "")) {
|
for (const char of split(text, "")) {
|
||||||
if (mightContainEmoji(char)) {
|
if (EMOJIBASE_REGEX.test(char)) {
|
||||||
if (plainText) {
|
if (plainText) {
|
||||||
parts.push(this.plain(plainText));
|
parts.push(this.plain(plainText));
|
||||||
plainText = "";
|
plainText = "";
|
||||||
|
|
Loading…
Reference in New Issue