Sha256: 4c5304ef0b16201c5c5ec679cf5eae881244f2fa0347b701fe05936e47b10833

Contents?: true

Size: 1.88 KB

Versions: 29

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module Haml
  # Like Temple::Filters::Escapable, but with support for escaping by
  # Haml::Herlpers.html_escape and Haml::Herlpers.escape_once.
  class Escapable < Temple::Filter
    # Special value of `flag` to ignore html_safe?
    EscapeSafeBuffer = Struct.new(:value)

    def initialize(*)
      super
      @escape = false
      @escape_safe_buffer = false
    end

    def on_escape(flag, exp)
      old_escape, old_escape_safe_buffer = @escape, @escape_safe_buffer
      @escape_safe_buffer = flag.is_a?(EscapeSafeBuffer)
      @escape = @escape_safe_buffer ? flag.value : flag
      compile(exp)
    ensure
      @escape, @escape_safe_buffer = old_escape, old_escape_safe_buffer
    end

    # The same as Haml::AttributeBuilder.build_attributes
    def on_static(value)
      [:static,
       if @escape == :once
         escape_once(value)
       elsif @escape
         escape(value)
       else
         value
       end
      ]
    end

    # The same as Haml::AttributeBuilder.build_attributes
    def on_dynamic(value)
      [:dynamic,
       if @escape == :once
         escape_once_code(value)
       elsif @escape
         escape_code(value)
       else
         "(#{value}).to_s"
       end
      ]
    end

    private

    def escape_once(value)
      if @escape_safe_buffer
        ::Haml::Helpers.escape_once_without_haml_xss(value)
      else
        ::Haml::Helpers.escape_once(value)
      end
    end

    def escape(value)
      if @escape_safe_buffer
        ::Haml::Helpers.html_escape_without_haml_xss(value)
      else
        ::Haml::Helpers.html_escape(value)
      end
    end

    def escape_once_code(value)
      "::Haml::Helpers.escape_once#{('_without_haml_xss' if @escape_safe_buffer)}((#{value}))"
    end

    def escape_code(value)
      "::Haml::Helpers.html_escape#{('_without_haml_xss' if @escape_safe_buffer)}((#{value}))"
    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
brakeman-6.2.2 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-6.2.2.rc1 bundle/ruby/3.3.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-6.2.1 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-6.2.0 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-6.1.2 bundle/ruby/3.3.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-6.1.1 bundle/ruby/3.0.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-6.1.0 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-6.0.1 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-6.0.0 bundle/ruby/3.0.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.4.1 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.4.0 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.3.1 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.3.0 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.2.3 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.2.2 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.2.1 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.2.0 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/escapable.rb
brakeman-5.1.2 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/escapable.rb
haml-5.2.2 lib/haml/escapable.rb
brakeman-5.1.1 bundle/ruby/2.7.0/gems/haml-5.2.1/lib/haml/escapable.rb