FIX: Support unicode replacements with multiple codepoints

This commit is contained in:
Robin Ward 2016-07-05 13:55:21 -04:00
parent c104e4c022
commit 3fe4903e63
2 changed files with 28 additions and 5 deletions

View File

@ -131,17 +131,22 @@ class Emoji
"#{Discourse.base_uri}/uploads/#{db}/_emoji" "#{Discourse.base_uri}/uploads/#{db}/_emoji"
end 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 def self.unicode_replacements
return @unicode_replacements if @unicode_replacements return @unicode_replacements if @unicode_replacements
@unicode_replacements = {} @unicode_replacements = {}
db['emojis'].each do |e| db['emojis'].each do |e|
hex = e['code'].hex next if e['name'] == 'tm'
# Don't replace digits, letters and some symbols code = replacement_code(e['code'])
if hex > 255 && e['name'] != 'tm' @unicode_replacements[code] = e['name'] if code
@unicode_replacements[[hex].pack('U')] = e['name']
end
end end
@unicode_replacements["\u{2639}"] = 'frowning' @unicode_replacements["\u{2639}"] = 'frowning'

18
spec/models/emoji_spec.rb Normal file
View File

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