Sha256: c8aaa65860e9801be53c78e986191484ec161298791b944a2055423037784af0

Contents?: true

Size: 1010 Bytes

Versions: 11

Compression:

Stored size: 1010 Bytes

Contents

module Temple
  module HTML
    # This filter removes empty attributes
    # @api public
    class AttributeRemover < Filter
      define_options :remove_empty_attrs => %w(id class)

      def initialize(opts = {})
        super
        raise ArgumentError, "Option :remove_empty_attrs must be an Array of Strings" unless Array === options[:remove_empty_attrs] &&
          options[:remove_empty_attrs].all? {|a| String === a }
      end

      def on_html_attrs(*attrs)
        [:multi, *attrs.map {|attr| compile(attr) }]
      end

      def on_html_attr(name, value)
        return super unless options[:remove_empty_attrs].include?(name.to_s)

        if empty_exp?(value)
          value
        elsif contains_nonempty_static?(value)
          [:html, :attr, name, value]
        else
          tmp = unique_name
          [:multi,
           [:capture, tmp, compile(value)],
           [:if, "!#{tmp}.empty?",
            [:html, :attr, name, [:dynamic, tmp]]]]
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
temple-0.6.10 lib/temple/html/attribute_remover.rb
temple-0.6.9 lib/temple/html/attribute_remover.rb
temple-0.6.8 lib/temple/html/attribute_remover.rb
temple-0.6.7 lib/temple/html/attribute_remover.rb
temple-0.6.6 lib/temple/html/attribute_remover.rb
temple-0.6.5 lib/temple/html/attribute_remover.rb
temple-0.6.4 lib/temple/html/attribute_remover.rb
temple-0.6.3 lib/temple/html/attribute_remover.rb
temple-0.6.0 lib/temple/html/attribute_remover.rb
temple-0.5.5 lib/temple/html/attribute_remover.rb
temple-0.5.4 lib/temple/html/attribute_remover.rb