FIX: allows replacement of digits and symbols emojis (#7978)

This commit is contained in:
Joffrey JAFFEUX 2019-08-07 11:38:58 +02:00 committed by GitHub
parent 9e248de02a
commit 5d72561f4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 18 deletions

View File

@ -21,11 +21,29 @@ export function extendedEmojiList() {
const emojiHash = {};
const unicodeRegexp = new RegExp(
Object.keys(replacements)
export function buildReplacementsList(emojiReplacements) {
return Object.keys(emojiReplacements)
.sort()
.reverse()
.join("|") + "|\\B:[^\\s:]+(?::t\\d)?:?\\B",
.map(emoji => {
return emoji
.split("")
.map(chr => {
return (
"\\u" +
chr
.charCodeAt(0)
.toString(16)
.padStart(4, "0")
);
})
.join("");
})
.join("|");
}
const unicodeRegexp = new RegExp(
buildReplacementsList(replacements) + "|\\B:[^\\s:]+(?::t\\d)?:?\\B",
"g"
);

View File

@ -133,9 +133,10 @@ class Emoji
end
def self.replacement_code(code)
hexes = code.split('-'.freeze).map!(&:hex)
# Don't replace digits, letters and some symbols
hexes.pack("U*".freeze) if hexes[0] > 255
code
.split('-'.freeze)
.map!(&:hex)
.pack("U*".freeze)
end
def self.unicode_replacements
@ -146,6 +147,10 @@ class Emoji
db['emojis'].each do |e|
name = e['name']
# special cased as we prefer to keep these as symbols
next if name == 'registered'.freeze
next if name == 'copyright'.freeze
next if name == 'tm'.freeze
code = replacement_code(e['code'])

View File

@ -1,6 +1,7 @@
__PrettyText = require("pretty-text/pretty-text").default;
__buildOptions = require("pretty-text/pretty-text").buildOptions;
__performEmojiUnescape = require("pretty-text/emoji").performEmojiUnescape;
__buildReplacementsList = require("pretty-text/emoji").buildReplacementsList;
__performEmojiEscape = require("pretty-text/emoji").performEmojiEscape;
__utils = require("discourse/lib/utilities");
@ -8,13 +9,7 @@ __utils = require("discourse/lib/utilities");
__emojiUnicodeReplacer = null;
__setUnicode = function(replacements) {
let unicodeRegexp = new RegExp(
Object.keys(replacements)
.sort()
.reverse()
.join("|"),
"g"
);
let unicodeRegexp = new RegExp(__buildReplacementsList(replacements), "g");
__emojiUnicodeReplacer = function(text) {
unicodeRegexp.lastIndex = 0;

View File

@ -877,6 +877,13 @@ describe PrettyText do
expect(PrettyText.cook("")).to match(/\:slight_smile\:/)
end
it "replaces digits" do
expect(PrettyText.cook("🔢")).to match(/\:1234\:/)
expect(PrettyText.cook("1")).to match(/\:one\:/)
expect(PrettyText.cook("#️⃣")).to match(/\:hash\:/)
expect(PrettyText.cook("*️⃣")).to match(/\:asterisk\:/)
end
it "doesn't replace unicode emoji if emoji is disabled" do
SiteSetting.enable_emoji = false
expect(PrettyText.cook("💣")).not_to match(/\:bomb\:/)

View File

@ -12,11 +12,6 @@ describe Emoji do
expect(Emoji.replacement_code('1f1e9-1f1ea').codepoints).to eq([127465, 127466])
end
it "returns nil for weird cases" do
expect(Emoji.replacement_code('32')).to be_nil
expect(Emoji.replacement_code('robin')).to be_nil
end
describe '.load_custom' do
describe 'when a custom emoji has an invalid upload_id' do
it 'should return the custom emoji without a URL' do