1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Trim falsy value in array attribute

This commit is contained in:
Takashi Kokubun 2015-11-13 22:51:56 +09:00
parent e2b9a20c62
commit 49c0fe2d60
3 changed files with 49 additions and 31 deletions

View file

@ -17,7 +17,7 @@ module Hamlit
end end
def build_id(*values) def build_id(*values)
values.select { |v| v }.join('_') values.flatten.select { |v| v }.join('_')
end end
def build_class(*values) def build_class(*values)

View file

@ -153,26 +153,33 @@ class EngineTest < Haml::TestCase
end end
def test_class_attr_with_array def test_class_attr_with_array
skip '[INCOMPATIBILITY] Hamlit deletes only boolean attributes'
assert_equal("<p class='a b'>foo</p>\n", render("%p{:class => %w[a b]} foo")) # basic assert_equal("<p class='a b'>foo</p>\n", render("%p{:class => %w[a b]} foo")) # basic
assert_equal("<p class='a b css'>foo</p>\n", render("%p.css{:class => %w[a b]} foo")) # merge with css assert_equal("<p class='a b css'>foo</p>\n", render("%p.css{:class => %w[a b]} foo")) # merge with css
assert_equal("<p class='b css'>foo</p>\n", render("%p.css{:class => %w[css b]} foo")) # merge uniquely assert_equal("<p class='b css'>foo</p>\n", render("%p.css{:class => %w[css b]} foo")) # merge uniquely
assert_equal("<p class='a b c d'>foo</p>\n", render("%p{:class => [%w[a b], %w[c d]]} foo")) # flatten assert_equal("<p class='a b c d'>foo</p>\n", render("%p{:class => [%w[a b], %w[c d]]} foo")) # flatten
assert_equal("<p class='a b'>foo</p>\n", render("%p{:class => [:a, :b] } foo")) # stringify assert_equal("<p class='a b'>foo</p>\n", render("%p{:class => [:a, :b] } foo")) # stringify
assert_equal("<p>foo</p>\n", render("%p{:class => [nil, false] } foo")) # strip falsey # [INCOMPATIBILITY] Hamlit deletes only boolean attributes
# assert_equal("<p>foo</p>\n", render("%p{:class => [nil, false] } foo")) # strip falsey
assert_equal("<p class=''>foo</p>\n", render("%p{:class => [nil, false] } foo")) # strip falsey
assert_equal("<p class='a'>foo</p>\n", render("%p{:class => :a} foo")) # single stringify assert_equal("<p class='a'>foo</p>\n", render("%p{:class => :a} foo")) # single stringify
assert_equal("<p>foo</p>\n", render("%p{:class => false} foo")) # single falsey # [INCOMPATIBILITY] Hamlit deletes only boolean attributes
# assert_equal("<p>foo</p>\n", render("%p{:class => false} foo")) # single falsey
assert_equal("<p class=''>foo</p>\n", render("%p{:class => false} foo")) # single falsey
assert_equal("<p class='a b html'>foo</p>\n", render("%p(class='html'){:class => %w[a b]} foo")) # html attrs assert_equal("<p class='a b html'>foo</p>\n", render("%p(class='html'){:class => %w[a b]} foo")) # html attrs
end end
def test_id_attr_with_array; skip # array attribute def test_id_attr_with_array
assert_equal("<p id='a_b'>foo</p>\n", render("%p{:id => %w[a b]} foo")) # basic assert_equal("<p id='a_b'>foo</p>\n", render("%p{:id => %w[a b]} foo")) # basic
assert_equal("<p id='css_a_b'>foo</p>\n", render("%p#css{:id => %w[a b]} foo")) # merge with css assert_equal("<p id='css_a_b'>foo</p>\n", render("%p#css{:id => %w[a b]} foo")) # merge with css
assert_equal("<p id='a_b_c_d'>foo</p>\n", render("%p{:id => [%w[a b], %w[c d]]} foo")) # flatten assert_equal("<p id='a_b_c_d'>foo</p>\n", render("%p{:id => [%w[a b], %w[c d]]} foo")) # flatten
assert_equal("<p id='a_b'>foo</p>\n", render("%p{:id => [:a, :b] } foo")) # stringify assert_equal("<p id='a_b'>foo</p>\n", render("%p{:id => [:a, :b] } foo")) # stringify
assert_equal("<p>foo</p>\n", render("%p{:id => [nil, false] } foo")) # strip falsey # [INCOMPATIBILITY] Hamlit deletes only boolean attributes
# assert_equal("<p>foo</p>\n", render("%p{:id => [nil, false] } foo")) # strip falsey
assert_equal("<p id=''>foo</p>\n", render("%p{:id => [nil, false] } foo")) # strip falsey
assert_equal("<p id='a'>foo</p>\n", render("%p{:id => :a} foo")) # single stringify assert_equal("<p id='a'>foo</p>\n", render("%p{:id => :a} foo")) # single stringify
assert_equal("<p>foo</p>\n", render("%p{:id => false} foo")) # single falsey # [INCOMPATIBILITY] Hamlit deletes only boolean attributes
# assert_equal("<p>foo</p>\n", render("%p{:id => false} foo")) # single falsey
assert_equal("<p id=''>foo</p>\n", render("%p{:id => false} foo")) # single falsey
assert_equal("<p id='html_a_b'>foo</p>\n", render("%p(id='html'){:id => %w[a b]} foo")) # html attrs assert_equal("<p id='html_a_b'>foo</p>\n", render("%p(id='html'){:id => %w[a b]} foo")) # html attrs
end end

View file

@ -10,30 +10,41 @@ describe Hamlit::Engine do
it { assert_render(%q|%a{ href: "'\"" }|, %Q|<a href='&#39;&quot;'></a>\n|) } it { assert_render(%q|%a{ href: "'\"" }|, %Q|<a href='&#39;&quot;'></a>\n|) }
it { assert_inline(%Q|%a{ href: '/search?foo=bar&hoge=<fuga>' }|) } it { assert_inline(%Q|%a{ href: '/search?foo=bar&hoge=<fuga>' }|) }
specify 'id attributes' do describe 'id attributes' do
assert_haml(<<-HAML) specify 'compatilibity' do
#a assert_haml(<<-HAML)
#a{ id: [] } #a
#a{ id: nil } #a{ id: nil }
#a{ id: nil }(id=nil) #a{ id: nil }(id=nil)
#a{ id: false } #a{ id: false }
#a{ id: 'b' } #a{ id: 'b' }
#b{ id: 'a' } #b{ id: 'a' }
- id = 'c' - id = 'c'
#a{ id: 'b' }(id=id) #a{ id: 'b' }(id=id)
- id = 'b' - id = 'b'
#c{ id: a = 'a' }(id=id) #c{ id: a = 'a' }(id=id)
- id = 'a' - id = 'a'
#d#c{ id: a = 'b' }(id=id) #d#c{ id: a = 'b' }(id=id)
#d#c{ id: [] }(id=id) #d#c{ id: %w[b e] }(id=id)
#d#c{ id: %w[b e] }(id=id) - hash = { id: 'a' }
- hash = { id: 'a' } %div{ hash }
%div{ hash } #b{ hash }
#b{ hash } #b{ hash }(id='c')
#b{ hash }(id='c') - id = 'c'
- id = 'c' #b{ hash }(id=id)
#b{ hash }(id=id) HAML
HAML end
specify 'incompatibility' do
assert_render(%q|#a{ id: [] }|, %Q|<div id='a'></div>\n|)
assert_render(%q|%a{ id: [nil, false] }|, %Q|<a id=''></a>\n|)
assert_render(<<-HAML, <<-HTML)
- id = 'a'
#d#c{ id: [] }(id=id)
HAML
<div id='c_a'></div>
HTML
end
end end
specify 'class attributes' do specify 'class attributes' do