diff --git a/app/models/emoji.rb b/app/models/emoji.rb index e737110cf23..ab61a60257a 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -146,6 +146,16 @@ class Emoji @unicode_replacements end + def self.unicode_unescape(string) + string.each_char.map do |c| + if str = unicode_replacements[c] + ":#{str}:" + else + c + end + end.join + end + def self.lookup_unicode(name) @reverse_map ||= begin map = {} diff --git a/app/models/topic.rb b/app/models/topic.rb index b0c7b86bb09..9dafc340b2c 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -295,7 +295,7 @@ class Topic < ActiveRecord::Base def self.fancy_title(title) escaped = ERB::Util.html_escape(title) return unless escaped - HtmlPrettify.render(escaped) + Emoji.unicode_unescape(HtmlPrettify.render(escaped)) end def fancy_title diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 4330ccb42f0..0fc5c80cd70 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -257,11 +257,16 @@ describe Topic do let(:topic_bold) { build_topic_with_title("Topic with bold text in its title" ) } let(:topic_image) { build_topic_with_title("Topic with image in its title" ) } let(:topic_script) { build_topic_with_title("Topic with script in its title" ) } + let(:topic_emoji) { build_topic_with_title("I 💖 candy alot") } it "escapes script contents" do expect(topic_script.fancy_title).to eq("Topic with <script>alert(‘title’)</script> script in its title") end + it "expands emojis" do + expect(topic_emoji.fancy_title).to eq("I :sparkling_heart: candy alot") + end + it "escapes bold contents" do expect(topic_bold.fancy_title).to eq("Topic with <b>bold</b> text in its title") end