Sha256: c740a2911fd1ab47817e50da4b59c8d1bae9f85fabaceb584825acc5d3ea8bd0

Contents?: true

Size: 1.46 KB

Versions: 10

Compression:

Stored size: 1.46 KB

Contents

module Temple
  module HTML
    # This filter merges html attributes (e.g. used for id and class)
    # @api public
    class AttributeMerger < Filter
      define_options merge_attrs: {'id' => '_', 'class' => ' '}

      def on_html_attrs(*attrs)
        names = []
        values = {}

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

        attrs = names.map do |name|
          value = values[name]
          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 }
              [:html, :attr, name, exp]
            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
            [:html, :attr, name, exp]
          else
            [:html, :attr, name, value.first]
          end
        end

        [:html, :attrs, *attrs]
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
brakeman-3.3.0 bundle/ruby/2.3.0/gems/temple-0.7.6/lib/temple/html/attribute_merger.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/temple-0.7.6/lib/temple/html/attribute_merger.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/temple-0.7.6/lib/temple/html/attribute_merger.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/temple-0.7.6/lib/temple/html/attribute_merger.rb
temple-0.7.6 lib/temple/html/attribute_merger.rb
temple-0.7.5 lib/temple/html/attribute_merger.rb
temple-0.7.4 lib/temple/html/attribute_merger.rb
temple-0.7.3 lib/temple/html/attribute_merger.rb
temple-0.7.2 lib/temple/html/attribute_merger.rb
temple-0.7.1 lib/temple/html/attribute_merger.rb