emoji: Abstract all name_to_codepoint, codepoint_to_name accesses.

Computed indexes into these raw objects should be guarded with
Object.prototype.hasOwnProperty; make our accessors do this
automatically and use them consistently.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2020-05-26 19:17:29 -07:00
committed by Tim Abbott
parent e5e1a05e74
commit 080abf4a1e
6 changed files with 51 additions and 68 deletions

View File

@@ -32,6 +32,7 @@ set_global('$', global.make_zjquery());
$.fn.keydown = noop;
$.fn.keypress = noop;
zrequire('emoji');
const hotkey = zrequire('hotkey');
zrequire('common');

View File

@@ -1,51 +1,35 @@
set_global('document', 'document-stub');
set_global('$', global.make_zjquery());
zrequire('emoji');
zrequire('people');
zrequire('reactions');
set_global('emoji', {
all_realm_emojis: new Map(Object.entries({
set_global('page_params', {
user_id: 5,
realm_emoji: {
991: {
id: '991',
emoji_name: 'realm_emoji',
emoji_url: 'TBD',
name: 'realm_emoji',
source_url: 'TBD',
deactivated: false,
},
992: {
id: '992',
emoji_name: 'inactive_realm_emoji',
emoji_url: 'TBD',
name: 'inactive_realm_emoji',
source_url: 'TBD',
deactivated: true,
},
zulip: {
id: 'zulip',
emoji_name: 'zulip',
emoji_url: 'TBD',
name: 'zulip',
source_url: 'TBD',
deactivated: false,
},
})),
active_realm_emojis: new Map(Object.entries({
realm_emoji: {
id: '991',
emoji_name: 'realm_emoji',
emoji_url: 'TBD',
},
zulip: {
id: 'zulip',
emoji_name: 'zulip',
emoji_url: 'TBD',
},
})),
deactivated_realm_emojis: {
inactive_realm_emoji: {
emoji_name: 'inactive_realm_emoji',
emoji_url: 'TBD',
},
},
});
set_global('page_params', {user_id: 5});
emoji.initialize();
set_global('channel', {});
set_global('emoji_picker', {

View File

@@ -35,16 +35,13 @@ const emoticon_translations = (() => {
*/
const translations = [];
for (const emoticon in emoji_codes.emoticon_conversions) {
if (emoji_codes.emoticon_conversions.hasOwnProperty(emoticon)) {
const replacement_text = emoji_codes.emoticon_conversions[emoticon];
const regex = new RegExp('(' + util.escape_regexp(emoticon) + ')', 'g');
for (const [emoticon, replacement_text] of Object.entries(emoji_codes.emoticon_conversions)) {
const regex = new RegExp('(' + util.escape_regexp(emoticon) + ')', 'g');
translations.push({
regex: regex,
replacement_text: replacement_text,
});
}
translations.push({
regex: regex,
replacement_text: replacement_text,
});
}
return translations;
@@ -60,12 +57,16 @@ const zulip_emoji = {
exports.get_emoji_name = (codepoint) => {
// get_emoji_name('1f384') === 'holiday_tree'
return emoji_codes.codepoint_to_name[codepoint];
if (Object.prototype.hasOwnProperty.call(emoji_codes.codepoint_to_name, codepoint)) {
return emoji_codes.codepoint_to_name[codepoint];
}
};
exports.get_emoji_codepoint = (emoji_name) => {
// get_emoji_codepoint('avocado') === '1f951'
return emoji_codes.name_to_codepoint[emoji_name];
if (Object.prototype.hasOwnProperty.call(emoji_codes.name_to_codepoint, emoji_name)) {
return emoji_codes.name_to_codepoint[emoji_name];
}
};
exports.get_realm_emoji_url = (emoji_name) => {
@@ -147,19 +148,17 @@ exports.build_emoji_data = function (realm_emojis) {
for (const codepoints of Object.values(emoji_codes.emoji_catalog)) {
for (const codepoint of codepoints) {
if (emoji_codes.codepoint_to_name.hasOwnProperty(codepoint)) {
const emoji_name = exports.get_emoji_name(codepoint);
if (!exports.emojis_by_name.has(emoji_name)) {
const emoji_dict = {
name: emoji_name,
display_name: emoji_name,
aliases: exports.default_emoji_aliases.get(codepoint),
is_realm_emoji: false,
emoji_code: codepoint,
has_reacted: false,
};
exports.emojis_by_name.set(emoji_name, emoji_dict);
}
const emoji_name = exports.get_emoji_name(codepoint);
if (emoji_name !== undefined && !exports.emojis_by_name.has(emoji_name)) {
const emoji_dict = {
name: emoji_name,
display_name: emoji_name,
aliases: exports.default_emoji_aliases.get(codepoint),
is_realm_emoji: false,
emoji_code: codepoint,
has_reacted: false,
};
exports.emojis_by_name.set(emoji_name, emoji_dict);
}
}
}
@@ -169,11 +168,11 @@ exports.get_canonical_name = function (emoji_name) {
if (exports.active_realm_emojis.has(emoji_name)) {
return emoji_name;
}
if (!emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) {
const codepoint = exports.get_emoji_codepoint(emoji_name);
if (codepoint === undefined) {
blueslip.error("Invalid emoji name: " + emoji_name);
return;
}
const codepoint = exports.get_emoji_codepoint(emoji_name);
return exports.get_emoji_name(codepoint);
};

View File

@@ -109,10 +109,9 @@ exports.generate_emoji_picker_data = function (realm_emojis) {
for (const [category, codepoints] of Object.entries(emoji_codes.emoji_catalog)) {
const emojis = [];
for (const codepoint of codepoints) {
if (emoji_codes.codepoint_to_name.hasOwnProperty(codepoint)) {
const emoji_dict = emoji.emojis_by_name.get(
emoji_codes.codepoint_to_name[codepoint]
);
const name = emoji.get_emoji_name(codepoint);
if (name !== undefined) {
const emoji_dict = emoji.emojis_by_name.get(name);
if (emoji_dict !== undefined && emoji_dict.is_realm_emoji !== true) {
emojis.push(emoji_dict);
}
@@ -123,8 +122,9 @@ exports.generate_emoji_picker_data = function (realm_emojis) {
const popular = [];
for (const codepoint of typeahead.popular_emojis) {
if (emoji_codes.codepoint_to_name.hasOwnProperty(codepoint)) {
const emoji_dict = emoji.emojis_by_name.get(emoji_codes.codepoint_to_name[codepoint]);
const name = emoji.get_emoji_name(codepoint);
if (name !== undefined) {
const emoji_dict = emoji.emojis_by_name.get(name);
if (emoji_dict !== undefined) {
popular.push(emoji_dict);
}

View File

@@ -1,5 +1,3 @@
const emoji_codes = require("../generated/emoji/emoji_codes.json");
function do_narrow_action(action) {
action(current_msg_list.selected_id(), {trigger: 'hotkey'});
return true;
@@ -731,7 +729,7 @@ exports.process_hotkey = function (e, hotkey) {
case 'thumbs_up_emoji': { // '+': reacts with thumbs up emoji on selected message
// Use canonical name.
const thumbs_up_emoji_code = '1f44d';
const canonical_name = emoji_codes.codepoint_to_name[thumbs_up_emoji_code];
const canonical_name = emoji.get_emoji_name(thumbs_up_emoji_code);
reactions.toggle_emoji_reaction(msg.id, canonical_name);
return true;
}

View File

@@ -1,4 +1,3 @@
const emoji_codes = require("../generated/emoji/emoji_codes.json");
const render_message_reaction = require('../templates/message_reaction.hbs');
exports.view = {}; // function namespace
@@ -103,12 +102,14 @@ exports.toggle_emoji_reaction = function (message_id, emoji_name) {
reaction_info.reaction_type = 'realm_emoji';
}
reaction_info.emoji_code = emoji.active_realm_emojis.get(emoji_name).id;
} else if (emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) {
reaction_info.reaction_type = 'unicode_emoji';
reaction_info.emoji_code = emoji_codes.name_to_codepoint[emoji_name];
} else {
blueslip.warn('Bad emoji name: ' + emoji_name);
return;
const codepoint = emoji.get_emoji_codepoint(emoji_name);
if (codepoint === undefined) {
blueslip.warn('Bad emoji name: ' + emoji_name);
return;
}
reaction_info.reaction_type = 'unicode_emoji';
reaction_info.emoji_code = codepoint;
}
update_ui_and_send_reaction_ajax(message_id, reaction_info);