opal/opal-haml.rb in opal-haml-0.4.0 vs opal/opal-haml.rb in opal-haml-0.4.1
- old
+ new
@@ -7,22 +7,38 @@
# allow tags in haml to have dynamic attributes
def attributes(class_id, obj_ref, *attributes_hashes)
attributes = class_id
attributes_hashes.each do |hash|
- attributes.update hash
+ attributes.update hash do |_, oldval, newval|
+ Array(oldval) + Array(newval)
+ end
end
- result = attributes.collect do |attr, value|
- if value == true
- next " #{attr}"
- elsif value == false
- next
+ result = []
+ _render_attributes attributes, result, ''
+
+ result.join
+ end
+
+ private
+
+ def _render_attributes(attributes, out, prefix)
+ attributes.each do |attr, value|
+ attr_name = prefix + attr
+
+ case value
+ when true then out << " #{attr_name}"
+ when false, nil then next
+ when Hash then _render_attributes(value, out, attr_name + '-')
else
- " #{attr}='#{value}'"
+ value = value.compact.join ' ' if value.is_a? Array
+ out << " #{attr_name}='#{_attribute_escape value}'"
end
end
+ end
- result.join
+ def _attribute_escape(value)
+ value.to_s.gsub /['"&<>]/, '"' => '"', "'" => ''', '&' => '&', '<' => '<', '>' => '>'
end
end
end