From 37c613dde21760a0ddd7c76d964fefc8784c7106 Mon Sep 17 00:00:00 2001 From: Tim Lange Date: Fri, 15 Mar 2019 05:44:49 +0100 Subject: [PATCH] FIX: Outsourced erb part from emoji.js.es6 (#7168) --- app/assets/javascripts/pretty-text-bundle.js | 1 + .../{emoji.js.es6.erb => emoji.js.es6} | 67 +++++++++++++------ .../pretty-text/emoji/version.js.es6.erb | 2 + .../acceptance/emoji-picker-test.js.es6 | 2 +- test/javascripts/acceptance/emoji-test.js.es6 | 2 +- test/javascripts/acceptance/topic-test.js.es6 | 2 +- test/javascripts/lib/emoji-test.js.es6 | 3 +- test/javascripts/lib/pretty-text-test.js.es6 | 2 +- test/javascripts/models/topic-test.js.es6 | 2 +- 9 files changed, 57 insertions(+), 26 deletions(-) rename app/assets/javascripts/pretty-text/{emoji.js.es6.erb => emoji.js.es6} (65%) create mode 100644 app/assets/javascripts/pretty-text/emoji/version.js.es6.erb diff --git a/app/assets/javascripts/pretty-text-bundle.js b/app/assets/javascripts/pretty-text-bundle.js index 41324ba31e5..ed5331b6435 100644 --- a/app/assets/javascripts/pretty-text-bundle.js +++ b/app/assets/javascripts/pretty-text-bundle.js @@ -2,6 +2,7 @@ //= require ./pretty-text/guid //= require ./pretty-text/censored-words //= require ./pretty-text/emoji/data +//= require ./pretty-text/emoji/version //= require ./pretty-text/emoji //= require ./pretty-text/engines/discourse-markdown-it //= require xss.min diff --git a/app/assets/javascripts/pretty-text/emoji.js.es6.erb b/app/assets/javascripts/pretty-text/emoji.js.es6 similarity index 65% rename from app/assets/javascripts/pretty-text/emoji.js.es6.erb rename to app/assets/javascripts/pretty-text/emoji.js.es6 index 09ab4ce5ed6..5b48a649e4e 100644 --- a/app/assets/javascripts/pretty-text/emoji.js.es6.erb +++ b/app/assets/javascripts/pretty-text/emoji.js.es6 @@ -1,7 +1,11 @@ -import { emojis, aliases, searchAliases, translations, tonableEmojis } from 'pretty-text/emoji/data'; - -// bump up this number to expire all emojis -export const IMAGE_VERSION = "<%= Emoji::EMOJI_VERSION %>"; +import { + emojis, + aliases, + searchAliases, + translations, + tonableEmojis +} from "pretty-text/emoji/data"; +import { IMAGE_VERSION } from "pretty-text/emoji/version"; const extendedEmoji = {}; @@ -17,16 +21,18 @@ export function extendedEmojiList() { const emojiHash = {}; // add all default emojis -emojis.forEach(code => emojiHash[code] = true); +emojis.forEach(code => (emojiHash[code] = true)); // and their aliases const aliasHash = {}; Object.keys(aliases).forEach(name => { - aliases[name].forEach(alias => aliasHash[alias] = name); + aliases[name].forEach(alias => (aliasHash[alias] = name)); }); export function performEmojiUnescape(string, opts) { - if (!string) { return; } + if (!string) { + return; + } // this can be further improved by supporting matches of emoticons that don't begin with a colon if (string.indexOf(":") >= 0) { @@ -35,10 +41,15 @@ export function performEmojiUnescape(string, opts) { const emojiVal = isEmoticon ? translations[m] : m.slice(1, m.length - 1); const hasEndingColon = m.lastIndexOf(":") === m.length - 1; const url = buildEmojiUrl(emojiVal, opts); - const classes = isCustomEmoji(emojiVal, opts) ? "emoji emoji-custom" : "emoji"; + const classes = isCustomEmoji(emojiVal, opts) + ? "emoji emoji-custom" + : "emoji"; - return url && (isEmoticon || hasEndingColon) ? - `${emojiVal}` : m; + return url && (isEmoticon || hasEndingColon) + ? `${emojiVal}` + : m; }); } @@ -48,7 +59,8 @@ export function performEmojiUnescape(string, opts) { export function isCustomEmoji(code, opts) { code = code.toLowerCase(); if (extendedEmoji.hasOwnProperty(code)) return true; - if (opts && opts.customEmoji && opts.customEmoji.hasOwnProperty(code)) return true; + if (opts && opts.customEmoji && opts.customEmoji.hasOwnProperty(code)) + return true; return false; } @@ -64,8 +76,15 @@ export function buildEmojiUrl(code, opts) { } const noToneMatch = code.match(/([^:]+):?/); - if (noToneMatch && !url && (emojiHash.hasOwnProperty(noToneMatch[1]) || aliasHash.hasOwnProperty(noToneMatch[1]))) { - url = opts.getURL(`/images/emoji/${opts.emojiSet}/${code.replace(/:t/, '/')}.png`); + if ( + noToneMatch && + !url && + (emojiHash.hasOwnProperty(noToneMatch[1]) || + aliasHash.hasOwnProperty(noToneMatch[1])) + ) { + url = opts.getURL( + `/images/emoji/${opts.emojiSet}/${code.replace(/:t/, "/")}.png` + ); } if (url) { @@ -77,15 +96,23 @@ export function buildEmojiUrl(code, opts) { export function emojiExists(code) { code = code.toLowerCase(); - return !!(extendedEmoji.hasOwnProperty(code) || emojiHash.hasOwnProperty(code) || aliasHash.hasOwnProperty(code)); -}; + return !!( + extendedEmoji.hasOwnProperty(code) || + emojiHash.hasOwnProperty(code) || + aliasHash.hasOwnProperty(code) + ); +} let toSearch; export function emojiSearch(term, options) { const maxResults = (options && options["maxResults"]) || -1; - if (maxResults === 0) { return []; } + if (maxResults === 0) { + return []; + } - toSearch = toSearch || _.union(_.keys(emojiHash), _.keys(extendedEmoji), _.keys(aliasHash)).sort(); + toSearch = + toSearch || + _.union(_.keys(emojiHash), _.keys(extendedEmoji), _.keys(aliasHash)).sort(); const results = []; @@ -97,7 +124,7 @@ export function emojiSearch(term, options) { } // if term matches from beginning - for (let i=0; i 0) addResult(item); } @@ -116,7 +143,7 @@ export function emojiSearch(term, options) { } else { return results.slice(0, maxResults); } -}; +} export function isSkinTonableEmoji(term) { const match = _.compact(term.split(":"))[0]; diff --git a/app/assets/javascripts/pretty-text/emoji/version.js.es6.erb b/app/assets/javascripts/pretty-text/emoji/version.js.es6.erb new file mode 100644 index 00000000000..b846ca59d55 --- /dev/null +++ b/app/assets/javascripts/pretty-text/emoji/version.js.es6.erb @@ -0,0 +1,2 @@ +// bump up this number to expire all emojis +export const IMAGE_VERSION = "<%= Emoji::EMOJI_VERSION %>"; diff --git a/test/javascripts/acceptance/emoji-picker-test.js.es6 b/test/javascripts/acceptance/emoji-picker-test.js.es6 index 3c2ebe7be4b..9122cd8ea5b 100644 --- a/test/javascripts/acceptance/emoji-picker-test.js.es6 +++ b/test/javascripts/acceptance/emoji-picker-test.js.es6 @@ -1,5 +1,5 @@ import { acceptance } from "helpers/qunit-helpers"; -import { IMAGE_VERSION as v } from "pretty-text/emoji"; +import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; import { resetCache } from "discourse/components/emoji-picker"; acceptance("EmojiPicker", { diff --git a/test/javascripts/acceptance/emoji-test.js.es6 b/test/javascripts/acceptance/emoji-test.js.es6 index 48ac7f9bcc4..39dc37696e6 100644 --- a/test/javascripts/acceptance/emoji-test.js.es6 +++ b/test/javascripts/acceptance/emoji-test.js.es6 @@ -1,4 +1,4 @@ -import { IMAGE_VERSION as v } from "pretty-text/emoji"; +import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; import { acceptance } from "helpers/qunit-helpers"; acceptance("Emoji", { loggedIn: true }); diff --git a/test/javascripts/acceptance/topic-test.js.es6 b/test/javascripts/acceptance/topic-test.js.es6 index c38e5743452..b5f5893990f 100644 --- a/test/javascripts/acceptance/topic-test.js.es6 +++ b/test/javascripts/acceptance/topic-test.js.es6 @@ -1,5 +1,5 @@ import { acceptance } from "helpers/qunit-helpers"; -import { IMAGE_VERSION as v } from "pretty-text/emoji"; +import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; acceptance("Topic", { loggedIn: true, diff --git a/test/javascripts/lib/emoji-test.js.es6 b/test/javascripts/lib/emoji-test.js.es6 index 47665a8c8ae..c3b8ccd5630 100644 --- a/test/javascripts/lib/emoji-test.js.es6 +++ b/test/javascripts/lib/emoji-test.js.es6 @@ -1,4 +1,5 @@ -import { emojiSearch, IMAGE_VERSION as v } from "pretty-text/emoji"; +import { emojiSearch } from "pretty-text/emoji"; +import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; import { emojiUnescape } from "discourse/lib/text"; QUnit.module("lib:emoji"); diff --git a/test/javascripts/lib/pretty-text-test.js.es6 b/test/javascripts/lib/pretty-text-test.js.es6 index 162ef49691f..9495122a87c 100644 --- a/test/javascripts/lib/pretty-text-test.js.es6 +++ b/test/javascripts/lib/pretty-text-test.js.es6 @@ -1,7 +1,7 @@ import Quote from "discourse/lib/quote"; import Post from "discourse/models/post"; import { default as PrettyText, buildOptions } from "pretty-text/pretty-text"; -import { IMAGE_VERSION as v } from "pretty-text/emoji"; +import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; import { INLINE_ONEBOX_LOADING_CSS_CLASS } from "pretty-text/inline-oneboxer"; QUnit.module("lib:pretty-text"); diff --git a/test/javascripts/models/topic-test.js.es6 b/test/javascripts/models/topic-test.js.es6 index ca6bd1f7b53..e385e1e92f7 100644 --- a/test/javascripts/models/topic-test.js.es6 +++ b/test/javascripts/models/topic-test.js.es6 @@ -1,4 +1,4 @@ -import { IMAGE_VERSION as v } from "pretty-text/emoji"; +import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; QUnit.module("model:topic");