Sha256: b9c1f2ce2826843d852ea2e249b8a8fa51eb0d594eab6886e964637b9f4d4355

Contents?: true

Size: 921 Bytes

Versions: 2

Compression:

Stored size: 921 Bytes

Contents

# frozen_string_literal: true

module BetterHtml
  class HtmlAttributes
    def initialize(data)
      @data = data.stringify_keys
    end

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
better_html-2.1.1 lib/better_html/html_attributes.rb
better_html-2.1.0 lib/better_html/html_attributes.rb