Sha256: 7978fafe2ece5e59f7bf8b4b1db223b1eebf5aaae6aca3a1dcc2b1ac803470fc

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

module Temple
  module Filters
    # Escape dynamic or static expressions.
    # This filter must be used after Temple::HTML::* and before the generators.
    # It can be enclosed with Temple::Filters::DynamicInliner filters to
    # reduce calls to Temple::Utils#escape_html.
    #
    # @api public
    class Escapable < Filter
      # Activate the usage of html_safe? if it is available (for Rails 3 for example)
      set_default_options :use_html_safe => ''.respond_to?(:html_safe?),
                          :disable_escape => false

      def initialize(opts = {})
        super
        @escape_code = options[:escape_code] ||
          "Temple::Utils.escape_html#{options[:use_html_safe] ? '_safe' : ''}((%s))"
        @escaper = eval("proc {|v| #{@escape_code % 'v'} }")
        @escape = false
      end

      def on_escape(flag, exp)
        old = @escape
        @escape = flag && !options[:disable_escape]
        compile(exp)
      ensure
        @escape = old
      end

      def on_static(value)
        [:static, @escape ? @escaper[value] : value]
      end

      def on_dynamic(value)
        [:dynamic, @escape ? @escape_code % value : value]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
temple-0.3.2 lib/temple/filters/escapable.rb
temple-0.3.1 lib/temple/filters/escapable.rb
temple-0.3.0 lib/temple/filters/escapable.rb