0
0
Fork 0

Avoid emojifying on invisible text (#5558)

This commit is contained in:
MIYAGI Hikaru 2017-11-07 22:48:13 +09:00 committed by Eugen Rochko
parent 84cfee2488
commit 782224c991
3 changed files with 60 additions and 9 deletions

View file

@ -89,20 +89,28 @@ class Formatter
end
end
def count_tag_nesting(tag)
if tag[1] == '/' then -1
elsif tag[-2] == '/' then 0
else 1
end
end
def encode_custom_emojis(html, emojis)
return html if emojis.empty?
emoji_map = emojis.map { |e| [e.shortcode, full_asset_url(e.image.url(:static))] }.to_h
i = -1
inside_tag = false
tag_open_index = nil
inside_shortname = false
shortname_start_index = -1
invisible_depth = 0
while i + 1 < html.size
i += 1
if inside_shortname && html[i] == ':'
if invisible_depth.zero? && inside_shortname && html[i] == ':'
shortcode = html[shortname_start_index + 1..i - 1]
emoji = emoji_map[shortcode]
@ -116,12 +124,18 @@ class Formatter
end
inside_shortname = false
elsif inside_tag && html[i] == '>'
inside_tag = false
elsif tag_open_index && html[i] == '>'
tag = html[tag_open_index..i]
tag_open_index = nil
if invisible_depth.positive?
invisible_depth += count_tag_nesting(tag)
elsif tag == '<span class="invisible">'
invisible_depth = 1
end
elsif html[i] == '<'
inside_tag = true
tag_open_index = i
inside_shortname = false
elsif !inside_tag && html[i] == ':'
elsif !tag_open_index && html[i] == ':'
inside_shortname = true
shortname_start_index = i
end