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)
%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 runtime hash attribute' do
assert_render(<<-'HAML', <<-HTML)
- hash = { foo: 'bar' }
%span{ hash }
HAML
HTML
end
it 'renders multi-byte chars as static attribute value' do
assert_render(<<-'HAML', <<-HTML)
%img{ alt: 'こんにちは' }
HAML
HTML
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 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 }
HAML
HTML
end
it 'does not delete some attributes, for optimization' do
assert_render(<<-'HAML', <<-HTML)
- val = false
%a{ href: val }
- val = nil
%a{ href: val }
HAML
HTML
end
end
describe 'html escape' do
it 'escapes attribute values on static attributes' do
assert_render(<<-'HAML', <<-HTML)
%a{title: "'"}
%a{title: "'\""}
%a{href: '/search?foo=bar&hoge='}
HAML
HTML
end
it 'escapes attribute values on dynamic attributes' do
assert_render(<<-'HAML', <<-HTML)
- title = "'\""
- href = '/search?foo=bar&hoge='
%a{title: title}
%a{href: href}
HAML
HTML
end
it 'escapes attribute values on hash attributes' do
assert_render(<<-'HAML', <<-HTML)
- 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' do
assert_render(<<-'HAML', <<-HTML)
%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