lib/faml/compiler.rb in faml-0.3.2 vs lib/faml/compiler.rb in faml-0.3.3

- old
+ new

@@ -43,14 +43,14 @@ raise e end def self.find_and_preserve(input) # Taken from the original haml code - re = /<(#{options[:preserve].map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im + re = %r{<(#{options[:preserve].map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)}im input.to_s.gsub(re) do |s| - s =~ re # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible - "<#{$1}#{$2}>#{Helpers.preserve($3)}</#{$1}>" + m = s.match(re) # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible + "<#{m[1]}#{m[2]}>#{Helpers.preserve(m[3])}</#{m[1]}>" end end private @@ -164,11 +164,11 @@ else temple << [:static, "[#{ast.conditional}]>"] << [:mknl] << [:newline] end compile_children(ast, temple) unless ast.conditional.empty? - temple << [:static, "<![endif]"] + temple << [:static, '<![endif]'] end [:multi, [:html, :comment, temple]] end end @@ -226,11 +226,12 @@ def compile_attributes(text, static_id, static_class) if text.empty? return compile_static_id_and_class(static_id, static_class) end - if attrs = try_optimize_attributes(text, static_id, static_class) + attrs = try_optimize_attributes(text, static_id, static_class) + if attrs line_count = text.count("\n") return [:multi, [:html, :attrs, *attrs]].concat([[:newline]] * line_count) end # Slow version @@ -273,11 +274,11 @@ static_attributes, dynamic_attributes = build_optimized_attributes(parser, static_id, static_class) if static_attributes.nil? return nil end - if dynamic_attributes.has_key?('data') + if dynamic_attributes.key?('data') # XXX: Quit optimization... return nil end if text.include?("\n") && !dynamic_attributes.empty? @@ -285,11 +286,11 @@ # https://github.com/eagletmt/faml/issues/18 return nil end (static_attributes.keys + dynamic_attributes.keys).sort.flat_map do |k| - if static_attributes.has_key?(k) + if static_attributes.key?(k) compile_static_attribute(k, static_attributes[k]) else compile_dynamic_attribute(k, dynamic_attributes[k]) end end @@ -329,11 +330,11 @@ def build_optimized_dynamic_attributes(parser, static_attributes) dynamic_attributes = {} parser.dynamic_attributes.each do |k, v| k = k.to_s - if static_attributes.has_key?(k) + if static_attributes.key?(k) if StaticHashParser::SPECIAL_ATTRIBUTES.include?(k) # XXX: Quit optimization return nil end end @@ -341,21 +342,27 @@ end dynamic_attributes end def compile_static_attribute(key, value) - case - when value == true - [[:haml, :attr, key, [:multi]]] - when value == false || value == nil - [[:multi]] - when value.is_a?(Hash) && key == 'data' + if value.is_a?(Hash) && key == 'data' data = AttributeBuilder.normalize_data(value) data.keys.sort.map do |k| - [:haml, :attr, "data-#{k}", [:static, Temple::Utils.escape_html(data[k])]] + compile_static_simple_attribute("data-#{k}", data[k]) end else - [[:haml, :attr, key, [:static, Temple::Utils.escape_html(value)]]] + [compile_static_simple_attribute(key, value)] + end + end + + def compile_static_simple_attribute(key, value) + case + when value == true + [:haml, :attr, key, [:multi]] + when value == false || value.nil? + [:multi] + else + [:haml, :attr, key, [:static, Temple::Utils.escape_html(value)]] end end def compile_dynamic_attribute(key, value) [[:haml, :attr, key, [:dvalue, value]]]