From 0ff971faa99ca946acf116a51fc31ef7fe87a745 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Mon, 21 Sep 2020 08:43:56 +0200 Subject: [PATCH] fix: Obay variant selectors for emoji regex --- lib/src/event.dart | 10 +++++----- test/event_test.dart | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index a92bbd2..bb6c428 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -770,7 +770,7 @@ class Event extends MatrixEvent { // regexes to fetch the number of emotes, including emoji, and if the message consists of only those // to match an emoji we can use the following regex: - // \x{00a9}|\x{00ae}|[\x{2000}-\x{3300}]|\x{d83c}[\x{d000}-\x{dfff}]|\x{d83d}[\x{d000}-\x{dfff}]|\x{d83e}[\x{d000}-\x{dfff}] + // (?:\x{00a9}|\x{00ae}|[\x{2000}-\x{3300}]|\x{d83c}[\x{d000}-\x{dfff}]|\x{d83d}[\x{d000}-\x{dfff}]|\x{d83e}[\x{d000}-\x{dfff}])[\x{fe00}-\x{fe0f}]? // we need to replace \x{0000} with \u0000, the comment is left in the other format to be able to paste into regex101.com // to see if there is a custom emote, we use the following regex: ]+data-mx-(?:emote|emoticon)(?==|>|\s)[^>]*> // now we combind the two to have four regexes: @@ -779,19 +779,19 @@ class Event extends MatrixEvent { // 3. count number of emoji // 4- count number of emoji or emotes static final RegExp _onlyEmojiRegex = RegExp( - r'^(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|\s)*$', + r'^((?:\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?|\s)*$', caseSensitive: false, multiLine: false); static final RegExp _onlyEmojiEmoteRegex = RegExp( - r'^(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|]+data-mx-(?:emote|emoticon)(?==|>|\s)[^>]*>|\s)*$', + r'^((?:\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?|]+data-mx-(?:emote|emoticon)(?==|>|\s)[^>]*>|\s)*$', caseSensitive: false, multiLine: false); static final RegExp _countEmojiRegex = RegExp( - r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])', + r'((?:\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?)', caseSensitive: false, multiLine: false); static final RegExp _countEmojiEmoteRegex = RegExp( - r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|]+data-mx-(?:emote|emoticon)(?==|>|\s)[^>]*>)', + r'((?:\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?|]+data-mx-(?:emote|emoticon)(?==|>|\s)[^>]*>)', caseSensitive: false, multiLine: false); diff --git a/test/event_test.dart b/test/event_test.dart index aaa4aff..74d7c6a 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -1267,6 +1267,18 @@ void main() { }, null); expect(event.onlyEmotes, true); expect(event.numberEmotes, 2); + // with variant selector + event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'msgtype': 'm.text', + 'body': '❤️', + }, + 'event_id': '\$edit2', + 'sender': '@alice:example.org', + }, null); + expect(event.onlyEmotes, true); + expect(event.numberEmotes, 1); }); }); }