describe Hamlit::Engine do describe 'old attributes' do it 'renders attributes' do assert_render(<<-HAML, <<-HTML) %span{class: 'foo'} bar HAML bar HTML end it 'renders attributes' do assert_render(<<-HAML, <<-HTML) %span{ data: 2 } bar HAML bar HTML end it 'renders attributes' do assert_render(<<-'HAML', <<-HTML) %span{ :class => 'foo' } bar HAML bar HTML end it 'renders attributes' do assert_render(<<-'HAML', <<-HTML) %span{ :class => 'foo', id: 'bar' } bar HAML bar HTML end it 'renders attributes' do assert_render(<<-'HAML', <<-HTML) %span{ :'data-disable' => true } bar HAML bar HTML end it 'accepts even illegal input for haml' do assert_render(<<-'HAML', <<-HTML, error_with: [:haml, :faml]) %span{ class: '}}}', id: '{}}' } }{ HAML }{ HTML end it 'accepts method call including comma' do assert_render(<<-'HAML', <<-HTML) %body{ class: "#{"ab".gsub(/a/, 'b')}", data: { confirm: 'really?', disable: true }, id: 'c'.gsub(/c/, 'a') } HAML HTML end it 'renders multi-byte chars as static attribute value' do assert_render(<<-'HAML', <<-HTML) %img{ alt: 'こんにちは' } HAML こんにちは HTML end it 'sorts static attributes by name' do assert_render(<<-HAML, <<-HTML) %span{ :foo => "bar", :hoge => "piyo"} %span{ :hoge => "piyo", :foo => "bar"} HAML HTML end describe 'runtime attributes' do it 'renders runtime hash attribute' do assert_render(<<-'HAML', <<-HTML) - hash = { foo: 'bar' } %span{ hash } HAML HTML end it 'renders multiples hashes' do assert_render(<<-'HAML', <<-HTML) - h1 = { a: 'b' } - h2 = { c: 'd' } - h3 = { e: 'f' } %span{ h1, h2, h3 } HAML HTML end it 'renders multiples hashes and literal hash' do assert_render(<<-'HAML', <<-HTML) - h1 = { a: 'b' } - h2 = { c: 'd' } - h3 = { e: 'f' } %span{ h1, h2, h3, g: 'h', i: 'j' } HAML HTML end end describe 'joinable attributes' do it 'joins class with a space' do assert_render(<<-'HAML', <<-HTML) - val = ['a', 'b', 'c'] %p{ class: val } %p{ class: %w[a b c] } %p{ class: ['a', 'b', 'c'] } HAML

HTML end it 'joins attribute class and element class', skipdoc: true do assert_render(<<-HAML, <<-HTML, compatible_only: :haml) .foo{ class: ['bar'] } .foo{ class: ['bar', nil] } .foo{ class: ['bar', 'baz'] } HAML
HTML end it 'joins id with an underscore' do assert_render(<<-'HAML', <<-HTML) - val = ['a', 'b', 'c'] %p{ id: val } %p{ id: %w[a b c] } %p{ id: ['a', 'b', 'c'] } HAML

HTML end it 'does not join others' do assert_render(<<-'HAML', <<-HTML) %a{ data: { value: [count: 1] } } HAML HTML end end describe 'deletable attributes' do it 'deletes attributes whose value is nil or false' do assert_render(<<-'HAML', <<-HTML) - hash = { checked: false } %input{ hash } %input{ checked: false } %input{ checked: nil } - checked = nil %input{ checked: checked } - checked = false %input{ checked: checked } HAML HTML end it 'deletes some limited attributes with dynamic value' do assert_render(<<-'HAML', <<-HTML) - val = false #foo.bar{ autofocus: val } #foo.bar{ checked: val } #foo.bar{ data: { disabled: val } } #foo.bar{ disabled: val } #foo.bar{ formnovalidate: val } #foo.bar{ multiple: val } #foo.bar{ readonly: val } #foo.bar{ required: val } HAML
HTML end it 'does not delete non-boolean attributes, for optimization' do assert_render(<<-'HAML', <<-HTML, compatible_only: []) / wontfix: Non-boolean attributes are not escaped for optimization. - val = false %a{ href: val } - val = nil %a{ href: val } / Boolean attributes are escaped correctly. - val = false %a{ disabled: val } - val = nil %a{ disabled: val } HAML HTML end end describe 'html escape' do it 'escapes attribute values on static attributes', skipdoc: true do assert_render(<<-'HAML', <<-HTML, compatible_only: :faml) %a{title: "'"} %a{title: "'\""} %a{href: '/search?foo=bar&hoge='} HAML HTML end it 'escapes attribute values on dynamic attributes', skipdoc: true do assert_render(<<-'HAML', <<-HTML, compatible_only: :faml) - title = "'\"" - href = '/search?foo=bar&hoge=' %a{title: title} %a{href: href} HAML HTML end it 'escapes attribute values on hash attributes', skipdoc: true do assert_render(<<-'HAML', <<-HTML, compatible_only: :faml) - title = { title: "'\"" } - href = { href: '/search?foo=bar&hoge=' } %a{ title } %a{ href } HAML HTML end end describe 'nested attributes' do it 'renders data attribute by hash' do assert_render(<<-'HAML', <<-HTML) - hash = { bar: 'baz' } %span.foo{ data: hash } HAML HTML end it 'renders true attributes', skipdoc: true do assert_render(<<-'HAML', <<-HTML, compatible_only: :haml) %span{ data: { disable: true } } bar HAML bar HTML end it 'renders nested hash whose value is variable' do assert_render(<<-'HAML', <<-HTML) - hash = { disable: true } %span{ data: hash } bar HAML bar HTML end it 'changes an underscore in a nested key to a hyphen' do assert_render(<<-'HAML', <<-HTML) %div{ data: { raw_src: 'foo' } } HAML
HTML end it 'changes an underscore in a nested dynamic attribute' do assert_render(<<-'HAML', <<-HTML) - hash = { raw_src: 'foo' } %div{ data: hash } HAML
HTML end end describe 'element class with attribute class' do it 'does not generate double classes' do assert_render(<<-HAML, <<-HTML) .item{ class: 'first' } HAML
HTML end it 'does not generate double classes for a variable' do assert_render(<<-HAML, <<-HTML) - val = 'val' .element{ class: val } HAML
HTML end it 'does not generate double classes for hash attributes' do assert_render(<<-HAML, <<-HTML) - hash = { class: 'val' } .element{ hash } HAML
HTML end end describe 'element id with attribute id' do it 'does not generate double ids' do assert_render(<<-HAML, <<-HTML) #item{ id: 'first' } HAML
HTML end it 'does not generate double ids for a variable' do assert_render(<<-HAML, <<-HTML) - val = 'first' #item{ id: val } HAML
HTML end it 'does not generate double ids for hash attributes' do assert_render(<<-HAML, <<-HTML) - hash = { id: 'first' } #item{ hash } HAML
HTML end it 'does not generate double ids and classes for hash attributes' do assert_render(<<-HAML, <<-HTML) - hash = { id: 'first', class: 'foo' } #item.bar{ hash } HAML
HTML end end end end