FIX: unescape emoji in pretty title

This makes emoji in title consistent, it is later escaped back when needed
This commit is contained in:
Sam 2017-05-15 10:27:54 -04:00
parent b839eb41ff
commit 44d7fe89ed
3 changed files with 16 additions and 1 deletions

View File

@ -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 = {}

View File

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

View File

@ -257,11 +257,16 @@ describe Topic do
let(:topic_bold) { build_topic_with_title("Topic with <b>bold</b> text in its title" ) }
let(:topic_image) { build_topic_with_title("Topic with <img src='something'> image in its title" ) }
let(:topic_script) { build_topic_with_title("Topic with <script>alert('title')</script> 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 &lt;script&gt;alert(&lsquo;title&rsquo;)&lt;/script&gt; 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 &lt;b&gt;bold&lt;/b&gt; text in its title")
end