Class: Temple::HTML::AttributeRemover

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

Overview

This filter removes empty attributes

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_comment, #on_html_condcomment, #on_html_js, #on_html_tag

Methods included from Mixins::Options

included

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

Constructor Details

#initialize(opts = {}) ⇒ AttributeRemover

Returns a new instance of AttributeRemover.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/temple/html/attribute_remover.rb', line 9

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

Instance Method Details

#on_html_attr(name, value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/temple/html/attribute_remover.rb', line 19

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

#on_html_attrs(*attrs) ⇒ Object



15
16
17
# File 'lib/temple/html/attribute_remover.rb', line 15

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