Class: Temple::HTML::AttributeMerger

Inherits:
Filter show all
Defined in:
lib/temple/html/attribute_merger.rb

Overview

This filter merges html attributes (e.g. used for id and class)

Constant Summary

Constants included from Utils

Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN

Instance Attribute Summary

Attributes included from Mixins::Options

#options

Instance Method Summary collapse

Methods inherited from Filter

#contains_nonempty_static?

Methods included from Dispatcher

#on_html_attr, #on_html_comment, #on_html_condcomment, #on_html_js, #on_html_tag

Methods included from Mixins::Options

included, #initialize

Methods included from Mixins::ControlFlowDispatcher

#on_block, #on_case, #on_cond, #on_if

Methods included from Mixins::EscapeDispatcher

#on_escape

Methods included from Mixins::CoreDispatcher

#on_capture, #on_multi

Methods included from Mixins::CompiledDispatcher

#call, #compile

Methods included from Utils

#empty_exp?, #escape_html, #escape_html_safe, #indent_dynamic, #unique_name

Instance Method Details

#on_html_attrs(*attrs) ⇒ Object



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
# File 'lib/temple/html/attribute_merger.rb', line 9

def on_html_attrs(*attrs)
  values = {}

  attrs.each do |_, _, name, value|
    name = name.to_s
    if values[name]
      raise(FilterError, "Multiple #{name} attributes specified") unless options[:merge_attrs][name]
      values[name] << value
    else
      values[name] = [value]
    end
  end

  attrs = values.map do |name, value|
    if (delimiter = options[:merge_attrs][name]) && value.size > 1
      exp = [:multi]
      if value.all? {|v| contains_nonempty_static?(v) }
        exp << value.first
        value[1..-1].each {|v| exp << [:static, delimiter] << v }
      else
        captures = unique_name
        exp << [:code, "#{captures} = []"]
        value.each_with_index {|v, i| exp << [:capture, "#{captures}[#{i}]", v] }
        exp << [:dynamic, "#{captures}.reject(&:empty?).join(#{delimiter.inspect})"]
      end
    else
      exp = value.first
    end
    [:html, :attr, name, exp]
  end
  [:html, :attrs, *attrs]
end