From bfedcd1ff0b9585156ba8c256e931a9787f4492b Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 15 Sep 2017 12:03:32 +0100 Subject: [PATCH] Don't check for only-emoji if there were none We were still running the emojione regex on all messages to determine if they were *only* emoji (for the big emoji). This is pointless on messages that we already know don't have emoji. Also stop exporting unicodeToImage because it isn't used anywhere. --- src/HtmlUtils.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 4693394ef3..ee2bcd2b0f 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -58,10 +58,7 @@ export function containsEmoji(str) { /* modified from https://github.com/Ranks/emojione/blob/master/lib/js/emojione.js * because we want to include emoji shortnames in title text */ -export function unicodeToImage(str) { - // fast path - if (!containsEmoji(str)) return str; - +function unicodeToImage(str) { let replaceWith, unicode, alt, short, fname; const mappedUnicode = emojione.mapUnicodeToShort(); @@ -399,6 +396,8 @@ export function bodyToHtml(content, highlights, opts) { var isHtml = (content.format === "org.matrix.custom.html"); let body = isHtml ? content.formatted_body : escape(content.body); + let bodyHasEmoji = false; + var safeBody; // XXX: We sanitize the HTML whilst also highlighting its text nodes, to avoid accidentally trying // to highlight HTML tags themselves. However, this does mean that we don't highlight textnodes which @@ -416,16 +415,20 @@ export function bodyToHtml(content, highlights, opts) { }; } safeBody = sanitizeHtml(body, sanitizeHtmlParams); - safeBody = unicodeToImage(safeBody); + bodyHasEmoji = containsEmoji(body); + if (bodyHasEmoji) safeBody = unicodeToImage(safeBody); } finally { delete sanitizeHtmlParams.textFilter; } - EMOJI_REGEX.lastIndex = 0; - let contentBodyTrimmed = content.body !== undefined ? content.body.trim() : ''; - let match = EMOJI_REGEX.exec(contentBodyTrimmed); - let emojiBody = match && match[0] && match[0].length === contentBodyTrimmed.length; + let emojiBody = false; + if (bodyHasEmoji) { + EMOJI_REGEX.lastIndex = 0; + let contentBodyTrimmed = content.body !== undefined ? content.body.trim() : ''; + let match = EMOJI_REGEX.exec(contentBodyTrimmed); + emojiBody = match && match[0] && match[0].length === contentBodyTrimmed.length; + } const className = classNames({ 'mx_EventTile_body': true,