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

This commit is contained in:
Gerhard Schlager 2019-05-21 16:56:51 +02:00
parent e8799f0ba4
commit e224100023
8 changed files with 146 additions and 13 deletions

View File

@ -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) {

View File

@ -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;
}

View File

@ -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 => {

View File

@ -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

View File

@ -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)

View File

@ -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") }
let(:topic_shortcut_emoji) { build_topic_with_title("I love candy :)") }
it "escapes script contents" do
expect(topic_script.fancy_title).to eq("Topic with &lt;script&gt;alert(&lsquo;title&rsquo;)&lt;/script&gt; script in its title")
@ -320,6 +321,29 @@ describe Topic do
expect(topic_script.fancy_title).not_to include("<script>")
end
context "emoji shortcuts enabled" do
before { SiteSetting.enable_emoji_shortcuts = true }
it "converts emoji shortcuts into emoji" do
expect(topic_shortcut_emoji.fancy_title).to eq("I love candy :slight_smile:")
end
context "emojis disabled" do
before { SiteSetting.enable_emoji = false }
it "does not convert emoji shortcuts" do
expect(topic_shortcut_emoji.fancy_title).to eq("I love candy :)")
end
end
end
context "emoji shortcuts disabled" do
before { SiteSetting.enable_emoji_shortcuts = false }
it "does not convert emoji shortcuts" do
expect(topic_shortcut_emoji.fancy_title).to eq("I love candy :)")
end
end
end
context 'fancy title' do

View File

@ -91,6 +91,7 @@ Discourse.SiteSettingsOriginal = {
highlighted_languages:
"apache|bash|cs|cpp|css|coffeescript|diff|xml|http|ini|json|java|javascript|makefile|markdown|nginx|objectivec|ruby|perl|php|python|sql|handlebars",
enable_emoji: true,
enable_emoji_shortcuts: true,
emoji_set: "emoji_one",
desktop_category_page_style: "categories_and_latest_topics",
enable_mentions: true,

View File

@ -5,8 +5,18 @@ import { emojiUnescape } from "discourse/lib/text";
QUnit.module("lib:emoji");
QUnit.test("emojiUnescape", assert => {
const testUnescape = (input, expected, description) => {
const testUnescape = (input, expected, description, settings = {}) => {
const originalSettings = {};
for (const [key, value] of Object.entries(settings)) {
originalSettings[key] = Discourse.SiteSettings[key];
Discourse.SiteSettings[key] = value;
}
assert.equal(emojiUnescape(input), expected, description);
for (const [key, value] of Object.entries(originalSettings)) {
Discourse.SiteSettings[key] = value;
}
};
testUnescape(
@ -64,6 +74,24 @@ QUnit.test("emojiUnescape", assert => {
"hi :blonde_man:t6",
"end colon not optional for skin tones"
);
testUnescape(
"emoticons :)",
"emoticons :)",
"no emoticons when emojis are disabled",
{ enable_emoji: false }
);
testUnescape(
"emoji :smile:",
"emoji :smile:",
"no emojis when emojis are disabled",
{ enable_emoji: false }
);
testUnescape(
"emoticons :)",
"emoticons :)",
"no emoticons when emoji shortcuts are disabled",
{ enable_emoji_shortcuts: false }
);
});
QUnit.test("Emoji search", assert => {