-
-
Notifications
You must be signed in to change notification settings - Fork 382
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,42 @@ 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' />" | ||
"<img #{img_html_attrs(name)}>" | ||
end | ||
|
||
def img_html_attrs(name) | ||
img_attrs(name).map { |attr, value| %(#{attr}="#{value}") }.join(" ") | ||
end | ||
|
||
def img_attrs(name) | ||
user_overrides = customized_attrs(name).select { |k, v| !v.nil? } | ||
excluded_keys = customized_attrs(name).select { |k, v| v.nil? }.keys | ||
result = default_img_attrs(name).merge!(user_overrides) | ||
result.except(*excluded_keys) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
def default_img_attrs(name) | ||
{ | ||
"class" => "emoji", | ||
"title" => ":#{name}:", | ||
"alt" => ":#{name}:", | ||
"src" => "#{emoji_url(name)}", | ||
"height" => "20", | ||
"width" => "20", | ||
"align" => "absmiddle", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 thanks for keeping this backwards compatible |
||
} | ||
end | ||
|
||
def customized_attrs(name) | ||
return {} unless context[:img_attrs] | ||
|
||
@_custom_img_attributes ||= begin | ||
custom_img_attributes = context[:img_attrs] | ||
|
||
custom_img_attributes.each do |key, value| | ||
custom_img_attributes[key] = value.call(name) if value.respond_to?(:call) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be inlined as well. Similar reasoning as before. Here, we're mutating the default_attrs = default_img_attrs(name)
# This avoids creating an extra empty hash for each invocation and only merges if user passed something in
merged_attrs = context[:img_attrs] ? default_attrs.merge(context[:img_attrs]) : default_attrs
# This builds the attributes and handles procs. In your implementation, you remove nils, but I think that should be the responsibility of the caller. Nils would print as an empty value, which would still be valid.
html_attrs = merged_attrs.map do |k, v|
"#{k}='#{v.try(:call) || v}'"
end
"<img #{html_attrs.join(' '}>" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
👍 Awesome!
I adapt your imagination as f1c1618.
This is because #234, user of Another change I added is indifferent access to Why? Because a user may be tempted to use |
||
end | ||
|
||
def emoji_url(name) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wary of this change because unsafe user input could be injected here. Could you look whitelisting only attributes that make sense for a
img
element?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not from user input. It is context option used by developer.
ditto, let's leave this to developer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I missed that.
emoji_image_tag
method?edit noticed the new methods are private, but still feel they add too many layers.