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.



5
6
7
# File 'lib/better_html/html_attributes.rb', line 5

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

Instance Method Details

#to_sObject



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

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.to_s
    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