diff --git a/app/models/emoji.rb b/app/models/emoji.rb index 9ff96bebc81..e7bab298546 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -131,17 +131,22 @@ class Emoji "#{Discourse.base_uri}/uploads/#{db}/_emoji" end + def self.replacement_code(code) + hexes = code.split('-').map(&:hex) + + # Don't replace digits, letters and some symbols + return hexes.pack("U" * hexes.size) if hexes[0] > 255 + end + def self.unicode_replacements return @unicode_replacements if @unicode_replacements @unicode_replacements = {} db['emojis'].each do |e| - hex = e['code'].hex - # Don't replace digits, letters and some symbols - if hex > 255 && e['name'] != 'tm' - @unicode_replacements[[hex].pack('U')] = e['name'] - end + next if e['name'] == 'tm' + code = replacement_code(e['code']) + @unicode_replacements[code] = e['name'] if code end @unicode_replacements["\u{2639}"] = 'frowning' diff --git a/spec/models/emoji_spec.rb b/spec/models/emoji_spec.rb new file mode 100644 index 00000000000..33b2f9114d9 --- /dev/null +++ b/spec/models/emoji_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +describe Emoji do + + it "returns the correct codepoints" do + expect(Emoji.replacement_code('1f47d').codepoints).to eq([128125]) + end + + it "handles multiple codepoints" 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 + +end