Fix emoji substitution not applying only to text nodes in backend code

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Claire 2022-11-09 15:38:46 +01:00
parent 6f26dcae4d
commit 0ce2b4d358

View file

@ -8,6 +8,8 @@ class Formatter
include ActionView::Helpers::TextHelper include ActionView::Helpers::TextHelper
DISALLOWED_BOUNDING_REGEX = /[[:alnum:]:]/.freeze
def format(status, **options) def format(status, **options)
if status.reblog? if status.reblog?
prepend_reblog = status.reblog.account.acct prepend_reblog = status.reblog.account.acct
@ -140,54 +142,49 @@ class Formatter
emoji_map = emojis.each_with_object({}) { |e, h| h[e.shortcode] = [full_asset_url(e.image.url), full_asset_url(e.image.url(:static))] } emoji_map = emojis.each_with_object({}) { |e, h| h[e.shortcode] = [full_asset_url(e.image.url), full_asset_url(e.image.url(:static))] }
i = -1 tree = Nokogiri::HTML.fragment(html)
tag_open_index = nil tree.xpath('./text()|.//text()[not(ancestor[@class="invisible"])]').to_a.each do |node|
inside_shortname = false i = -1
shortname_start_index = -1 inside_shortname = false
invisible_depth = 0 shortname_start_index = -1
last_index = 0
text = node.content
result = Nokogiri::XML::NodeSet.new(tree.document)
while i + 1 < html.size while i + 1 < text.size
i += 1 i += 1
if invisible_depth.zero? && inside_shortname && html[i] == ':' if inside_shortname && text[i] == ':'
shortcode = html[shortname_start_index + 1..i - 1] inside_shortname = false
emoji = emoji_map[shortcode] shortcode = text[shortname_start_index + 1..i - 1]
char_after = text[i + 1]
next unless (char_after.nil? || !DISALLOWED_BOUNDING_REGEX.match?(char_after)) && (emoji = emoji_map[shortcode])
if emoji
original_url, static_url = emoji original_url, static_url = emoji
replacement = begin
result << Nokogiri::XML::Text.new(text[last_index..shortname_start_index - 1], tree.document) if shortname_start_index.positive?
result << Nokogiri::HTML.fragment(
if animate if animate
image_tag(original_url, draggable: false, class: 'emojione', alt: ":#{shortcode}:", title: ":#{shortcode}:") image_tag(original_url, draggable: false, class: 'emojione', alt: ":#{shortcode}:", title: ":#{shortcode}:")
else else
image_tag(original_url, draggable: false, class: 'emojione custom-emoji', alt: ":#{shortcode}:", title: ":#{shortcode}:", data: { original: original_url, static: static_url }) image_tag(original_url, draggable: false, class: 'emojione custom-emoji', alt: ":#{shortcode}:", title: ":#{shortcode}:", data: { original: original_url, static: static_url })
end end
end )
before_html = shortname_start_index.positive? ? html[0..shortname_start_index - 1] : ''
html = before_html + replacement + html[i + 1..-1]
i += replacement.size - (shortcode.size + 2) - 1
else
i -= 1
end
inside_shortname = false last_index = i + 1
elsif tag_open_index && html[i] == '>' elsif text[i] == ':' && (i.zero? || !DISALLOWED_BOUNDING_REGEX.match?(text[i - 1]))
tag = html[tag_open_index..i] inside_shortname = true
tag_open_index = nil shortname_start_index = i
if invisible_depth.positive?
invisible_depth += count_tag_nesting(tag)
elsif tag == '<span class="invisible">'
invisible_depth = 1
end end
elsif html[i] == '<'
tag_open_index = i
inside_shortname = false
elsif !tag_open_index && html[i] == ':'
inside_shortname = true
shortname_start_index = i
end end
result << Nokogiri::XML::Text.new(text[last_index..-1], tree.document)
node.replace(result)
end end
html tree.to_html
end end
# rubocop:enable Metrics/BlockNesting # rubocop:enable Metrics/BlockNesting