Class: BetterHtml::HtmlAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/better_html/html_attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ HtmlAttributes

Returns a new instance of HtmlAttributes.



3
4
5
# File 'lib/better_html/html_attributes.rb', line 3

def initialize(data)
  @data = data.stringify_keys
end

Instance Method Details

#to_sObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/better_html/html_attributes.rb', line 7

def to_s
  @data.map do |key, value|
    unless key =~ BetterHtml.config.partial_attribute_name_pattern
      raise ArgumentError, "Attribute names must match the pattern #{BetterHtml.config.partial_attribute_name_pattern.inspect}"
    end
    if value.nil?
      "#{key}"
    else
      value = value.to_s
      escaped_value = value.html_safe? ? value : CGI.escapeHTML(value)
      if escaped_value.include?('"')
        raise ArgumentError, "The value provided for attribute '#{key}' contains a `\"` "\
          "character which is not allowed. Did you call .html_safe without properly escaping this data?"
      end
      "#{key}=\"#{escaped_value}\""
    end
  end.join(" ")
end