From 011b60af610c4579c012392a903acb25bda3100b Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 7 Jun 2022 20:02:48 -0400 Subject: [PATCH] Fix Emoji detection And move to Zotero.Utilities.Internal.isOnlyEmoji() Fixes #2643 --- chrome/content/zotero/itemTree.jsx | 8 +------- .../zotero/xpcom/utilities_internal.js | 6 ++++++ test/tests/utilities_internalTest.js | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/chrome/content/zotero/itemTree.jsx b/chrome/content/zotero/itemTree.jsx index 1b4b5ff483..a2331c4e44 100644 --- a/chrome/content/zotero/itemTree.jsx +++ b/chrome/content/zotero/itemTree.jsx @@ -40,7 +40,6 @@ Cu.import("resource://gre/modules/osfile.jsm"); const CHILD_INDENT = 12; const COLORED_TAGS_RE = new RegExp("^[0-" + Zotero.Tags.MAX_COLORED_TAGS + "]{1}$"); const COLUMN_PREFS_FILEPATH = OS.Path.join(Zotero.Profile.dir, "treePrefs.json"); -const EMOJI_RE = /\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}|[\u200D\uFE0F]/gu; const ATTACHMENT_STATE_LOAD_DELAY = 150; //ms var ItemTree = class ItemTree extends LibraryTree { @@ -3789,11 +3788,6 @@ var ItemTree = class ItemTree extends LibraryTree { || (item.isFileAttachment() && item.isTopLevelItem()); } - _isOnlyEmoji(str) { - // Remove emoji, Zero Width Joiner, and Variation Selector-16 and see if anything's left - return !str.replace(EMOJI_RE, ''); - } - _getTagSwatch(tag, color) { let span = document.createElement('span'); span.className = 'tag-swatch'; @@ -3801,7 +3795,7 @@ var ItemTree = class ItemTree extends LibraryTree { // // TODO: Check for a maximum number of graphemes, which is hard to do // https://stackoverflow.com/a/54369605 - if (this._isOnlyEmoji(tag)) { + if (Zotero.Utilities.Internal.isOnlyEmoji(tag)) { span.textContent = tag; } // Otherwise display color diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 366e12ddf6..87bbe46615 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -405,6 +405,12 @@ Zotero.Utilities.Internal = { return s; }, + isOnlyEmoji: function (str) { + // Remove emoji and Variation Selector-16 and see if anything's left + const re = /\p{Extended_Pictographic}|\uFE0F/gu; + return !str.replace(re, ''); + }, + /** * Display a prompt from an error with custom buttons and a callback */ diff --git a/test/tests/utilities_internalTest.js b/test/tests/utilities_internalTest.js index ba1fbea59d..63df823e09 100644 --- a/test/tests/utilities_internalTest.js +++ b/test/tests/utilities_internalTest.js @@ -68,6 +68,25 @@ describe("Zotero.Utilities.Internal", function () { }); + describe("#isOnlyEmoji()", function () { + it("should return true for emoji", function () { + assert.isTrue(Zotero.Utilities.Internal.isOnlyEmoji("🐩")); + }); + + it("should return true for emoji with text representation that use Variation Selector-16", function () { + assert.isTrue(Zotero.Utilities.Internal.isOnlyEmoji("⭐️")); + }); + + it("should return true for emoji made up of multiple characters with ZWJ", function () { + assert.isTrue(Zotero.Utilities.Internal.isOnlyEmoji("👨‍🌾")); + }); + + it("should return false for integer", function () { + assert.isFalse(Zotero.Utilities.Internal.isOnlyEmoji("0")); + }); + }); + + describe("#delayGenerator", function () { var spy;