From 3f5bd11ed651cef4f098c1d9dba16fe099e89a42 Mon Sep 17 00:00:00 2001 From: Yumin Wong Date: Fri, 31 Aug 2018 11:46:09 -0500 Subject: SafeBuffer should maintain safety upon getting a slice via a range if original buffer was safe. Co-Authored-By: no-itsbackpack --- .../lib/active_support/core_ext/string/output_safety.rb | 4 +--- activesupport/test/safe_buffer_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb index f3bdc2977e..d837bb10aa 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -149,9 +149,7 @@ module ActiveSupport #:nodoc: end def [](*args) - if args.size < 2 - super - elsif html_safe? + if html_safe? new_safe_buffer = super if new_safe_buffer diff --git a/activesupport/test/safe_buffer_test.rb b/activesupport/test/safe_buffer_test.rb index 9456bb8753..bf661a5f92 100644 --- a/activesupport/test/safe_buffer_test.rb +++ b/activesupport/test/safe_buffer_test.rb @@ -150,6 +150,18 @@ class SafeBufferTest < ActiveSupport::TestCase assert_not y.html_safe?, "should not be safe" end + test "Should continue safe on slice" do + x = "
foo
".html_safe + + assert x.html_safe?, "should be safe" + + # getting a slice of it + y = x[0..-1] + + # should still be safe + assert y.html_safe?, "should be safe" + end + test "Should work with interpolation (array argument)" do x = "foo %s bar".html_safe % ["qux"] assert_equal "foo qux bar", x -- cgit v1.2.3 From 0a1567793b556c50ae775128a75a96c2b929fb4d Mon Sep 17 00:00:00 2001 From: Yumin Wong Date: Thu, 6 Sep 2018 12:48:50 -0500 Subject: Use assert_predicate instead Co-authored-by: no-itsbackpack --- activesupport/test/safe_buffer_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/test/safe_buffer_test.rb b/activesupport/test/safe_buffer_test.rb index bf661a5f92..70dec6b3d2 100644 --- a/activesupport/test/safe_buffer_test.rb +++ b/activesupport/test/safe_buffer_test.rb @@ -153,13 +153,13 @@ class SafeBufferTest < ActiveSupport::TestCase test "Should continue safe on slice" do x = "
foo
".html_safe - assert x.html_safe?, "should be safe" + assert_predicate x, :html_safe? # getting a slice of it y = x[0..-1] # should still be safe - assert y.html_safe?, "should be safe" + assert_predicate y, :html_safe? end test "Should work with interpolation (array argument)" do -- cgit v1.2.3 From 1d42a661d8ff7795ebf9255cb7efd17de7b47fb0 Mon Sep 17 00:00:00 2001 From: Yumin Wong Date: Thu, 6 Sep 2018 15:17:18 -0500 Subject: Update CHANGELOG for SafetyBuffer slice access Co-authored-by: no-itsbackpack --- activesupport/CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 4ae02edd6a..6266eccc0d 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,10 @@ +* Maintain `html_safe?` on html_safe strings when sliced + + string = "
test
".html_safe + string[-1..1].html_safe? # => true + + *Elom Gomez, Yumin Wong* + * Add `Array#extract!`. The method removes and returns the elements for which the block returns a true value. -- cgit v1.2.3