Adding pretty attributes + tests to html2haml. Patch by Mislav Marohnic.
git-svn-id: svn://hamptoncatlin.com/haml/trunk@573 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
39e1abe669
commit
0b9b2dd742
|
@ -138,9 +138,9 @@ module Haml
|
|||
if attributes
|
||||
output += "##{attributes['id']}" if attributes['id']
|
||||
attributes['class'].split(' ').each { |c| output += ".#{c}" } if attributes['class']
|
||||
attributes.delete("id")
|
||||
attributes.delete("class")
|
||||
output += attributes.inspect if attributes.length > 0
|
||||
remove_attribute('id')
|
||||
remove_attribute('class')
|
||||
output += haml_attributes if attributes.length > 0
|
||||
end
|
||||
|
||||
output += "/" if children.length == 0
|
||||
|
@ -152,6 +152,18 @@ module Haml
|
|||
|
||||
output
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Returns a string representation of an attributes hash
|
||||
# that's prettier than that produced by Hash#inspect
|
||||
def haml_attributes
|
||||
attrs = attributes.map do |name, value|
|
||||
name = name.index(/\W/) ? name.inspect : ":#{name}"
|
||||
"#{name} => #{value.inspect}"
|
||||
end
|
||||
"{ #{attrs.join(', ')} }"
|
||||
end
|
||||
end
|
||||
|
||||
def self.haml_tag_loud(text)
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'test/unit'
|
||||
require File.dirname(__FILE__) + '/../../lib/haml'
|
||||
require 'haml/html'
|
||||
|
||||
class Html2HamlTest < Test::Unit::TestCase
|
||||
|
||||
def test_empty_render_should_remain_empty
|
||||
assert_equal '', render('')
|
||||
end
|
||||
|
||||
def test_id_and_class_should_be_removed_from_hash
|
||||
assert_equal '%span#foo.bar', render('<span id="foo" class="bar"> </span>')
|
||||
end
|
||||
|
||||
def test_no_tag_name_for_div_if_class_or_id_is_present
|
||||
assert_equal '#foo', render('<div id="foo"> </div>')
|
||||
assert_equal '.foo', render('<div class="foo"> </div>')
|
||||
end
|
||||
|
||||
def test_multiple_class_names
|
||||
assert_equal '.foo.bar.baz', render('<div class=" foo bar baz "> </div>')
|
||||
end
|
||||
|
||||
def test_should_have_pretty_attributes
|
||||
assert_equal_attributes('%input{ :type => "text", :name => "login" }/',
|
||||
render('<input type="text" name="login" />'))
|
||||
assert_equal_attributes('%meta{ "http-equiv" => "Content-Type", :content => "text/html" }/',
|
||||
render('<meta http-equiv="Content-Type" content="text/html" />'))
|
||||
assert_equal_attributes('%div{ "xml:lang" => "hr" }/',
|
||||
render('<div xml:lang="hr" />'))
|
||||
end
|
||||
|
||||
def test_sqml_comment
|
||||
assert_equal "/\n IE sucks", render('<!-- IE sucks -->')
|
||||
end
|
||||
|
||||
def test_rhtml
|
||||
assert_equal '- foo = bar', render_rhtml('<% foo = bar %>')
|
||||
assert_equal '- foo = bar', render_rhtml('<% foo = bar -%>')
|
||||
assert_equal '= h @item.title', render_rhtml('<%=h @item.title %>')
|
||||
assert_equal '= h @item.title', render_rhtml('<%=h @item.title -%>')
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def render(text, options = {})
|
||||
Haml::HTML.new(text, options).render.rstrip
|
||||
end
|
||||
|
||||
def render_rhtml(text)
|
||||
render(text, :rhtml => true)
|
||||
end
|
||||
|
||||
def assert_equal_attributes(expected, result)
|
||||
assert_equal *[expected, result].map { |s| s.gsub!(/\{ (.+) \}/, ''); $1.split(', ').sort }
|
||||
assert_equal expected, result
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue