Skip to content

Commit 3518bbb

Browse files
committed
Make Emoji img attributes configurable
1. User can specify a hash `context[:img_attrs]` to change the `img` tag attributes, e.g. `{ "draggable" => false }` 2. The hash key can be either `String` / `Symbol` (indifferent access) `{ "draggable" => false }` / `{ draggable: false }` => `<img draggable="false">` 3. The hash value can be either anything / proc-like object Proc-like object with default argument `name`: Given name is `:shipit:` `{ title: ->(name) { |n| n.gsub(":", "") } }` => `<img title="shipit">` So you can do any customisations with the attribute. 4. The hash value nil means clear the attribute of img tag For example, to clear the default `height`, `width`, and `align` attributes, pass `{ height: nil, width: nil, align: nil }` to `context[:img_attrs]`. 5. Refine tests with consistent styles
1 parent f612486 commit 3518bbb

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/html/pipeline/emoji_filter.rb

+22-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Pipeline
1414
# :asset_root (required) - base url to link to emoji sprite
1515
# :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.
1616
# :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).
17+
# :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.
1718
class EmojiFilter < Filter
1819

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

7273
# Build an emoji image tag
7374
def emoji_image_tag(name)
74-
"<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />"
75+
require "active_support/core_ext/hash/indifferent_access"
76+
html_attrs =
77+
default_img_attrs(name).
78+
merge!((context[:img_attrs] || {}).with_indifferent_access).
79+
map { |attr, value| !value.nil? && %(#{attr}="#{value.try(:call, name) || value}") }.
80+
reject(&:blank?).join(" ".freeze)
81+
82+
"<img #{html_attrs}>"
83+
end
84+
85+
# Default attributes for img tag
86+
def default_img_attrs(name)
87+
{
88+
"class" => "emoji".freeze,
89+
"title" => ":#{name}:",
90+
"alt" => ":#{name}:",
91+
"src" => "#{emoji_url(name)}",
92+
"height" => "20".freeze,
93+
"width" => "20".freeze,
94+
"align" => "absmiddle".freeze,
95+
}
7596
end
7697

7798
def emoji_url(name)

test/html/pipeline/emoji_filter_test.rb

+29
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,33 @@ def test_not_emojify_in_custom_multiple_tags_foo_and_bar
6262
doc = filter.call
6363
assert_equal body, doc.to_html
6464
end
65+
66+
def test_img_tag_attributes
67+
body = ":shipit:"
68+
filter = EmojiFilter.new(body, {:asset_root => "https://foo.com"})
69+
doc = filter.call
70+
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
71+
end
72+
73+
def test_img_tag_attributes_can_be_customized
74+
body = ":shipit:"
75+
filter = EmojiFilter.new(body, {:asset_root => "https://foo.com", img_attrs: Hash("draggable"=> "false", "height" => nil, "width" => nil, "align" => nil)})
76+
doc = filter.call
77+
assert_equal %(<img class="emoji" title=":shipit:" alt=":shipit:" src="https://foo.com/emoji/shipit.png" draggable="false">), doc.to_html
78+
end
79+
80+
def test_img_attrs_value_can_accept_proclike_object
81+
remove_colons = ->(name) { name.gsub(":", "") }
82+
body = ":shipit:"
83+
filter = EmojiFilter.new(body, {:asset_root => "https://foo.com", img_attrs: Hash("title" => remove_colons)})
84+
doc = filter.call
85+
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
86+
end
87+
88+
def test_img_attrs_can_accept_symbolized_keys
89+
body = ":shipit:"
90+
filter = EmojiFilter.new(body, {:asset_root => "https://foo.com", img_attrs: Hash(draggable: false, height: nil, width: nil, align: nil)})
91+
doc = filter.call
92+
assert_equal %(<img class="emoji" title=":shipit:" alt=":shipit:" src="https://foo.com/emoji/shipit.png" draggable="false">), doc.to_html
93+
end
6594
end

0 commit comments

Comments
 (0)