FIX: Don't convert :) into Emoji when emojis or emoji shurtcuts are disabled

FIX: Don’t convert :slight_smile: into Emoji when emojis or emoji shurtcuts are disabled

diff --git a/app/assets/javascripts/discourse/lib/text.js.es6 b/app/assets/javascripts/discourse/lib/text.js.es6
index 6986700..ee51cb5 100644
--- a/app/assets/javascripts/discourse/lib/text.js.es6
+++ b/app/assets/javascripts/discourse/lib/text.js.es6
@@ -60,17 +60,24 @@ function createPrettyText(options) {
 }
 
 function emojiOptions() {
-  const siteSettings = Discourse.__container__.lookup("site-settings:main");
-  if (!siteSettings.enable_emoji) {
+  if (!Discourse.SiteSettings.enable_emoji) {
     return;
   }
 
-  return { getURL: Discourse.getURLWithCDN, emojiSet: siteSettings.emoji_set };
+  return {
+    getURL: Discourse.getURLWithCDN,
+    emojiSet: Discourse.SiteSettings.emoji_set,
+    enableEmojiShortcuts: Discourse.SiteSettings.enable_emoji_shortcuts
+  };
 }
 
 export function emojiUnescape(string, options) {
-  const opts = _.extend(emojiOptions(), options || {});
-  return opts ? performEmojiUnescape(string, opts) : string;
+  const opts = emojiOptions();
+  if (opts) {
+    return performEmojiUnescape(string, Object.assign(opts, options || {}));
+  } else {
+    return string;
+  }
 }
 
 export function emojiUrlFor(code) {
diff --git a/app/assets/javascripts/pretty-text/emoji.js.es6 b/app/assets/javascripts/pretty-text/emoji.js.es6
index 49c5f15..6e73be9 100644
--- a/app/assets/javascripts/pretty-text/emoji.js.es6
+++ b/app/assets/javascripts/pretty-text/emoji.js.es6
@@ -44,7 +44,7 @@ export function performEmojiUnescape(string, opts) {
   }
 
   return string.replace(unicodeRegexp, m => {
-    const isEmoticon = !!translations[m];
+    const isEmoticon = opts.enableEmojiShortcuts && !!translations[m];
     const isUnicodeEmoticon = !!replacements[m];
     let emojiVal;
     if (isEmoticon) {
@@ -70,12 +70,12 @@ export function performEmojiUnescape(string, opts) {
   return string;
 }
 
-export function performEmojiEscape(string) {
+export function performEmojiEscape(string, opts) {
   return string.replace(unicodeRegexp, m => {
     if (!!translations[m]) {
-      return ":" + translations[m] + ":";
+      return opts.emojiShortcuts ? `:${translations[m]}:` : m;
     } else if (!!replacements[m]) {
-      return ":" + replacements[m] + ":";
+      return `:${replacements[m]}:`;
     } else {
       return m;
     }
diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6
index 9b851d2..e1c9a31 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6
@@ -119,7 +119,8 @@ const rule = {
           if (options.enableEmoji) {
             title = performEmojiUnescape(topicInfo.title, {
               getURL: options.getURL,
-              emojiSet: options.emojiSet
+              emojiSet: options.emojiSet,
+              enableEmojiShortcuts: options.enableEmojiShortcuts
             });
           }
 
@@ -154,6 +155,7 @@ export function setup(helper) {
   helper.registerOptions((opts, siteSettings) => {
     opts.enableEmoji = siteSettings.enable_emoji;
     opts.emojiSet = siteSettings.emoji_set;
+    opts.enableEmojiShortcuts = siteSettings.enable_emoji_shortcuts;
   });
 
   helper.registerPlugin(md => {
diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index 94d5d02..32063ad 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -235,7 +235,12 @@ module PrettyText
     protect do
       v8.eval(<<~JS)
         __paths = #{paths_json};
-        __performEmojiUnescape(#{title.inspect}, { getURL: __getURL, emojiSet: #{set}, customEmoji: #{custom} });
+        __performEmojiUnescape(#{title.inspect}, {
+          getURL: __getURL,
+          emojiSet: #{set},
+          customEmoji: #{custom},
+          enableEmojiShortcuts: #{SiteSetting.enable_emoji_shortcuts}
+        });
       JS
     end
   end
@@ -243,9 +248,11 @@ module PrettyText
   def self.escape_emoji(title)
     return unless title
 
+    replace_emoji_shortcuts = SiteSetting.enable_emoji && SiteSetting.enable_emoji_shortcuts
+
     protect do
       v8.eval(<<~JS)
-        __performEmojiEscape(#{title.inspect});
+        __performEmojiEscape(#{title.inspect}, { emojiShortcuts: #{replace_emoji_shortcuts} });
       JS
     end
   end
diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb
index 929c0c4..9ace739 100644
--- a/spec/components/pretty_text_spec.rb
+++ b/spec/components/pretty_text_spec.rb
@@ -47,6 +47,70 @@ describe PrettyText do
         expect(cook("[quote=\"EvilTrout, post:2, topic:#{topic.id}\"]\nddd\n[/quote]", topic_id: 1)).to eq(n(expected))
       end
 
+      context "emojis" do
+        let(:md) do
+          <<~MD
+          > This is a quote with a regular emoji :upside_down_face:
+
+          > This is a quote with an emoji shortcut :)
+
+          > This is a quote with a Unicode emoji 😎
+          MD
+        end
+
+        it "does not unescape emojis when emojis are disabled" do
+          SiteSetting.enable_emoji = false
+
+          html = <<~HTML
+            <blockquote>
+            <p>This is a quote with a regular emoji :upside_down_face:</p>
+            </blockquote>
+            <blockquote>
+            <p>This is a quote with an emoji shortcut :)</p>
+            </blockquote>
+            <blockquote>
+            <p>This is a quote with a Unicode emoji 😎</p>
+            </blockquote>
+          HTML
+
+          expect(cook(md)).to eq(html.strip)
+        end
+
+        it "does not convert emoji shortcuts when emoji shortcuts are disabled" do
+          SiteSetting.enable_emoji_shortcuts = false
+
+          html = <<~HTML
+            <blockquote>
+            <p>This is a quote with a regular emoji <img src="/images/emoji/twitter/upside_down_face.png?v=#{Emoji::EMOJI_VERSION}" title=":upside_down_face:" class="emoji" alt=":upside_down_face:"></p>
+            </blockquote>
+            <blockquote>
+            <p>This is a quote with an emoji shortcut :)</p>
+            </blockquote>
+            <blockquote>
+            <p>This is a quote with a Unicode emoji <img src="/images/emoji/twitter/sunglasses.png?v=#{Emoji::EMOJI_VERSION}" title=":sunglasses:" class="emoji" alt=":sunglasses:"></p>
+            </blockquote>
+          HTML
+
+          expect(cook(md)).to eq(html.strip)
+        end
+
+        it "unescapes all emojis" do
+          html = <<~HTML
+            <blockquote>
+            <p>This is a quote with a regular emoji <img src="/images/emoji/twitter/upside_down_face.png?v=#{Emoji::EMOJI_VERSION}" title=":upside_down_face:" class="emoji" alt=":upside_down_face:"></p>
+            </blockquote>
+            <blockquote>
+            <p>This is a quote with an emoji shortcut <img src="/images/emoji/twitter/slight_smile.png?v=#{Emoji::EMOJI_VERSION}" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
+            </blockquote>
+            <blockquote>
+            <p>This is a quote with a Unicode emoji <img src="/images/emoji/twitter/sunglasses.png?v=#{Emoji::EMOJI_VERSION}" title=":sunglasses:" class="emoji" alt=":sunglasses:"></p>
+            </blockquote>
+          HTML
+
+          expect(cook(md)).to eq(html.strip)
+        end
+      end
+
       it "do off topic quoting of posts from secure categories" do
         category = Fabricate(:category, read_restricted: true)
         topic = Fabricate(:topic, title: "this is topic with secret category", category: category)
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 8b6d441..98b5fb1 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -292,6 +292,7 @@ describe Topic do
     let(:topic_script) { build_topic_with_title("Topic with <script>alert('title')</script> script in its title") }
     let(:topic_emoji) { build_topic_with_title("I 💖 candy alot") }
     let(:topic_modifier_emoji) { build_topic_with_title("I 👨‍🌾 candy alot") }

[... diff too long, it was truncated ...]

GitHub sha: e2241000

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: