# Module: Haml::AttributeBuilder

Defined in:
  lib/haml/attribute_builder.rb

## Class Method Summary collapse

- .build(class_id, obj_ref, is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, *attributes_hashes)
- .build_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes = {})
- .filter_and_join(value, separator) ⇒ String?
- .merge_attributes!(to, from) ⇒ {String => String,Hash}

  Merges two attribute hashes.

- .merge_values(key, *values) ⇒ String, Hash

  Merge multiple values to one attribute value.

- .verify_attribute_names!(attribute_names)

## Class Method Details

### .build(class_id, obj_ref, is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, *attributes_hashes)

    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    # File 'lib/haml/attribute_builder.rb', line 9

    def build(class_id, obj_ref, is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, *attributes_hashes)
      attributes = class_id
      attributes_hashes.each do |old|
        result = {}
        old.each { |k, v| result[k.to_s] = v }
        merge_attributes!(attributes, result)
      end
      merge_attributes!(attributes, parse_object_ref(obj_ref)) if obj_ref
      build_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes)
    end

### .build_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes = {})

    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61

    # File 'lib/haml/attribute_builder.rb', line 20

    def build_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes = {})
      # @TODO this is an absolutely ridiculous amount of arguments. At least
      # some of this needs to be moved into an instance method.
      join_char = hyphenate_data_attrs ? '-' : '_'

      attributes.each do |key, value|
        if value.is_a?(Hash)
          data_attributes = attributes.delete(key)
          data_attributes = flatten_data_attributes(data_attributes, '', join_char)
          data_attributes = build_data_keys(data_attributes, hyphenate_data_attrs, key)
          verify_attribute_names!(data_attributes.keys)
          attributes = data_attributes.merge(attributes)
        end
      end

      result = attributes.collect do |attr, value|
        next if value.nil?

        value = filter_and_join(value, ' ') if attr == 'class'
        value = filter_and_join(value, '_') if attr == 'id'

        if value == true
          next " #{attr}" if is_html
          next " #{attr}=#{attr_wrapper}#{attr}#{attr_wrapper}"
        elsif value == false
          next
        end

        value =
          if escape_attrs == :once
            Haml::Helpers.escape_once_without_haml_xss(value.to_s)
          elsif escape_attrs
            Haml::Helpers.html_escape_without_haml_xss(value.to_s)
          else
            value.to_s
          end
        " #{attr}=#{attr_wrapper}#{value}#{attr_wrapper}"
      end
      result.compact!
      result.sort!
      result.join
    end

### .filter_and_join(value, separator) ⇒ `String`?

Returns:

- (`String`, `nil`)

    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76

    # File 'lib/haml/attribute_builder.rb', line 64

    def filter_and_join(value, separator)
      return '' if (value.respond_to?(:empty?) && value.empty?)

      if value.is_a?(Array)
        value = value.flatten
        value.map! {|item| item ? item.to_s : nil}
        value.compact!
        value = value.join(separator)
      else
        value = value ? value.to_s : nil
      end
      !value.nil? && !value.empty? && value
    end

### .merge_attributes!(to, from) ⇒ `{String => String,Hash}`

Merges two attribute hashes. This is the same as `to.merge!(from)`, except that it merges id, class, and data attributes.

ids are concatenated with `"_"`, and classes are concatenated with `" "`. data hashes are simply merged.

Destructively modifies `to`.

Parameters:

- to (`{String => String,Hash}`) —

  The attribute hash to merge into

- from (`{String => Object}`) —

  The attribute hash to merge from

Returns:

- (`{String => String,Hash}`) —

  `to`, after being merged

    91
    92
    93
    94
    95
    96

    # File 'lib/haml/attribute_builder.rb', line 91

    def merge_attributes!(to, from)
      from.keys.each do |key|
        to[key] = merge_value(key, to[key], from[key])
      end
      to
    end

### .merge_values(key, *values) ⇒ `String`, `Hash`

Merge multiple values to one attribute value. No destructive operation.

Parameters:

- key (`String`)
- values (`Array<Object>`)

Returns:

- (`String`, `Hash`)

    103
    104
    105
    106
    107

    # File 'lib/haml/attribute_builder.rb', line 103

    def merge_values(key, *values)
      values.inject(nil) do |to, from|
        merge_value(key, to, from)
      end
    end

### .verify_attribute_names!(attribute_names)

    109
    110
    111
    112
    113
    114
    115

    # File 'lib/haml/attribute_builder.rb', line 109

    def verify_attribute_names!(attribute_names)
      attribute_names.each do |attribute_name|
        if attribute_name =~ INVALID_ATTRIBUTE_NAME_REGEX
          raise InvalidAttributeNameError.new("Invalid attribute name '#{attribute_name}' was rendered")
        end
      end
    end