From 4cb8ac100a943e817db4cb487ebb30187a38ade7 Mon Sep 17 00:00:00 2001 From: Harshit Bansal Date: Tue, 20 Jun 2017 19:22:14 +0530 Subject: [PATCH] emoji: Fix the title and alt text for unicode emojis. Unicode emojis when rendered should display canonical short name. Similarly, the alt text should be of the format `::`. For both of these we currently display the actual unicode symbol. As some systems don't have the fonts necessary for displaying them properly, they are rendered as empty square blocks. This commit also ensures that the markup generated for emoji generated by canonical name and by an unicode emoji is same. Fixes: #5555. --- frontend_tests/node_tests/markdown.js | 2 +- static/js/markdown.js | 5 +++-- version.py | 2 +- zerver/fixtures/bugdown-data.json | 18 +++++++++--------- zerver/lib/bugdown/__init__.py | 10 +++++++--- zerver/tests/test_bugdown.py | 13 +++++++++++-- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/frontend_tests/node_tests/markdown.js b/frontend_tests/node_tests/markdown.js index 5afa1c5b32..c733ee84c4 100644 --- a/frontend_tests/node_tests/markdown.js +++ b/frontend_tests/node_tests/markdown.js @@ -221,7 +221,7 @@ var bugdown_data = JSON.parse(fs.readFileSync(path.join(__dirname, '../../zerver {input: 'This is an :poop: message', expected: '

This is an :poop: message

'}, {input: "\ud83d\udca9", - expected: '

\ud83d\udca9

'}, + expected: '

:poop:

'}, {input: '\u{1f937}', expected: '

\u{1f937}

' }, {input: 'This is a realm filter #1234 with text after it', diff --git a/static/js/markdown.js b/static/js/markdown.js index dcb843ed7f..b8ecfc9a84 100644 --- a/static/js/markdown.js +++ b/static/js/markdown.js @@ -118,9 +118,10 @@ function handleUnicodeEmoji(unicode_emoji) { var hex_value = unicode_emoji.codePointAt(0).toString(16); if (emoji.emojis_by_unicode.hasOwnProperty(hex_value)) { var emoji_url = emoji.emojis_by_unicode[hex_value]; - return '' + unicode_emoji + ''; + ' title="' + display_string + '">'; } return unicode_emoji; } diff --git a/version.py b/version.py index e56a0be889..ec54626880 100644 --- a/version.py +++ b/version.py @@ -1,2 +1,2 @@ ZULIP_VERSION = "1.6.0+git" -PROVISION_VERSION = '5.11' +PROVISION_VERSION = '5.12' diff --git a/zerver/fixtures/bugdown-data.json b/zerver/fixtures/bugdown-data.json index 8821134a94..13b74e6905 100644 --- a/zerver/fixtures/bugdown-data.json +++ b/zerver/fixtures/bugdown-data.json @@ -273,55 +273,55 @@ { "name": "unicode_emoji", "input": "\ud83d\udca9", - "expected_output":"

\"\ud83d\udca9\"<\/p>", + "expected_output":"

\":poop:\"<\/p>", "bugdown_matches_marked": true }, { "name": "two_unicode_emoji", "input": "\ud83d\udca9\ud83d\udca9", - "expected_output":"

\"\ud83d\udca9\"\"\ud83d\udca9\"<\/p>", + "expected_output":"

\":poop:\"\":poop:\"<\/p>", "bugdown_matches_marked": true }, { "name": "two_unicode_emoji_separated_by_text", "input": "\ud83d\udca9 word \ud83d\udca9", - "expected_output":"

\"\ud83d\udca9\" word \"\ud83d\udca9\"<\/p>", + "expected_output":"

\":poop:\" word \":poop:\"<\/p>", "bugdown_matches_marked": true }, { "name": "miscellaneous_symbols_and_pictographs", "input": "Merry Christmas!!\ud83c\udf84", - "expected_output":"

Merry Christmas!!\"\ud83c\udf84\"<\/p>", + "expected_output":"

Merry Christmas!!\":christmas_tree:\"<\/p>", "bugdown_matches_marked": true }, { "name": "miscellaneous_and_dingbats_emoji", "input": "\u2693\u2797", - "expected_output":"

\"\u2693\"\"\u2797\"<\/p>", + "expected_output":"

\":anchor:\"\":heavy_division_sign:\"<\/p>", "bugdown_matches_marked": true }, { "name": "supplemental_symbols_and_pictographs", "input": "I am a robot \ud83e\udd16.", - "expected_output":"

I am a robot \"\ud83e\udd16\".<\/p>", + "expected_output":"

I am a robot \":robot_face:\".<\/p>", "bugdown_matches_marked": true }, { "name": "miscellaneous_symbols_and_arrows", "input": "Black upward arrow \u2b06", - "expected_output":"

Black upward arrow \"\u2b06\"<\/p>", + "expected_output":"

Black upward arrow \":arrow_up:\"<\/p>", "bugdown_matches_marked": true }, { "name": "unicode_emoji_without_space", "input": "Extra\ud83d\udc7dTerrestrial", - "expected_output":"

Extra\"\ud83d\udc7d\"Terrestrial<\/p>", + "expected_output":"

Extra\":alien:\"Terrestrial<\/p>", "bugdown_matches_marked": true }, { "name": "unicode_emojis_new_line", "input": "\ud83d\udc7d\n\ud83d\udc7d", - "expected_output":"

\"\ud83d\udc7d\"
\n\"\ud83d\udc7d\"<\/p>", + "expected_output":"

\":alien:\"
\n\":alien:\"<\/p>", "bugdown_matches_marked": true }, { diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.py index d832485830..f60c51501c 100644 --- a/zerver/lib/bugdown/__init__.py +++ b/zerver/lib/bugdown/__init__.py @@ -700,7 +700,10 @@ class Avatar(markdown.inlinepatterns.Pattern): path_to_name_to_codepoint = os.path.join(settings.STATIC_ROOT, "generated", "emoji", "name_to_codepoint.json") name_to_codepoint = ujson.load(open(path_to_name_to_codepoint)) -unicode_emoji_list = set([name_to_codepoint[name] for name in name_to_codepoint]) + +path_to_codepoint_to_name = os.path.join(settings.STATIC_ROOT, "generated", "emoji", "codepoint_to_name.json") +with open(path_to_codepoint_to_name) as codepoint_to_name_file: + codepoint_to_name = ujson.load(codepoint_to_name_file) # All of our emojis(non ZWJ sequences) belong to one of these unicode blocks: # \U0001f100-\U0001f1ff - Enclosed Alphanumeric Supplement @@ -780,8 +783,9 @@ class UnicodeEmoji(markdown.inlinepatterns.Pattern): # type: (Match[Text]) -> Optional[Element] orig_syntax = match.group('syntax') codepoint = unicode_emoji_to_codepoint(orig_syntax) - if codepoint in unicode_emoji_list: - return make_emoji(codepoint, orig_syntax) + if codepoint in codepoint_to_name: + display_string = ':' + codepoint_to_name[codepoint] + ':' + return make_emoji(codepoint, display_string) else: return None diff --git a/zerver/tests/test_bugdown.py b/zerver/tests/test_bugdown.py index e1c6a28df0..95364f4f7d 100644 --- a/zerver/tests/test_bugdown.py +++ b/zerver/tests/test_bugdown.py @@ -528,11 +528,20 @@ class BugdownTest(ZulipTestCase): # type: () -> None msg = u'\u2615' # ☕ converted = bugdown_convert(msg) - self.assertEqual(converted, u'

\u2615

') + self.assertEqual(converted, u'

:coffee:

') msg = u'\u2615\u2615' # ☕☕ converted = bugdown_convert(msg) - self.assertEqual(converted, u'

\u2615\u2615

') + self.assertEqual(converted, u'

:coffee::coffee:

') + + def test_same_markup(self): + # type: () -> None + msg = u'\u2615' # ☕ + unicode_converted = bugdown_convert(msg) + + msg = u':coffee:' # ☕☕ + converted = bugdown_convert(msg) + self.assertEqual(converted, unicode_converted) def test_realm_patterns(self): # type: () -> None