lib/faml/attribute_compiler.rb in faml-0.5.1 vs lib/faml/attribute_compiler.rb in faml-0.6.0

- old
+ new

@@ -2,23 +2,29 @@ require_relative 'object_ref' module Faml class AttributeCompiler def compile(ast) - if !ast.object_ref && ast.attributes.empty? + if !ast.object_ref && !ast.old_attributes && !ast.new_attributes return compile_static_id_and_class(ast.static_id, ast.static_class) end unless ast.object_ref - attrs = try_optimize_attributes(ast.attributes, ast.static_id, ast.static_class) + attrs = try_optimize_attributes(ast.old_attributes, ast.new_attributes, ast.static_id, ast.static_class) if attrs - line_count = ast.attributes.count("\n") + line_count = 0 + if ast.old_attributes + line_count += ast.old_attributes.count("\n") + end + if ast.new_attributes + line_count += ast.new_attributes.count("\n") + end return [:multi, [:html, :attrs, *attrs]].concat([[:newline]] * line_count) end end - compile_slow_attributes(ast.attributes, ast.static_id, ast.static_class, ast.object_ref) + compile_slow_attributes(ast.old_attributes, ast.new_attributes, ast.static_id, ast.static_class, ast.object_ref) end private def compile_static_id_and_class(static_id, static_class) @@ -30,12 +36,12 @@ html_attrs << [:haml, :attr, 'id', [:static, static_id]] end end end - def try_optimize_attributes(text, static_id, static_class) - static_attributes, dynamic_attributes = AttributeOptimizer.new.try_optimize(text, static_id, static_class) + def try_optimize_attributes(old_attributes, new_attributes, static_id, static_class) + static_attributes, dynamic_attributes = AttributeOptimizer.new.try_optimize(old_attributes, new_attributes, static_id, static_class) if static_attributes (static_attributes.keys + dynamic_attributes.keys).sort.flat_map do |k| if static_attributes.key?(k) compile_static_attribute(k, static_attributes[k]) else @@ -69,11 +75,11 @@ def compile_dynamic_attribute(key, value) [[:haml, :attr, key, [:dvalue, value]]] end - def compile_slow_attributes(text, static_id, static_class, object_ref) + def compile_slow_attributes(old_attributes, new_attributes, static_id, static_class, object_ref) h = {} unless static_class.empty? h[:class] = static_class.split(/ +/) end unless static_id.empty? @@ -87,11 +93,14 @@ codes << 'nil' end unless h.empty? codes << h.inspect end - unless text.empty? - codes << text + if new_attributes + codes << "{#{new_attributes}}" + end + if old_attributes + codes << old_attributes end [:haml, :attrs, codes.join(', ')] end end end