[Haml] Support non-String values for class attributes.

This commit is contained in:
Nathan Weizenbaum 2010-02-15 14:16:13 -08:00
parent cc58c5548b
commit 4777a09436
3 changed files with 25 additions and 7 deletions

View File

@ -55,12 +55,16 @@ When the {file:HAML_REFERENCE.md#ugly-option `:ugly` option} is enabled,
{Haml::Helpers#haml_tag haml\_tag} and {Haml::Helpers#haml_concat haml\_concat}
won't do any indentation of their arguments.
### Object Reference Customization
### Basic Tag Improvements
It's now possible to customize the name used for {file:HAML_REFERENCE.md#object_reference_ object reference}
for a given object by implementing the `haml_object_ref` method on that object.
This method should return a string that will be used in place of the class name of the object
in the generated class and id.
* It's now possible to customize the name used for {file:HAML_REFERENCE.md#object_reference_ object reference}
for a given object by implementing the `haml_object_ref` method on that object.
This method should return a string that will be used in place of the class name of the object
in the generated class and id.
* All attribute values may be non-String types.
Their `#to_s` method will be called to convert them to strings.
Previously, this only worked for attributes other than `class`.
### More Powerful `:autoclose` Option

View File

@ -241,7 +241,7 @@ RUBY
# Destructively modifies both `to` and `from`.
#
# @param to [{String => String}] The attribute hash to merge into
# @param from [{String => String}] The attribute hash to merge from
# @param from [{String => #to_s}] The attribute hash to merge from
# @return [{String => String}] `to`, after being merged
def self.merge_attrs(to, from)
if to['id'] && from['id']
@ -252,7 +252,7 @@ RUBY
if to['class'] && from['class']
# Make sure we don't duplicate class names
from['class'] = (from['class'].split(' ') | to['class'].split(' ')).sort.join(' ')
from['class'] = (from['class'].to_s.split(' ') | to['class'].split(' ')).sort.join(' ')
elsif to['class'] || from['class']
from['class'] ||= to['class']
end

View File

@ -141,6 +141,20 @@ HTML
HAML
end
def test_attributes_with_to_s
assert_equal(<<HTML, render(<<HAML))
<p id='foo_2'></p>
<p class='2 foo'></p>
<p blaz='2'></p>
<p 2='2'></p>
HTML
%p#foo{:id => 1+1}
%p.foo{:class => 1+1}
%p{:blaz => 1+1}
%p{(1+1) => 1+1}
HAML
end
def test_nil_should_render_empty_tag
assert_equal("<div class='no_attributes'></div>",
render(".no_attributes{:nil => nil}").chomp)