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

Stop sort for one dynamic attribute

This commit is contained in:
Takashi Kokubun 2015-11-21 23:39:18 +09:00
parent 1fd9afb4f2
commit 0161ab926d
3 changed files with 25 additions and 9 deletions

View file

@ -42,13 +42,28 @@ module Hamlit::AttributeBuilder
end
def build_class(escape_attrs, *values)
if values.size == 1
value = values.first
case
when value.is_a?(String)
# noop
when value.is_a?(Array)
value = value.select { |v| v }.sort.uniq.join(' ')
when value
value = value.to_s
else
return ''
end
return escape_html(escape_attrs, value)
end
classes = []
values.each do |value|
case
when value.is_a?(String)
classes += value.split(' ')
when value.is_a?(Array)
classes += value.select { |a| a }
classes += value.select { |v| v }
when value
classes << value.to_s
end

View file

@ -66,12 +66,6 @@ module Hamlit
end
def compile_class!(temple, key, values)
# NOTE: Haml does not sort classes if static
if values.size == 1 && (value = values.first)[0] == :static
temple << build_attr(key, *value)
return
end
build_code = attribute_builder(:class, values)
if values.all? { |type, exp| type == :static || StaticAnalyzer.static?(exp) }
temple << [:html, :attr, key, [:static, eval(build_code)]]

View file

@ -68,12 +68,19 @@ describe Hamlit::Engine do
it { assert_haml(%q|.a{ hash }|, locals: { hash: { class: 'd' } }) }
it { assert_haml(%q|.b{ hash, class: 'a' }(class='c')|, locals: { hash: { class: 'd' } }) }
it { assert_haml(%q|.b{ hash, class: 'a' }(class=klass)|, locals: { hash: { class: 'd' }, klass: nil }) }
it { assert_haml(%q|%div{ class: 'b a' }|) }
it { assert_haml(%q|%div{ class: klass }|, locals: { klass: 'b a' }) }
it { assert_haml(%q|%div(class='b a')|) }
it { assert_haml(%q|%div(class=klass)|, locals: { klass: 'b a' }) }
it { assert_haml(%q|%div{ class: [false, 'a', nil] }|) }
it { assert_haml(%q|%div{ class: %q[b a] }|) }
it { assert_haml(%q|%div{ class: %q[b a b] }|) }
end
describe 'incompatibility' do
it { assert_render(%Q|<div class=''></div>\n|, %q|%div{ class: nil }|) }
it { assert_render(%Q|<div class='a b'></div>\n|, %q|%div{ class: 'b a' }|) }
it { assert_render(%Q|<div class='a b'></div>\n|, %q|%div{ class: klass }|, locals: { klass: 'b a' }) }
end
end