# Module: Haml::AttributeBuilder

Defined in:
  lib/haml/attribute_builder.rb

## Class Method Summary collapse

- .build(escape_attrs, quote, format, object_ref, *hashes)
- .build_aria(escape_attrs, quote, format, *hashes)
- .build_class(escape_attrs, *values)
- .build_data(escape_attrs, quote, format, *hashes)
- .build_id(escape_attrs, *values)

## Class Method Details

### .build(escape_attrs, quote, format, object_ref, *hashes)

    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29

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

    def build(escape_attrs, quote, format, object_ref, *hashes)
      hashes << Haml::ObjectRef.parse(object_ref) if object_ref
      buf  = []
      hash = merge_all_attrs(hashes)

      keys = hash.keys.sort!
      keys.each do |key|
        case key
        when 'id'
          buf << " id=#{quote}#{build_id(escape_attrs, *hash[key])}#{quote}"
        when 'class'
          buf << " class=#{quote}#{build_class(escape_attrs, *hash[key])}#{quote}"
        when 'data'
          buf << build_data(escape_attrs, quote, format, *hash[key])
        when 'aria'
          buf << build_aria(escape_attrs, quote, format, *hash[key])
        when *Haml::BOOLEAN_ATTRIBUTES, /\Adata-/, /\Aaria-/
          build_boolean!(escape_attrs, quote, format, buf, key, hash[key])
        else
          buf << " #{key}=#{quote}#{escape_html(escape_attrs, hash[key].to_s)}#{quote}"
        end
      end
      buf.join
    end

### .build_aria(escape_attrs, quote, format, *hashes)

    69
    70
    71

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

    def build_aria(escape_attrs, quote, format, *hashes)
      build_data_attribute(:aria, escape_attrs, quote, format, *hashes)
    end

### .build_class(escape_attrs, *values)

    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
    62
    63

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

    def build_class(escape_attrs, *values)
      if values.size == 1
        value = values.first
        case
        when value.is_a?(String)
          # noop
        when value.is_a?(Array)
          value = value.flatten.select { |v| v }.map(&:to_s).uniq.join(' ')
        when value
          value = value.to_s
        else
          return ''
        end
        return escape_html(escape_attrs, value)
      end

      classes = []
      values.each do |value|
        case
        when value.is_a?(String)
          classes += value.split(' ')
        when value.is_a?(Array)
          classes += value.flatten.select { |v| v }
        when value
          classes << value.to_s
        end
      end
      escape_html(escape_attrs, classes.map(&:to_s).uniq.join(' '))
    end

### .build_data(escape_attrs, quote, format, *hashes)

    65
    66
    67

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

    def build_data(escape_attrs, quote, format, *hashes)
      build_data_attribute(:data, escape_attrs, quote, format, *hashes)
    end

### .build_id(escape_attrs, *values)

    31
    32
    33

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

    def build_id(escape_attrs, *values)
      escape_html(escape_attrs, values.flatten.select { |v| v }.join('_'))
    end