Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Emoji img attributes configurable #258

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/html/pipeline/emoji_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Pipeline
# :asset_root (required) - base url to link to emoji sprite
# :asset_path (optional) - url path to link to emoji sprite. :file_name can be used as a placeholder for the sprite file name. If no asset_path is set "emoji/:file_name" is used.
# :ignored_ancestor_tags (optional) - Tags to stop the emojification. Node has matched ancestor HTML tags will not be emojified. Default to pre, code, and tt tags. Extra tags please pass in the form of array, e.g., %w(blockquote summary).
# :img_attrs (optional) - Attributes for generated img tag. E.g. Pass { "draggble" => true, "height" => nil } to set draggable attribute to "true" and clear height attribute of generated img tag.
class EmojiFilter < Filter

DEFAULT_IGNORED_ANCESTOR_TAGS = %w(pre code tt).freeze
Expand Down Expand Up @@ -71,7 +72,27 @@ def asset_path(name)

# Build an emoji image tag
def emoji_image_tag(name)
"<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />"
require "active_support/core_ext/hash/indifferent_access"
html_attrs =
default_img_attrs(name).
merge!((context[:img_attrs] || {}).with_indifferent_access).
map { |attr, value| !value.nil? && %(#{attr}="#{value.try(:call, name) || value}") }.
reject(&:blank?).join(" ".freeze)

"<img #{html_attrs}>"
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we simplify this to just merge the hashes and iterate through the values inline? This method creates several intermediate structures that aren't necessary and increase memory usage.


# Default attributes for img tag
def default_img_attrs(name)
{
"class" => "emoji".freeze,
"title" => ":#{name}:",
"alt" => ":#{name}:",
"src" => "#{emoji_url(name)}",
"height" => "20".freeze,
"width" => "20".freeze,
"align" => "absmiddle".freeze,
}
end

def emoji_url(name)
Expand Down
29 changes: 29 additions & 0 deletions test/html/pipeline/emoji_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,33 @@ def test_not_emojify_in_custom_multiple_tags_foo_and_bar
doc = filter.call
assert_equal body, doc.to_html
end

def test_img_tag_attributes
body = ":shipit:"
filter = EmojiFilter.new(body, {:asset_root => "https://foo.com"})
doc = filter.call
assert_equal %(<img class="emoji" title=":shipit:" alt=":shipit:" src="https://foo.com/emoji/shipit.png" height="20" width="20" align="absmiddle">), doc.to_html
end

def test_img_tag_attributes_can_be_customized
body = ":shipit:"
filter = EmojiFilter.new(body, {:asset_root => "https://foo.com", img_attrs: Hash("draggable"=> "false", "height" => nil, "width" => nil, "align" => nil)})
doc = filter.call
assert_equal %(<img class="emoji" title=":shipit:" alt=":shipit:" src="https://foo.com/emoji/shipit.png" draggable="false">), doc.to_html
end

def test_img_attrs_value_can_accept_proclike_object
remove_colons = ->(name) { name.gsub(":", "") }
body = ":shipit:"
filter = EmojiFilter.new(body, {:asset_root => "https://foo.com", img_attrs: Hash("title" => remove_colons)})
doc = filter.call
assert_equal %(<img class="emoji" title="shipit" alt=":shipit:" src="https://foo.com/emoji/shipit.png" height="20" width="20" align="absmiddle">), doc.to_html
end

def test_img_attrs_can_accept_symbolized_keys
body = ":shipit:"
filter = EmojiFilter.new(body, {:asset_root => "https://foo.com", img_attrs: Hash(draggable: false, height: nil, width: nil, align: nil)})
doc = filter.call
assert_equal %(<img class="emoji" title=":shipit:" alt=":shipit:" src="https://foo.com/emoji/shipit.png" draggable="false">), doc.to_html
end
end