diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-04-19 07:04:13 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-19 07:04:13 +0900 |
commit | 4acddae05e65ec62114219d0595d78bd13ed889d (patch) | |
tree | 3c282de1ea3b7c058c43faf44f36bb4ac7a58b57 /activesupport | |
parent | 8ac4d1500505f15ba3630ba1e88d395ad43442a5 (diff) | |
parent | 9dd254c2a20b393d68c9db4b17a15a672e2a2c16 (diff) | |
download | rails-4acddae05e65ec62114219d0595d78bd13ed889d.tar.gz rails-4acddae05e65ec62114219d0595d78bd13ed889d.tar.bz2 rails-4acddae05e65ec62114219d0595d78bd13ed889d.zip |
Merge pull request #36012 from r7kamura/feature/safe-buffer-multiplication
Preserve html_safe? status on ActiveSupport::SafeBuffer#*
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 12 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/output_safety.rb | 6 | ||||
-rw-r--r-- | activesupport/test/safe_buffer_test.rb | 8 |
3 files changed, 26 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 597e7b8929..301b0c8822 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,15 @@ +* Preserve `html_safe?` status on `ActiveSupport::SafeBuffer#*`. + + Before: + + ("<br />".html_safe * 2).html_safe? #=> nil + + After: + + ("<br />".html_safe * 2).html_safe? #=> true + + *Ryo Nakamura* + * Calling test methods with `with_info_handler` method to allow minitest-hooks plugin to work. 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 638152626b..645b1fea17 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -213,6 +213,12 @@ module ActiveSupport #:nodoc: dup.concat(other) end + def *(*) + new_safe_buffer = super + new_safe_buffer.instance_variable_set(:@html_safe, @html_safe) + new_safe_buffer + end + def %(args) case args when Hash diff --git a/activesupport/test/safe_buffer_test.rb b/activesupport/test/safe_buffer_test.rb index b1a1c2d390..f475e05c9a 100644 --- a/activesupport/test/safe_buffer_test.rb +++ b/activesupport/test/safe_buffer_test.rb @@ -150,6 +150,14 @@ class SafeBufferTest < ActiveSupport::TestCase assert_equal "hello<>", clean + @buffer end + test "Should preserve html_safe? status on multiplication" do + multiplied_safe_buffer = "<br />".html_safe * 2 + assert_predicate multiplied_safe_buffer, :html_safe? + + multiplied_unsafe_buffer = @buffer.gsub("", "<>") * 2 + assert_not_predicate multiplied_unsafe_buffer, :html_safe? + end + test "Should concat as a normal string when safe" do clean = "hello".html_safe @buffer.gsub!("", "<>") |