emoji_formatter.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. class EmojiFormatter
  3. include RoutingHelper
  4. DISALLOWED_BOUNDING_REGEX = /[[:alnum:]:]/.freeze
  5. attr_reader :html, :custom_emojis, :options
  6. # @param [ActiveSupport::SafeBuffer] html
  7. # @param [Array<CustomEmoji>] custom_emojis
  8. # @param [Hash] options
  9. # @option options [Boolean] :animate
  10. # @option options [String] :style
  11. def initialize(html, custom_emojis, options = {})
  12. raise ArgumentError unless html.html_safe?
  13. @html = html
  14. @custom_emojis = custom_emojis
  15. @options = options
  16. end
  17. def to_s
  18. return html if custom_emojis.empty? || html.blank?
  19. tree = Nokogiri::HTML.fragment(html)
  20. tree.xpath('./text()|.//text()[not(ancestor[@class="invisible"])]').to_a.each do |node|
  21. i = -1
  22. inside_shortname = false
  23. shortname_start_index = -1
  24. last_index = 0
  25. text = node.content
  26. result = Nokogiri::XML::NodeSet.new(tree.document)
  27. while i + 1 < text.size
  28. i += 1
  29. if inside_shortname && text[i] == ':'
  30. inside_shortname = false
  31. shortcode = text[shortname_start_index + 1..i - 1]
  32. char_after = text[i + 1]
  33. next unless (char_after.nil? || !DISALLOWED_BOUNDING_REGEX.match?(char_after)) && (emoji = emoji_map[shortcode])
  34. result << Nokogiri::XML::Text.new(text[last_index..shortname_start_index - 1], tree.document) if shortname_start_index.positive?
  35. result << Nokogiri::HTML.fragment(image_for_emoji(shortcode, emoji))
  36. last_index = i + 1
  37. elsif text[i] == ':' && (i.zero? || !DISALLOWED_BOUNDING_REGEX.match?(text[i - 1]))
  38. inside_shortname = true
  39. shortname_start_index = i
  40. end
  41. end
  42. result << Nokogiri::XML::Text.new(text[last_index..-1], tree.document)
  43. node.replace(result)
  44. end
  45. tree.to_html.html_safe # rubocop:disable Rails/OutputSafety
  46. end
  47. private
  48. def emoji_map
  49. @emoji_map ||= custom_emojis.each_with_object({}) { |e, h| h[e.shortcode] = [full_asset_url(e.image.url), full_asset_url(e.image.url(:static))] }
  50. end
  51. def count_tag_nesting(tag)
  52. if tag[1] == '/'
  53. -1
  54. elsif tag[-2] == '/'
  55. 0
  56. else
  57. 1
  58. end
  59. end
  60. def image_for_emoji(shortcode, emoji)
  61. original_url, static_url = emoji
  62. image_tag(
  63. animate? ? original_url : static_url,
  64. image_attributes.merge(alt: ":#{shortcode}:", title: ":#{shortcode}:", data: image_data_attributes(original_url, static_url))
  65. )
  66. end
  67. def image_attributes
  68. { rel: 'emoji', draggable: false, width: 16, height: 16, class: image_class_names, style: image_style }
  69. end
  70. def image_data_attributes(original_url, static_url)
  71. { original: original_url, static: static_url } unless animate?
  72. end
  73. def image_class_names
  74. animate? ? 'emojione' : 'emojione custom-emoji'
  75. end
  76. def image_style
  77. @options[:style]
  78. end
  79. def animate?
  80. @options[:animate] || @options.key?(:style)
  81. end
  82. end