[Haml] [html2haml] Sort attributes.

This commit is contained in:
Nathan Weizenbaum 2009-10-05 03:26:42 -07:00
parent b9857c5575
commit ff0bdd254c
3 changed files with 5 additions and 9 deletions

View File

@ -44,6 +44,8 @@ including the line number and the offending character.
without spaces within the curly braces
(e.g. `%p{:foo => "bar"}` as opposed to `%p{ :foo => "bar" }`).
* Attributes are now sorted, to maintain a deterministic order.
## 2.2.7 (Unreleased)
* Fixed an `html2haml` issue where ERB attribute values

View File

@ -245,7 +245,7 @@ module Haml
# Returns a string representation of an attributes hash
# that's prettier than that produced by Hash#inspect
def haml_attributes(options)
attrs = attributes.map do |name, value|
attrs = attributes.sort.map do |name, value|
value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
name = name.index(/\W/) ? name.inspect : ":#{name}"
"#{name} => #{value}"

View File

@ -21,9 +21,9 @@ class Html2HamlTest < Test::Unit::TestCase
end
def test_should_have_pretty_attributes
assert_equal_attributes('%input{:type => "text", :name => "login"}',
assert_equal('%input{:name => "login", :type => "text"}',
render('<input type="text" name="login" />'))
assert_equal_attributes('%meta{"http-equiv" => "Content-Type", :content => "text/html"}',
assert_equal('%meta{:content => "text/html", "http-equiv" => "Content-Type"}',
render('<meta http-equiv="Content-Type" content="text/html" />'))
end
@ -201,10 +201,4 @@ HTML
def render_erb(text)
render(text, :erb => true)
end
def assert_equal_attributes(expected, result)
expected_attr, result_attr = [expected, result].map { |s| s.gsub!(/\{(.+)\}/, ''); $1.split(', ').sort }
assert_equal expected_attr, result_attr
assert_equal expected, result
end
end