class Hanami::Helpers::HtmlHelper::EmptyHtmlNode
Empty HTML node
@since 0.1.0 @api private
Constants
- ATTRIBUTES_SEPARATOR
Attributes separator
@since 0.1.0 @api private
- BOOLEAN_ATTRIBUTES
List of attributes that get special treatment when rendering.
@since 0.2.5 @api private
@see www.w3.org/html/wg/drafts/html/master/infrastructure.html#boolean-attribute
Public Class Methods
Initialize a new empty HTML node
@param name [Symbol,String] the name of the tag @param attributes [Hash,NilClass] the optional tag attributes
@return [Hanami::Helpers::HtmlHelper::EmptyHtmlNode]
@since 0.1.0 @api private
# File lib/hanami/helpers/html_helper/empty_html_node.rb, line 79 def initialize(name, attributes) @name = name @attributes = prepare_html_attributes(attributes) end
Public Instance Methods
Resolve and return the output
@return [String] the output
@since 0.1.0 @api private
# File lib/hanami/helpers/html_helper/empty_html_node.rb, line 90 def to_s %(<#{@name}#{attributes}>) end
Private Instance Methods
@api private
# File lib/hanami/helpers/html_helper/empty_html_node.rb, line 130 def attribute(attribute_name, value) %(#{ATTRIBUTES_SEPARATOR}#{attribute_name}="#{value}") end
Resolve the attributes
@return [String,NilClass] the tag attributes
@since 0.1.0 @api private
# File lib/hanami/helpers/html_helper/empty_html_node.rb, line 102 def attributes return unless defined?(@attributes) && !@attributes.nil? result = '' @attributes.each do |attribute_name, value| if boolean_attribute?(attribute_name) result << boolean_attribute(attribute_name, value) if value else result << attribute(attribute_name, value) end end result end
Do not render boolean attributes when their value is false. @api private
# File lib/hanami/helpers/html_helper/empty_html_node.rb, line 125 def boolean_attribute(attribute_name, _value) %(#{ATTRIBUTES_SEPARATOR}#{attribute_name}="#{attribute_name}") end
@api private
# File lib/hanami/helpers/html_helper/empty_html_node.rb, line 119 def boolean_attribute?(attribute_name) BOOLEAN_ATTRIBUTES.include?(attribute_name.to_s) end
Changes any attributes values that are arrays into a string joined by a space, as HTML expects it.
@param attributes [Hash] the attributes with values to be HTMLified
@return [Hash] the attributes transformed into HTML friendly values, i.e space separated strings
@since 1.2.0
# File lib/hanami/helpers/html_helper/empty_html_node.rb, line 142 def prepare_html_attributes(attributes) attributes&.inject({}) do |attrs, (key, value)| attrs[key] = value.is_a?(Array) ? value.join(ATTRIBUTES_SEPARATOR) : value attrs end end