FIX: Use only first character when looking up emoji (#22977)

The other characters may be variation selectors and result in a
false-negative.
This commit is contained in:
Bianca Nenciu 2023-08-04 15:28:58 +02:00 committed by GitHub
parent 207d983809
commit 1d58dcac1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -39,7 +39,7 @@ Object.entries(aliases).forEach(([name, list]) => {
list.forEach((alias) => aliasMap.set(alias, name)); list.forEach((alias) => aliasMap.set(alias, name));
}); });
function isReplacableInlineEmoji(string, index, inlineEmoji) { function isReplaceableInlineEmoji(string, index, inlineEmoji) {
if (inlineEmoji) { if (inlineEmoji) {
return true; return true;
} }
@ -68,13 +68,13 @@ export function performEmojiUnescape(string, opts) {
const replacementFunction = (m, index) => { const replacementFunction = (m, index) => {
const isEmoticon = opts.enableEmojiShortcuts && !!allTranslations[m]; const isEmoticon = opts.enableEmojiShortcuts && !!allTranslations[m];
const isUnicodeEmoticon = !!replacements[m]; const isUnicodeEmoticon = !!replacements[m] || !!replacements[m[0]];
let emojiVal; let emojiVal;
if (isEmoticon) { if (isEmoticon) {
emojiVal = allTranslations[m]; emojiVal = allTranslations[m];
} else if (isUnicodeEmoticon) { } else if (isUnicodeEmoticon) {
emojiVal = replacements[m]; emojiVal = replacements[m] || replacements[m[0]];
} else { } else {
emojiVal = m.slice(1, m.length - 1); emojiVal = m.slice(1, m.length - 1);
} }
@ -95,7 +95,7 @@ export function performEmojiUnescape(string, opts) {
const isReplacable = const isReplacable =
(isEmoticon || hasEndingColon || isUnicodeEmoticon) && (isEmoticon || hasEndingColon || isUnicodeEmoticon) &&
isReplacableInlineEmoji(string, index, opts.inlineEmoji); isReplaceableInlineEmoji(string, index, opts.inlineEmoji);
const title = opts.title ?? emojiVal; const title = opts.title ?? emojiVal;
const tabIndex = opts.tabIndex ? ` tabindex='${opts.tabIndex}'` : ""; const tabIndex = opts.tabIndex ? ` tabindex='${opts.tabIndex}'` : "";
@ -121,11 +121,13 @@ export function performEmojiEscape(string, opts) {
); );
const replacementFunction = (m, index) => { const replacementFunction = (m, index) => {
if (isReplacableInlineEmoji(string, index, opts.inlineEmoji)) { if (isReplaceableInlineEmoji(string, index, opts.inlineEmoji)) {
if (!!allTranslations[m]) { if (!!allTranslations[m]) {
return opts.emojiShortcuts ? `:${allTranslations[m]}:` : m; return opts.emojiShortcuts ? `:${allTranslations[m]}:` : m;
} else if (!!replacements[m]) { } else if (!!replacements[m]) {
return `:${replacements[m]}:`; return `:${replacements[m]}:`;
} else if (!!replacements[m[0]]) {
return `:${replacements[m[0]]}:`;
} }
} }

View File

@ -9,20 +9,33 @@ RSpec.describe MaxEmojisValidator do
end end
shared_examples "validating any topic title" do shared_examples "validating any topic title" do
it "adds an error when emoji count is greater than SiteSetting.max_emojis_in_title" do before do
SiteSetting.max_emojis_in_title = 3 SiteSetting.max_emojis_in_title = 3
CustomEmoji.create!(name: "trout", upload: Fabricate(:upload)) CustomEmoji.create!(name: "trout", upload: Fabricate(:upload))
Emoji.clear_cache Emoji.clear_cache
end
it "adds an error when emoji count is greater than SiteSetting.max_emojis_in_title" do
record.title = "🧐 Lots of emojis here 🎃 :trout: :)" record.title = "🧐 Lots of emojis here 🎃 :trout: :)"
validate validate
expect(record.errors[:title][0]).to eq( expect(record.errors[:title][0]).to eq(
I18n.t("errors.messages.max_emojis", max_emojis_count: 3), I18n.t("errors.messages.max_emojis", max_emojis_count: 3),
) )
end
it "does not add an error when emoji count is exactly SiteSetting.max_emojis_in_title" do
record.title = ":joy: :blush: :smile: is not only about emojis: Happiness::start()" record.title = ":joy: :blush: :smile: is not only about emojis: Happiness::start()"
validate validate
expect(record.valid?).to be true expect(record.valid?).to be true
end end
it "counts emojis with variation selectors" do
record.title = "Title with emojis ☠️☠️☠️☠️"
validate
expect(record.errors[:title][0]).to eq(
I18n.t("errors.messages.max_emojis", max_emojis_count: 3),
)
end
end end
describe "topic" do describe "topic" do