From d188b3526ab45f989e09f2a1c1f6e7a7eacf1605 Mon Sep 17 00:00:00 2001
From: Sheogorath <sheogorath@shivering-isles.com>
Date: Wed, 31 Oct 2018 15:12:34 +0100
Subject: [PATCH] Again: Replace emoji-plugin regex

The Regex introduced in the last commit[1], was already working quite
good. But still resulted in false positives for all URL that contained a
second `:`.

To fix this once and for all, we craft a simple, but long regex based on
all emoji names and use this to match them.

We could probably optimize it, but that should also be something the
regex engine itself can and should do.

[1]: 7e45533c75a3697c916e52e5f4ddff42a38bd3d5 (in this source tree)

Signed-off-by: Sheogorath <sheogorath@shivering-isles.com>
---
 public/js/extra.js | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/public/js/extra.js b/public/js/extra.js
index 4db36ff..ed1470b 100644
--- a/public/js/extra.js
+++ b/public/js/extra.js
@@ -1147,15 +1147,14 @@ const pdfPlugin = new Plugin(
 
 const emojijsPlugin = new Plugin(
     // regexp to match emoji shortcodes :something:
-    /:([^\s:]+):/,
+    // We generate an universal regex that guaranteed only contains the
+    // emojies we have available. This should prevent all false-positives
+    new RegExp(':(' + window.emojify.emojiNames.map((item) => { return RegExp.escape(item) }).join('|') + '):', 'i'),
 
     (match, utils) => {
-      const emoji = match[1] ? match[1].toLowerCase() : undefined
-      if (window.emojify.emojiNames.includes(emoji)) {
-        const div = $(`<img class="emoji" src="${serverurl}/build/emojify.js/dist/images/basic/${emoji}.png"></img>`)
-        return div[0].outerHTML
-      }
-      return match[0]
+      const emoji = match[1].toLowerCase()
+      const div = $(`<img class="emoji" src="${serverurl}/build/emojify.js/dist/images/basic/${emoji}.png"></img>`)
+      return div[0].outerHTML
     }
 )