mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure [] respects the status of the buffer.
This commit is contained in:
parent
71d8c77e5a
commit
8ccaa34103
2 changed files with 41 additions and 16 deletions
|
@ -103,19 +103,31 @@ module ActiveSupport #:nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
def [](*args)
|
||||
return super if args.size < 2
|
||||
|
||||
if html_safe?
|
||||
new_safe_buffer = super
|
||||
new_safe_buffer.instance_eval { @html_safe = true }
|
||||
new_safe_buffer
|
||||
else
|
||||
to_str[*args]
|
||||
end
|
||||
end
|
||||
|
||||
def safe_concat(value)
|
||||
raise SafeConcatError if dirty?
|
||||
raise SafeConcatError unless html_safe?
|
||||
original_concat(value)
|
||||
end
|
||||
|
||||
def initialize(*)
|
||||
@dirty = false
|
||||
@html_safe = true
|
||||
super
|
||||
end
|
||||
|
||||
def initialize_copy(other)
|
||||
super
|
||||
@dirty = other.dirty?
|
||||
@html_safe = other.html_safe?
|
||||
end
|
||||
|
||||
def clone_empty
|
||||
|
@ -125,7 +137,7 @@ module ActiveSupport #:nodoc:
|
|||
end
|
||||
|
||||
def concat(value)
|
||||
if dirty? || value.html_safe?
|
||||
if !html_safe? || value.html_safe?
|
||||
super(value)
|
||||
else
|
||||
super(ERB::Util.h(value))
|
||||
|
@ -138,7 +150,7 @@ module ActiveSupport #:nodoc:
|
|||
end
|
||||
|
||||
def html_safe?
|
||||
!dirty?
|
||||
defined?(@html_safe) && @html_safe
|
||||
end
|
||||
|
||||
def to_s
|
||||
|
@ -161,18 +173,12 @@ module ActiveSupport #:nodoc:
|
|||
end # end
|
||||
|
||||
def #{unsafe_method}!(*args) # def capitalize!(*args)
|
||||
@dirty = true # @dirty = true
|
||||
@html_safe = false # @html_safe = false
|
||||
super # super
|
||||
end # end
|
||||
EOT
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def dirty?
|
||||
@dirty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -84,13 +84,13 @@ class SafeBufferTest < ActiveSupport::TestCase
|
|||
assert_equal "hello<>", clean + @buffer
|
||||
end
|
||||
|
||||
test "Should concat as a normal string when dirty" do
|
||||
test "Should concat as a normal string when safe" do
|
||||
clean = "hello".html_safe
|
||||
@buffer.gsub!('', '<>')
|
||||
assert_equal "<>hello", @buffer + clean
|
||||
end
|
||||
|
||||
test "Should preserve dirty? status on copy" do
|
||||
test "Should preserve html_safe? status on copy" do
|
||||
@buffer.gsub!('', '<>')
|
||||
assert !@buffer.dup.html_safe?
|
||||
end
|
||||
|
@ -102,14 +102,14 @@ class SafeBufferTest < ActiveSupport::TestCase
|
|||
assert_equal "<script>", result_buffer
|
||||
end
|
||||
|
||||
test "Should raise an error when safe_concat is called on dirty buffers" do
|
||||
test "Should raise an error when safe_concat is called on unsafe buffers" do
|
||||
@buffer.gsub!('', '<>')
|
||||
assert_raise ActiveSupport::SafeBuffer::SafeConcatError do
|
||||
@buffer.safe_concat "BUSTED"
|
||||
end
|
||||
end
|
||||
|
||||
test "should not fail if the returned object is not a string" do
|
||||
test "Should not fail if the returned object is not a string" do
|
||||
assert_kind_of NilClass, @buffer.slice("chipchop")
|
||||
end
|
||||
|
||||
|
@ -121,4 +121,23 @@ class SafeBufferTest < ActiveSupport::TestCase
|
|||
assert @buffer.clone_empty.html_safe?
|
||||
assert !@buffer.gsub!('', '').clone_empty.html_safe?
|
||||
end
|
||||
|
||||
test "Should be safe when sliced if original value was safe" do
|
||||
new_buffer = @buffer[0,0]
|
||||
assert_not_nil new_buffer
|
||||
assert new_buffer.html_safe?, "should be safe"
|
||||
end
|
||||
|
||||
test "Should continue unsafe on slice" do
|
||||
x = 'foo'.html_safe.gsub!('f', '<script>alert("lolpwnd");</script>')
|
||||
|
||||
# calling gsub! makes the dirty flag true
|
||||
assert !x.html_safe?, "should not be safe"
|
||||
|
||||
# getting a slice of it
|
||||
y = x[0..-1]
|
||||
|
||||
# should still be unsafe
|
||||
assert !y.html_safe?, "should not be safe"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue