2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:48:35 -04:00
|
|
|
require "active_support/core_ext/string/output_safety"
|
2011-04-16 04:28:47 -04:00
|
|
|
|
|
|
|
module ActionView
|
2018-09-06 13:00:20 -04:00
|
|
|
# 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"
|
|
|
|
#
|
2011-04-16 05:42:02 -04:00
|
|
|
class OutputBuffer < ActiveSupport::SafeBuffer #:nodoc:
|
2011-04-16 04:28:47 -04:00
|
|
|
def initialize(*)
|
|
|
|
super
|
2011-12-24 07:57:54 -05:00
|
|
|
encode!
|
2011-04-16 04:28:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def <<(value)
|
2013-03-04 23:36:25 -05:00
|
|
|
return self if value.nil?
|
2011-04-16 04:28:47 -04:00
|
|
|
super(value.to_s)
|
|
|
|
end
|
|
|
|
alias :append= :<<
|
2013-03-04 23:36:25 -05:00
|
|
|
|
2014-09-14 20:11:04 -04:00
|
|
|
def safe_expr_append=(val)
|
|
|
|
return self if val.nil?
|
|
|
|
safe_concat val.to_s
|
|
|
|
end
|
|
|
|
|
2011-04-16 04:28:47 -04:00
|
|
|
alias :safe_append= :safe_concat
|
|
|
|
end
|
|
|
|
|
2011-04-16 05:42:02 -04:00
|
|
|
class StreamingBuffer #:nodoc:
|
2011-04-16 04:28:47 -04:00
|
|
|
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
|
2011-06-05 11:34:40 -04:00
|
|
|
|
2011-04-16 04:28:47 -04:00
|
|
|
def html_safe
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
2011-06-05 11:34:40 -04:00
|
|
|
end
|