[Haml] [html2haml] Support self-closing tag.

This commit is contained in:
Nathan Weizenbaum 2009-10-05 20:17:33 -07:00
parent ca96e0f559
commit d18f8bfdcd
3 changed files with 15 additions and 6 deletions

View File

@ -87,6 +87,9 @@ including the line number and the offending character.
bar
baz
* Self-closing tags (such as `<br />`) are now transformed into
self-closing Haml tags (like `%br/`).
* Attributes are now output in a more-standard format,
without spaces within the curly braces
(e.g. `%p{:foo => "bar"}` as opposed to `%p{ :foo => "bar" }`).

View File

@ -223,26 +223,28 @@ module Haml
return script_to_haml(tabs, options)
end
output = "#{tabulate(tabs)}"
output = tabulate(tabs)
if options[:erb] && name[0...5] == 'haml:'
return output + send("haml_tag_#{name[5..-1]}", CGI.unescapeHTML(self.inner_text))
end
output += "%#{name}" unless name == 'div' &&
output << "%#{name}" unless name == 'div' &&
(static_id?(options) || static_classname?(options))
if attributes
if static_id?(options)
output += "##{attributes['id']}"
output << "##{attributes['id']}"
remove_attribute('id')
end
if static_classname?(options)
attributes['class'].split(' ').each { |c| output += ".#{c}" }
remove_attribute('class')
end
output += haml_attributes(options) if attributes.length > 0
output << haml_attributes(options) if attributes.length > 0
end
output << "/" if empty? && !etag
if children && children.size == 1
child = children.first
if child.is_a?(::Hpricot::Text)

View File

@ -21,9 +21,9 @@ class Html2HamlTest < Test::Unit::TestCase
end
def test_should_have_pretty_attributes
assert_equal('%input{:name => "login", :type => "text"}',
assert_equal('%input{:name => "login", :type => "text"}/',
render('<input type="text" name="login" />'))
assert_equal('%meta{:content => "text/html", "http-equiv" => "Content-Type"}',
assert_equal('%meta{:content => "text/html", "http-equiv" => "Content-Type"}/',
render('<meta http-equiv="Content-Type" content="text/html" />'))
end
@ -51,6 +51,10 @@ HAML
HTML
end
def test_self_closing_tag
assert_equal("%foo/", render("<foo />"))
end
def test_inline_text
assert_equal("%p foo", render("<p>foo</p>"))
end