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=