Sha256: 25f29020045e5af4c9f2c976d2691a923a78518514cbc130b7082d84c89ad843

Contents?: true

Size: 1.37 KB

Versions: 37

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/string/output_safety"

module ActionView
  # Used as a buffer for views
  #
  # The main difference between this and ActiveSupport::SafeBuffer
  # is for the methods `<<` and `safe_expr_append=` the inputs are
  # checked for nil before they are assigned and `to_s` is called on
  # the input. For example:
  #
  #   obuf = ActionView::OutputBuffer.new "hello"
  #   obuf << 5
  #   puts obuf # => "hello5"
  #
  #   sbuf = ActiveSupport::SafeBuffer.new "hello"
  #   sbuf << 5
  #   puts sbuf # => "hello\u0005"
  #
  class OutputBuffer < ActiveSupport::SafeBuffer # :nodoc:
    def initialize(*)
      super
      encode!
    end

    def <<(value)
      return self if value.nil?
      super(value.to_s)
    end
    alias :append= :<<

    def safe_expr_append=(val)
      return self if val.nil?
      safe_concat val.to_s
    end

    alias :safe_append= :safe_concat
  end

  class StreamingBuffer # :nodoc:
    def initialize(block)
      @block = block
    end

    def <<(value)
      value = value.to_s
      value = ERB::Util.h(value) unless value.html_safe?
      @block.call(value)
    end
    alias :concat  :<<
    alias :append= :<<

    def safe_concat(value)
      @block.call(value.to_s)
    end
    alias :safe_append= :safe_concat

    def html_safe?
      true
    end

    def html_safe
      self
    end
  end
end

Version data entries

37 entries across 35 versions & 4 rubygems

Version Path
actionview-7.0.8.6 lib/action_view/buffers.rb
actionview-7.0.8.5 lib/action_view/buffers.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionview-7.0.8.4/lib/action_view/buffers.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/buffers.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/buffers.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/buffers.rb
actionview-7.0.8.4 lib/action_view/buffers.rb
actionview-7.0.8.1 lib/action_view/buffers.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionview-7.0.2.3/lib/action_view/buffers.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionview-7.0.3.1/lib/action_view/buffers.rb
actionview-7.0.8 lib/action_view/buffers.rb
actionview-7.0.7.2 lib/action_view/buffers.rb
actionview-7.0.7.1 lib/action_view/buffers.rb
actionview-7.0.7 lib/action_view/buffers.rb
actionview-7.0.6 lib/action_view/buffers.rb
actionview-7.0.5.1 lib/action_view/buffers.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionview-7.0.3.1/lib/action_view/buffers.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionview-7.0.2.3/lib/action_view/buffers.rb
actionview-7.0.5 lib/action_view/buffers.rb
actionview-7.0.4.3 lib/action_view/buffers.rb