class Haml::AttributeCompiler

def self.runtime_build(attributes, object_ref, dynamic_attributes)

Returns:
  • (String) - Attributes rendering code

Parameters:
  • dynamic_attributes (DynamicAttributes) --
  • object_ref (String, :nil) --
  • attributes (Hash) --
def self.runtime_build(attributes, object_ref, dynamic_attributes)
  "_hamlout.attributes(#{Haml::Util.inspect_obj(attributes)}, #{object_ref},#{dynamic_attributes.to_literal})"
end

def build_attribute_values(attributes, parsed_hashes)

Returns:
  • (Array) -

Parameters:
  • parsed_hashes ({ String => String }) --
  • attributes ({ String => String }) --
def build_attribute_values(attributes, parsed_hashes)
  [].tap do |attribute_values|
    attributes.each do |key, static_value|
      attribute_values << AttributeValue.new(:static, key, static_value)
    end
    parsed_hashes.each do |parsed_hash|
      parsed_hash.each do |key, dynamic_value|
        attribute_values << AttributeValue.new(:dynamic, key, dynamic_value)
      end
    end
  end
end

def compile(attributes, object_ref, dynamic_attributes)

Returns:
  • (Array) - Temple expression

Parameters:
  • dynamic_attributes (DynamicAttributes) --
  • object_ref (String, :nil) --
  • attributes (Hash) --
def compile(attributes, object_ref, dynamic_attributes)
  if object_ref != :nil || !AttributeParser.available?
    return [:dynamic, AttributeCompiler.runtime_build(attributes, object_ref, dynamic_attributes)]
  end
  parsed_hashes = [dynamic_attributes.new, dynamic_attributes.old].compact.map do |attribute_hash|
    unless (hash = AttributeParser.parse(attribute_hash))
      return [:dynamic, AttributeCompiler.runtime_build(attributes, object_ref, dynamic_attributes)]
    end
    hash
  end
  attribute_values = build_attribute_values(attributes, parsed_hashes)
  AttributeBuilder.verify_attribute_names!(attribute_values.map(&:key))
  [:multi, *group_values_for_sort(attribute_values).map { |value_group|
    compile_attribute_values(value_group)
  }]
end

def compile_attribute(key, values)

Returns:
  • (Array) - Temple expression

Parameters:
  • values (Array) --
  • key (String) --
def compile_attribute(key, values)
  if values.all? { |v| Temple::StaticAnalyzer.static?(v.to_literal) }
    return static_build(values)
  end
  case key
  when 'id', 'class'
    compile_id_or_class_attribute(key, values)
  else
    compile_common_attribute(key, values)
  end
end

def compile_attribute_values(values)

Returns:
  • (Array) - Temple expression

Parameters:
  • values (Array) -- whose `key`s are partially or fully the same from left.
def compile_attribute_values(values)
  if values.map(&:key).uniq.size == 1
    compile_attribute(values.first.key, values)
  else
    runtime_build(values)
  end
end

def compile_common_attribute(key, values)

Returns:
  • (Array) - Temple expression

Parameters:
  • values (Array) --
  • key (String) -- Not "id" or "class"
def compile_common_attribute(key, values)
  var = unique_name
  [:multi,
   [:code, "#{var} = (#{merged_value(key, values)})"],
   [:case, var,
    ['Hash', runtime_build([AttributeValue.new(:dynamic, key, var)])],
    ['true', true_value(key)],
    ['false, nil', [:multi]],
    [:else, [:multi,
             [:static, " #{key}=#{@attr_wrapper}"],
             [:escape, @escape_attrs, [:dynamic, var]],
             [:static, @attr_wrapper]],
    ]
   ],
  ]
end

def compile_id_or_class_attribute(id_or_class, values)

Returns:
  • (Array) - Temple expression

Parameters:
  • values (Array) --
  • id_or_class (String) -- "id" or "class"
def compile_id_or_class_attribute(id_or_class, values)
  var = unique_name
  [:multi,
   [:code, "#{var} = (#{merged_value(id_or_class, values)})"],
   [:case, var,
    ['Hash, Array', runtime_build([AttributeValue.new(:dynamic, id_or_class, var)])],
    ['false, nil', [:multi]],
    [:else, [:multi,
             [:static, " #{id_or_class}=#{@attr_wrapper}"],
             [:escape, @escape_attrs, [:dynamic, var]],
             [:static, @attr_wrapper]],
    ]
   ],
  ]
end

def frozen_string(str)

Returns:
  • (String) -

Parameters:
  • str (String) --
def frozen_string(str)
  "#{Haml::Util.inspect_obj(str)}.freeze"
end

def group_values_for_sort(values)

Returns:
  • (Array>) -

Parameters:
  • values (Array) --
def group_values_for_sort(values)
  sorted_values = values.sort_by(&:key)
  [].tap do |value_groups|
    until sorted_values.empty?
      key = sorted_values.first.key
      value_group, sorted_values = sorted_values.partition { |v| v.key.start_with?(key) }
      value_groups << value_group
    end
  end
end

def initialize(options)

Parameters:
  • options (Haml::Options) --
def initialize(options)
  @is_html = [:html4, :html5].include?(options[:format])
  @attr_wrapper = options[:attr_wrapper]
  @escape_attrs = options[:escape_attrs]
  @hyphenate_data_attrs = options[:hyphenate_data_attrs]
end

def merged_value(key, values)

Returns:
  • (String) -

Parameters:
  • values (Array) --
  • key (String) --
def merged_value(key, values)
  if values.size == 1
    values.first.to_literal
  else
    "::Haml::AttributeBuilder.merge_values(#{frozen_string(key)}, #{values.map(&:to_literal).join(', ')})"
  end
end

def runtime_build(values)

Returns:
  • (Array) - Temple expression

Parameters:
  • values (Array) --
def runtime_build(values)
  hash_content = values.group_by(&:key).map do |key, values_for_key|
    "#{frozen_string(key)} => #{merged_value(key, values_for_key)}"
  end.join(', ')
  [:dynamic, "_hamlout.attributes({ #{hash_content} }, nil)"]
end

def static_build(values)

Returns:
  • (Array) - Temple expression

Parameters:
  • values (Array) --
def static_build(values)
  hash_content = values.group_by(&:key).map do |key, values_for_key|
    "#{frozen_string(key)} => #{merged_value(key, values_for_key)}"
  end.join(', ')
  arguments = [@is_html, @attr_wrapper, @escape_attrs, @hyphenate_data_attrs]
  code = "::Haml::AttributeBuilder.build_attributes"\
    "(#{arguments.map { |a| Haml::Util.inspect_obj(a) }.join(', ')}, { #{hash_content} })"
  [:static, eval(code).to_s]
end

def true_value(key)

def true_value(key)
  if @is_html
    [:static, " #{key}"]
  else
    [:static, " #{key}=#{@attr_wrapper}#{key}#{@attr_wrapper}"]
  end
end

def unique_name

def unique_name
  @unique_name ||= 0
  "_haml_attribute_compiler#{@unique_name += 1}"
end