2007-07-24 09:15:27 +00:00
|
|
|
#!/usr/bin/env ruby
|
2008-06-08 09:54:36 -07:00
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
2007-07-24 09:15:27 +00:00
|
|
|
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
|
2009-10-05 20:17:33 -07:00
|
|
|
assert_equal('%input{:name => "login", :type => "text"}/',
|
2008-11-29 11:52:55 -08:00
|
|
|
render('<input type="text" name="login" />'))
|
2009-10-05 20:17:33 -07:00
|
|
|
assert_equal('%meta{:content => "text/html", "http-equiv" => "Content-Type"}/',
|
2008-11-29 11:52:55 -08:00
|
|
|
render('<meta http-equiv="Content-Type" content="text/html" />'))
|
2007-07-24 09:15:27 +00:00
|
|
|
end
|
|
|
|
|
2009-10-05 03:42:15 -07:00
|
|
|
def test_interpolation
|
|
|
|
assert_equal('Foo \#{bar} baz', render('Foo #{bar} baz'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_interpolation_in_attrs
|
2009-10-05 03:24:35 -07:00
|
|
|
assert_equal('%p{:foo => "\#{bar} baz"}', render('<p foo="#{bar} baz"></p>'))
|
2009-10-05 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
2009-10-04 21:07:57 -07:00
|
|
|
def test_cdata
|
|
|
|
assert_equal(<<HAML.strip, render(<<HTML))
|
|
|
|
%p
|
|
|
|
:cdata
|
|
|
|
<a foo="bar" baz="bang">
|
|
|
|
<div id="foo">flop</div>
|
|
|
|
</a>
|
|
|
|
HAML
|
|
|
|
<p><![CDATA[
|
|
|
|
<a foo="bar" baz="bang">
|
|
|
|
<div id="foo">flop</div>
|
|
|
|
</a>
|
|
|
|
]]></p>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-05 20:17:33 -07:00
|
|
|
def test_self_closing_tag
|
|
|
|
assert_equal("%foo/", render("<foo />"))
|
|
|
|
end
|
|
|
|
|
2009-10-04 21:36:05 -07:00
|
|
|
def test_inline_text
|
|
|
|
assert_equal("%p foo", render("<p>foo</p>"))
|
|
|
|
end
|
|
|
|
|
2009-10-05 19:56:40 -07:00
|
|
|
def test_inline_comment
|
|
|
|
assert_equal("/ foo", render("<!-- foo -->"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_non_inline_comment
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
/
|
|
|
|
Foo
|
|
|
|
Bar
|
|
|
|
HAML
|
|
|
|
<!-- Foo
|
|
|
|
Bar -->
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-04 21:36:05 -07:00
|
|
|
def test_non_inline_text
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
%p
|
|
|
|
foo
|
|
|
|
HAML
|
|
|
|
<p>
|
|
|
|
foo
|
|
|
|
</p>
|
|
|
|
HTML
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
%p
|
|
|
|
foo
|
|
|
|
HAML
|
|
|
|
<p>
|
|
|
|
foo</p>
|
|
|
|
HTML
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
%p
|
|
|
|
foo
|
|
|
|
HAML
|
|
|
|
<p>foo
|
|
|
|
</p>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-05 17:53:35 -07:00
|
|
|
def test_script_tag
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
:javascript
|
|
|
|
function foo() {
|
|
|
|
return "12" & "13";
|
|
|
|
}
|
|
|
|
HAML
|
|
|
|
<script type="text/javascript">
|
|
|
|
function foo() {
|
|
|
|
return "12" & "13";
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_script_tag_with_cdata
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
:javascript
|
|
|
|
function foo() {
|
|
|
|
return "&";
|
|
|
|
}
|
|
|
|
HAML
|
|
|
|
<script type="text/javascript">
|
|
|
|
<![CDATA[
|
|
|
|
function foo() {
|
|
|
|
return "&";
|
|
|
|
}
|
|
|
|
]]>
|
|
|
|
</script>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-05 19:44:03 -07:00
|
|
|
def test_pre
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
%pre
|
|
|
|
:preserve
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
baz
|
|
|
|
HAML
|
|
|
|
<pre>foo
|
|
|
|
bar
|
|
|
|
baz</pre>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pre_code
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
%pre
|
|
|
|
%code
|
|
|
|
:preserve
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
baz
|
|
|
|
HAML
|
|
|
|
<pre><code>foo
|
|
|
|
bar
|
|
|
|
baz</code></pre>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_code_without_pre
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
%code
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
baz
|
|
|
|
HAML
|
|
|
|
<code>foo
|
|
|
|
bar
|
|
|
|
baz</code>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-10 17:42:57 -07:00
|
|
|
def test_conditional_comment
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
/[if foo]
|
|
|
|
bar
|
|
|
|
baz
|
|
|
|
HAML
|
|
|
|
<!--[if foo]>
|
|
|
|
bar
|
|
|
|
baz
|
|
|
|
<![endif]-->
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inline_conditional_comment
|
|
|
|
assert_equal(<<HAML.rstrip, render(<<HTML))
|
|
|
|
/[if foo] bar baz
|
|
|
|
HAML
|
|
|
|
<!--[if foo]> bar baz <![endif]-->
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-04 21:17:19 -07:00
|
|
|
## ERB
|
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_erb
|
|
|
|
assert_equal '- foo = bar', render_erb('<% foo = bar %>')
|
|
|
|
assert_equal '- foo = bar', render_erb('<% foo = bar -%>')
|
|
|
|
assert_equal '= h @item.title', render_erb('<%=h @item.title %>')
|
|
|
|
assert_equal '= h @item.title', render_erb('<%=h @item.title -%>')
|
2007-07-24 09:15:27 +00:00
|
|
|
end
|
2009-10-04 21:07:57 -07:00
|
|
|
|
2009-10-04 21:41:30 -07:00
|
|
|
def test_inline_erb
|
|
|
|
assert_equal("%p= foo", render_erb("<p><%= foo %></p>"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_non_inline_erb
|
|
|
|
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
|
|
|
|
%p
|
|
|
|
= foo
|
|
|
|
HAML
|
|
|
|
<p>
|
|
|
|
<%= foo %>
|
|
|
|
</p>
|
|
|
|
HTML
|
|
|
|
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
|
|
|
|
%p
|
|
|
|
= foo
|
|
|
|
HAML
|
|
|
|
<p>
|
|
|
|
<%= foo %></p>
|
|
|
|
HTML
|
|
|
|
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
|
|
|
|
%p
|
|
|
|
= foo
|
|
|
|
HAML
|
|
|
|
<p><%= foo %>
|
|
|
|
</p>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-05 17:12:56 -07:00
|
|
|
def test_erb_in_cdata
|
|
|
|
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
|
|
|
|
:cdata
|
|
|
|
Foo \#{bar} baz
|
|
|
|
HAML
|
|
|
|
<![CDATA[Foo <%= bar %> baz]]>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-05 17:53:35 -07:00
|
|
|
def test_erb_in_script
|
|
|
|
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
|
|
|
|
:javascript
|
|
|
|
function foo() {
|
|
|
|
return \#{foo.to_json};
|
|
|
|
}
|
|
|
|
HAML
|
|
|
|
<script type="text/javascript">
|
|
|
|
function foo() {
|
|
|
|
return <%= foo.to_json %>;
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2009-10-05 16:24:38 -07:00
|
|
|
def test_erb_in_line
|
|
|
|
assert_equal 'foo bar #{baz}', render_erb('foo bar <%= baz %>')
|
|
|
|
assert_equal 'foo bar #{baz}! Bang.', render_erb('foo bar <%= baz %>! Bang.')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_erb_multi_in_line
|
|
|
|
assert_equal('foo bar #{baz}! Bang #{bop}.',
|
|
|
|
render_erb('foo bar <%= baz %>! Bang <%= bop %>.'))
|
|
|
|
assert_equal('foo bar #{baz}#{bop}!',
|
|
|
|
render_erb('foo bar <%= baz %><%= bop %>!'))
|
|
|
|
end
|
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_erb_with_html_special_chars
|
2008-10-16 10:37:57 +02:00
|
|
|
assert_equal '= 3 < 5 ? "OK" : "Your computer is b0rken"',
|
2009-10-04 21:15:16 -07:00
|
|
|
render_erb('<%= 3 < 5 ? "OK" : "Your computer is b0rken" %>')
|
2008-10-16 10:37:57 +02:00
|
|
|
end
|
2009-10-04 21:07:57 -07:00
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_erb_in_class_attribute
|
2009-10-05 03:24:35 -07:00
|
|
|
assert_equal "%div{:class => dyna_class} I have a dynamic attribute",
|
2009-10-04 21:15:16 -07:00
|
|
|
render_erb('<div class="<%= dyna_class %>">I have a dynamic attribute</div>')
|
2008-10-16 11:37:58 +02:00
|
|
|
end
|
2009-10-04 21:07:57 -07:00
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_erb_in_id_attribute
|
2009-10-05 03:24:35 -07:00
|
|
|
assert_equal "%div{:id => dyna_id} I have a dynamic attribute",
|
2009-10-04 21:15:16 -07:00
|
|
|
render_erb('<div id="<%= dyna_id %>">I have a dynamic attribute</div>')
|
2008-10-16 11:37:58 +02:00
|
|
|
end
|
2009-10-04 21:07:57 -07:00
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_erb_in_attribute_results_in_string_interpolation
|
2009-10-05 03:24:35 -07:00
|
|
|
assert_equal('%div{:id => "item_#{i}"} Ruby string interpolation FTW',
|
2009-10-04 21:36:05 -07:00
|
|
|
render_erb('<div id="item_<%= i %>">Ruby string interpolation FTW</div>'))
|
2008-10-16 11:42:23 +02:00
|
|
|
end
|
2009-10-04 21:07:57 -07:00
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_erb_in_attribute_with_trailing_content
|
2009-10-05 03:24:35 -07:00
|
|
|
assert_equal('%div{:class => "#{12}!"} Bang!',
|
2009-10-04 21:36:05 -07:00
|
|
|
render_erb('<div class="<%= 12 %>!">Bang!</div>'))
|
2008-10-16 22:30:57 +02:00
|
|
|
end
|
2009-10-04 21:07:57 -07:00
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_erb_in_html_escaped_attribute
|
2009-10-05 03:24:35 -07:00
|
|
|
assert_equal '%div{:class => "foo"} Bang!',
|
2009-10-04 21:36:05 -07:00
|
|
|
render_erb('<div class="<%= "foo" %>">Bang!</div>')
|
2009-10-05 03:39:40 -07:00
|
|
|
end
|
2009-10-04 21:07:57 -07:00
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_erb_in_attribute_to_multiple_interpolations
|
2009-10-05 03:24:35 -07:00
|
|
|
assert_equal('%div{:class => "#{12} + #{13}"} Math is super',
|
2009-10-04 21:36:05 -07:00
|
|
|
render_erb('<div class="<%= 12 %> + <%= 13 %>">Math is super</div>'))
|
2008-10-16 23:37:08 +02:00
|
|
|
end
|
2007-07-24 09:15:27 +00:00
|
|
|
|
2009-10-04 21:07:57 -07:00
|
|
|
def test_whitespace_eating_erb_tags
|
2009-10-04 21:15:16 -07:00
|
|
|
assert_equal '- form_for', render_erb('<%- form_for -%>')
|
2009-03-26 23:11:20 -07:00
|
|
|
end
|
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_interpolation_in_erb
|
|
|
|
assert_equal('= "Foo #{bar} baz"', render_erb('<%= "Foo #{bar} baz" %>'))
|
2009-10-05 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def test_interpolation_in_erb_attrs
|
2009-10-05 03:24:35 -07:00
|
|
|
assert_equal('%p{:foo => "#{bar} baz"}',
|
2009-10-04 21:15:16 -07:00
|
|
|
render_erb('<p foo="<%= "#{bar} baz" %>"></p>'))
|
2009-10-05 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
2009-09-23 15:54:46 -07:00
|
|
|
# Encodings
|
|
|
|
|
|
|
|
unless Haml::Util.ruby1_8?
|
|
|
|
def test_encoding_error
|
|
|
|
render("foo\nbar\nb\xFEaz".force_encoding("utf-8"))
|
|
|
|
assert(false, "Expected exception")
|
|
|
|
rescue Haml::Error => e
|
|
|
|
assert_equal(3, e.line)
|
|
|
|
assert_equal('Invalid UTF-8 character "\xFE"', e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ascii_incompatible_encoding_error
|
|
|
|
template = "foo\nbar\nb_z".encode("utf-16le")
|
|
|
|
template[9] = "\xFE".force_encoding("utf-16le")
|
|
|
|
render(template)
|
|
|
|
assert(false, "Expected exception")
|
|
|
|
rescue Haml::Error => e
|
|
|
|
assert_equal(3, e.line)
|
|
|
|
assert_equal('Invalid UTF-16LE character "\xFE"', e.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-07-24 09:15:27 +00:00
|
|
|
protected
|
|
|
|
|
2008-11-29 11:51:04 -08:00
|
|
|
def render(text, options = {})
|
|
|
|
Haml::HTML.new(text, options).render.rstrip
|
|
|
|
end
|
2007-07-24 09:15:27 +00:00
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
def render_erb(text)
|
|
|
|
render(text, :erb => true)
|
2008-11-29 11:51:04 -08:00
|
|
|
end
|
2007-07-24 09:15:27 +00:00
|
|
|
end
|