Module: Haml::AttributeBuilder
- Defined in:
- lib/haml/attribute_builder.rb
Class Method Summary collapse
-
.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_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes = {})
9 10 11 12 13 14 15 16 17 18 19 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 |
# File 'lib/haml/attribute_builder.rb', line 9
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(value.to_s)
elsif escape_attrs
Haml::Helpers.html_escape(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?
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/haml/attribute_builder.rb', line 53
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
.
80 81 82 83 84 85 |
# File 'lib/haml/attribute_builder.rb', line 80
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.
92 93 94 95 96 |
# File 'lib/haml/attribute_builder.rb', line 92
def merge_values(key, *values)
values.inject(nil) do |to, from|
merge_value(key, to, from)
end
end
|
.verify_attribute_names!(attribute_names)
98 99 100 101 102 103 104 |
# File 'lib/haml/attribute_builder.rb', line 98
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
|