-
-
Notifications
You must be signed in to change notification settings - Fork 500
/
code_attributes.rb
67 lines (64 loc) · 2.06 KB
/
code_attributes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Slim
# @api private
class CodeAttributes < Filter
define_options :merge_attrs
# Handle attributes expression `[:html, :attrs, *attrs]`
#
# @param [Array] attrs Array of temple expressions
# @return [Array] Compiled temple expression
def on_html_attrs(*attrs)
[:multi, *attrs.map {|a| compile(a) }]
end
# Handle attribute expression `[:html, :attr, name, value]`
#
# @param [String] name Attribute name
# @param [Array] value Value expression
# @return [Array] Compiled temple expression
def on_html_attr(name, value)
if value[0] == :slim && value[1] == :attrvalue && !options[:merge_attrs][name]
# We handle the attribute as a boolean attribute
escape, code = value[2], value[3]
case code
when 'true'
[:html, :attr, name, [:multi]]
when 'false', 'nil'
[:multi]
else
tmp = unique_name
[:multi,
[:code, "#{tmp} = #{code}"],
[:if, tmp,
[:if, "#{tmp} == true",
[:html, :attr, name, [:multi]],
[:html, :attr, name, [:escape, escape, [:dynamic, tmp]]]]]]
end
else
# Attribute with merging
@attr = name
super
end
end
# Handle attribute expression `[:slim, :attrvalue, escape, code]`
#
# @param [Boolean] escape Escape html
# @param [String] code Ruby code
# @return [Array] Compiled temple expression
def on_slim_attrvalue(escape, code)
# We perform attribute merging on Array values
if delimiter = options[:merge_attrs][@attr]
tmp = unique_name
[:multi,
[:code, "#{tmp} = #{code}"],
[:if, "Array === #{tmp}",
[:multi,
[:code, "#{tmp} = #{tmp}.flatten"],
[:code, "#{tmp}.map!(&:to_s)"],
[:code, "#{tmp}.reject!(&:empty?)"],
[:escape, escape, [:dynamic, "#{tmp}.join(#{delimiter.inspect})"]]],
[:escape, escape, [:dynamic, tmp]]]]
else
[:escape, escape, [:dynamic, code]]
end
end
end
end