Sha256: 6b5a867453abce5fea40af0175d04d4fda78b35c683e3ab8f988fbfc2a4ce3c8

Contents?: true

Size: 1.01 KB

Versions: 8

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true
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

8 entries across 8 versions & 2 rubygems

Version Path
brakeman-7.0.0 bundle/ruby/3.1.0/gems/temple-0.10.3/lib/temple/html/attribute_remover.rb
brakeman-6.2.2 bundle/ruby/3.1.0/gems/temple-0.10.3/lib/temple/html/attribute_remover.rb
brakeman-6.2.2.rc1 bundle/ruby/3.3.0/gems/temple-0.10.3/lib/temple/html/attribute_remover.rb
brakeman-6.2.1 bundle/ruby/3.1.0/gems/temple-0.10.3/lib/temple/html/attribute_remover.rb
brakeman-6.2.0 bundle/ruby/3.1.0/gems/temple-0.10.3/lib/temple/html/attribute_remover.rb
temple-0.10.3 lib/temple/html/attribute_remover.rb
temple-0.10.2 lib/temple/html/attribute_remover.rb
temple-0.10.1 lib/temple/html/attribute_remover.rb