mirror of
https://github.com/discourse/discourse.git
synced 2025-03-22 23:35:38 +08:00
FIX: Don't convert :) into Emoji when emojis or emoji shurtcuts are disabled
This commit is contained in:
parent
e8799f0ba4
commit
e224100023
@ -60,17 +60,24 @@ function createPrettyText(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function emojiOptions() {
|
function emojiOptions() {
|
||||||
const siteSettings = Discourse.__container__.lookup("site-settings:main");
|
if (!Discourse.SiteSettings.enable_emoji) {
|
||||||
if (!siteSettings.enable_emoji) {
|
|
||||||
return;
|
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) {
|
export function emojiUnescape(string, options) {
|
||||||
const opts = _.extend(emojiOptions(), options || {});
|
const opts = emojiOptions();
|
||||||
return opts ? performEmojiUnescape(string, opts) : string;
|
if (opts) {
|
||||||
|
return performEmojiUnescape(string, Object.assign(opts, options || {}));
|
||||||
|
} else {
|
||||||
|
return string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function emojiUrlFor(code) {
|
export function emojiUrlFor(code) {
|
||||||
|
@ -44,7 +44,7 @@ export function performEmojiUnescape(string, opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return string.replace(unicodeRegexp, m => {
|
return string.replace(unicodeRegexp, m => {
|
||||||
const isEmoticon = !!translations[m];
|
const isEmoticon = opts.enableEmojiShortcuts && !!translations[m];
|
||||||
const isUnicodeEmoticon = !!replacements[m];
|
const isUnicodeEmoticon = !!replacements[m];
|
||||||
let emojiVal;
|
let emojiVal;
|
||||||
if (isEmoticon) {
|
if (isEmoticon) {
|
||||||
@ -70,12 +70,12 @@ export function performEmojiUnescape(string, opts) {
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function performEmojiEscape(string) {
|
export function performEmojiEscape(string, opts) {
|
||||||
return string.replace(unicodeRegexp, m => {
|
return string.replace(unicodeRegexp, m => {
|
||||||
if (!!translations[m]) {
|
if (!!translations[m]) {
|
||||||
return ":" + translations[m] + ":";
|
return opts.emojiShortcuts ? `:${translations[m]}:` : m;
|
||||||
} else if (!!replacements[m]) {
|
} else if (!!replacements[m]) {
|
||||||
return ":" + replacements[m] + ":";
|
return `:${replacements[m]}:`;
|
||||||
} else {
|
} else {
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
@ -119,7 +119,8 @@ const rule = {
|
|||||||
if (options.enableEmoji) {
|
if (options.enableEmoji) {
|
||||||
title = performEmojiUnescape(topicInfo.title, {
|
title = performEmojiUnescape(topicInfo.title, {
|
||||||
getURL: options.getURL,
|
getURL: options.getURL,
|
||||||
emojiSet: options.emojiSet
|
emojiSet: options.emojiSet,
|
||||||
|
enableEmojiShortcuts: options.enableEmojiShortcuts
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,6 +155,7 @@ export function setup(helper) {
|
|||||||
helper.registerOptions((opts, siteSettings) => {
|
helper.registerOptions((opts, siteSettings) => {
|
||||||
opts.enableEmoji = siteSettings.enable_emoji;
|
opts.enableEmoji = siteSettings.enable_emoji;
|
||||||
opts.emojiSet = siteSettings.emoji_set;
|
opts.emojiSet = siteSettings.emoji_set;
|
||||||
|
opts.enableEmojiShortcuts = siteSettings.enable_emoji_shortcuts;
|
||||||
});
|
});
|
||||||
|
|
||||||
helper.registerPlugin(md => {
|
helper.registerPlugin(md => {
|
||||||
|
@ -235,7 +235,12 @@ module PrettyText
|
|||||||
protect do
|
protect do
|
||||||
v8.eval(<<~JS)
|
v8.eval(<<~JS)
|
||||||
__paths = #{paths_json};
|
__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
|
JS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -243,9 +248,11 @@ module PrettyText
|
|||||||
def self.escape_emoji(title)
|
def self.escape_emoji(title)
|
||||||
return unless title
|
return unless title
|
||||||
|
|
||||||
|
replace_emoji_shortcuts = SiteSetting.enable_emoji && SiteSetting.enable_emoji_shortcuts
|
||||||
|
|
||||||
protect do
|
protect do
|
||||||
v8.eval(<<~JS)
|
v8.eval(<<~JS)
|
||||||
__performEmojiEscape(#{title.inspect});
|
__performEmojiEscape(#{title.inspect}, { emojiShortcuts: #{replace_emoji_shortcuts} });
|
||||||
JS
|
JS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -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))
|
expect(cook("[quote=\"EvilTrout, post:2, topic:#{topic.id}\"]\nddd\n[/quote]", topic_id: 1)).to eq(n(expected))
|
||||||
end
|
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
|
it "do off topic quoting of posts from secure categories" do
|
||||||
category = Fabricate(:category, read_restricted: true)
|
category = Fabricate(:category, read_restricted: true)
|
||||||
topic = Fabricate(:topic, title: "this is topic with secret category", category: category)
|
topic = Fabricate(:topic, title: "this is topic with secret category", category: category)
|
||||||
|
@ -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_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_emoji) { build_topic_with_title("I 💖 candy alot") }
|
||||||
let(:topic_modifier_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
|
it "escapes script contents" do
|
||||||
expect(topic_script.fancy_title).to eq("Topic with <script>alert(‘title’)</script> script in its title")
|
expect(topic_script.fancy_title).to eq("Topic with <script>alert(‘title’)</script> script in its title")
|
||||||
@ -320,6 +321,29 @@ describe Topic do
|
|||||||
expect(topic_script.fancy_title).not_to include("<script>")
|
expect(topic_script.fancy_title).not_to include("<script>")
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'fancy title' do
|
context 'fancy title' do
|
||||||
|
@ -91,6 +91,7 @@ Discourse.SiteSettingsOriginal = {
|
|||||||
highlighted_languages:
|
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",
|
"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: true,
|
||||||
|
enable_emoji_shortcuts: true,
|
||||||
emoji_set: "emoji_one",
|
emoji_set: "emoji_one",
|
||||||
desktop_category_page_style: "categories_and_latest_topics",
|
desktop_category_page_style: "categories_and_latest_topics",
|
||||||
enable_mentions: true,
|
enable_mentions: true,
|
||||||
|
@ -5,8 +5,18 @@ import { emojiUnescape } from "discourse/lib/text";
|
|||||||
QUnit.module("lib:emoji");
|
QUnit.module("lib:emoji");
|
||||||
|
|
||||||
QUnit.test("emojiUnescape", assert => {
|
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);
|
assert.equal(emojiUnescape(input), expected, description);
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(originalSettings)) {
|
||||||
|
Discourse.SiteSettings[key] = value;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
testUnescape(
|
testUnescape(
|
||||||
@ -64,6 +74,24 @@ QUnit.test("emojiUnescape", assert => {
|
|||||||
"hi :blonde_man:t6",
|
"hi :blonde_man:t6",
|
||||||
"end colon not optional for skin tones"
|
"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 => {
|
QUnit.test("Emoji search", assert => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user