-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Fix: Size of the single emoji in chat along with updated Emoji Regex #5039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f0273ce
fix(emoji-size-in-chat): Moved regex to a different function
mananjadhav 41d5ecf
fix(emoji-size-in-chat): Convert emoji to Unicode and compare
mananjadhav 800e002
fix(emoji-size-in-chat): Replaced Emoji Regex
mananjadhav 8fe203d
fix(emoji-size-in-chat): Handled skin-tone support
mananjadhav 1da613a
fix(emoji-size-in-chat): wip: Added test for emoji regex
mananjadhav 87a29b2
fix(emoji-size-in-chat): Update emoji regex test and pending emoji range
mananjadhav 1f6354b
fix(emoji-size-in-chat): Added skintone emoji in test
mananjadhav c07557b
fix(emoji-size-in-chat): Hardcoded emoji variants to test against the…
mananjadhav cb3a584
fix(emoji-size-in-chat): Removed unused import
mananjadhav c6e1d9a
fix(emoji-size-in-chat): Replace function with inline regex
mananjadhav 09a56b5
fix(emoji-size-in-chat): Merged function into one
mananjadhav 73655c7
Merge branch 'main' of https://github.com/mananjadhav/App into fix/si…
mananjadhav 031191e
fix(emoji-size-in-chat): Merged all Emoji functions into EmojiUtils
mananjadhav b3322b6
fix(emoji-size-in-chat): Added comments on surrogate and changed to _…
mananjadhav 09babe0
fix(emoji-size-in-chat): Updated test documentation
mananjadhav 5d75570
Merge branch 'main' of https://github.com/mananjadhav/App into fix/si…
mananjadhav 22ca1d7
fix(emoji-size-in-chat): Added additional skull emoji from previous r…
mananjadhav 52d9827
fix(emoji-size-in-chat): Added radix to parseInt
mananjadhav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import _ from 'underscore'; | ||
| import CONST from '../CONST'; | ||
|
|
||
| /** | ||
| * Get the unicode code of an emoji in base 16. | ||
| * @param {String} input | ||
| * @returns {String} | ||
| */ | ||
| function getEmojiUnicode(input) { | ||
| if (input.length === 0) { | ||
| return ''; | ||
| } | ||
|
|
||
| if (input.length === 1) { | ||
| return _.map(input.charCodeAt(0).toString().split(' '), val => parseInt(val, 10).toString(16)).join(' '); | ||
| } | ||
|
|
||
| const pairs = []; | ||
|
|
||
| // Some Emojis in UTF-16 are stored as pair of 2 Unicode characters (eg Flags) | ||
| // The first char is generally between the range U+D800 to U+DBFF called High surrogate | ||
| // & the second char between the range U+DC00 to U+DFFF called low surrogate | ||
| // More info in the following links: | ||
| // 1. https://docs.microsoft.com/en-us/windows/win32/intl/surrogates-and-supplementary-characters | ||
| // 2. https://thekevinscott.com/emojis-in-javascript/ | ||
| for (let i = 0; i < input.length; i++) { | ||
| if (input.charCodeAt(i) >= 0xd800 && input.charCodeAt(i) <= 0xdbff) { // high surrogate | ||
| if (input.charCodeAt(i + 1) >= 0xdc00 && input.charCodeAt(i + 1) <= 0xdfff) { // low surrogate | ||
| pairs.push( | ||
| ((input.charCodeAt(i) - 0xd800) * 0x400) | ||
| + (input.charCodeAt(i + 1) - 0xdc00) + 0x10000, | ||
| ); | ||
| } | ||
| } else if (input.charCodeAt(i) < 0xd800 || input.charCodeAt(i) > 0xdfff) { | ||
| // modifiers and joiners | ||
| pairs.push(input.charCodeAt(i)); | ||
| } | ||
| } | ||
| return _.map(pairs, val => parseInt(val, 10).toString(16)).join(' '); | ||
| } | ||
|
|
||
| /** | ||
| * Function to remove Skin Tone and utf16 surrogates from Emoji | ||
| * @param {String} emojiCode | ||
| * @returns {String} | ||
| */ | ||
| function trimEmojiUnicode(emojiCode) { | ||
| return emojiCode.replace(/(fe0f|1f3fb|1f3fc|1f3fd|1f3fe|1f3ff)$/, '').trim(); | ||
| } | ||
|
|
||
| /** | ||
| * Validates that this string is composed of a single emoji | ||
| * | ||
| * @param {String} message | ||
| * @returns {Boolean} | ||
| */ | ||
| function isSingleEmoji(message) { | ||
| const match = message.match(CONST.REGEX.EMOJIS); | ||
|
|
||
| if (!match) { | ||
| return false; | ||
| } | ||
|
|
||
| const matchedEmoji = match[0]; | ||
| const matchedUnicode = getEmojiUnicode(matchedEmoji); | ||
| const currentMessageUnicode = trimEmojiUnicode(getEmojiUnicode(message)); | ||
| return matchedUnicode === currentMessageUnicode; | ||
| } | ||
|
|
||
|
|
||
| export { | ||
| getEmojiUnicode, | ||
| trimEmojiUnicode, | ||
| isSingleEmoji, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import _ from 'underscore'; | ||
| import Emoji from '../../assets/emojis'; | ||
| import CONST from '../../src/CONST'; | ||
| import {isSingleEmoji} from '../../src/libs/EmojiUtils'; | ||
|
|
||
| describe('EmojiRegexTest', () => { | ||
| it('matches all the emojis in the list', () => { | ||
| // Given the set of Emojis available in the application | ||
| const emojiMatched = _.every(Emoji, (emoji) => { | ||
| if (emoji.header === true || emoji.code === CONST.EMOJI_SPACER) { | ||
| return true; | ||
| } | ||
|
|
||
| // When we match every Emoji Code | ||
| const isEmojiMatched = isSingleEmoji(emoji.code); | ||
| let skinToneMatched = true; | ||
| if (emoji.types) { | ||
| // and every skin tone variant of the Emoji code | ||
| skinToneMatched = _.every(emoji.types, emojiWithSkinTone => isSingleEmoji(emojiWithSkinTone)); | ||
| } | ||
| return skinToneMatched && isEmojiMatched; | ||
| }); | ||
|
|
||
| // Then it should return true for every Emoji Code | ||
| expect(emojiMatched).toBe(true); | ||
| }); | ||
|
|
||
| it('matches single emojis variants for size', () => { | ||
| // GIVEN an emoji that has the default Unicode representation WHEN we check if it's a single emoji THEN it should return true | ||
| expect(isSingleEmoji('👉')).toBe(true); | ||
| expect(isSingleEmoji('😪️')).toBe(true); | ||
| expect(isSingleEmoji('😎️')).toBe(true); | ||
|
|
||
| // GIVEN an emoji that different cross-platform variations WHEN we check if it's a single emoji THEN it should return true | ||
| expect(isSingleEmoji('🔫️')).toBe(true); | ||
| expect(isSingleEmoji('🛍')).toBe(true); | ||
| expect(isSingleEmoji('🕍')).toBe(true); | ||
|
|
||
| // GIVEN an emoji that is symbol/numerical WHEN we check if it's a single emoji THEN it should return true | ||
| expect(isSingleEmoji('*️⃣')).toBe(true); | ||
| expect(isSingleEmoji('1️⃣️')).toBe(true); | ||
|
|
||
| // GIVEN an emoji that has text-variant WHEN we check if it's a single emoji THEN it should return true | ||
| expect(isSingleEmoji('❤️')).toBe(true); | ||
| expect(isSingleEmoji('⁉️')).toBe(true); | ||
| expect(isSingleEmoji('✳️')).toBe(true); | ||
| expect(isSingleEmoji('☠️')).toBe(true); | ||
|
|
||
|
|
||
| // GIVEN an emoji that has skin tone attached WHEN we check if it's a single emoji THEN it should return true | ||
| expect(isSingleEmoji('👶🏽')).toBe(true); | ||
| expect(isSingleEmoji('👩🏾')).toBe(true); | ||
| expect(isSingleEmoji('👊🏾')).toBe(true); | ||
|
|
||
| // GIVEN an emoji that is composite(family) with 4+ unicode pairs WHEN we check if it's a single emoji THEN it should return true | ||
| expect(isSingleEmoji('👨👩👦️')).toBe(true); | ||
| expect(isSingleEmoji('👩👩👧👦️')).toBe(true); | ||
|
|
||
| // GIVEN an emoji that has a length of 2 (flags) WHEN we check if it's a single emoji THEN it should return true | ||
| expect(isSingleEmoji('🇺🇲')).toBe(true); | ||
| expect(isSingleEmoji('🇮🇳')).toBe(true); | ||
| expect(isSingleEmoji('🇺🇦️')).toBe(true); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.