From a4d2493b26ff166aa3605684f22e0abb174c0752 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 21 Nov 2020 09:54:43 +0900 Subject: [PATCH] Let AS::SafeBuffer#[] and * return value be an instance of SafeBuffer in Ruby 3 Ruby 3 introduces an incompatibility that String methods return String instances when called on a subclass instance. https://bugs.ruby-lang.org/issues/10845 https://github.com/ruby/ruby/pull/3701 --- .../active_support/core_ext/string/output_safety.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 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 635f9cf45780..60e39d58ca5c 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -153,12 +153,12 @@ def initialize def [](*args) if html_safe? - new_safe_buffer = super + new_string = super - if new_safe_buffer - new_safe_buffer.instance_variable_set :@html_safe, true - end + return unless new_string + new_safe_buffer = new_string.is_a?(SafeBuffer) ? new_string : SafeBuffer.new(new_string) + new_safe_buffer.instance_variable_set :@html_safe, true new_safe_buffer else to_str[*args] @@ -214,7 +214,8 @@ def +(other) end def *(*) - new_safe_buffer = super + new_string = super + new_safe_buffer = new_string.is_a?(SafeBuffer) ? new_string : SafeBuffer.new(new_string) new_safe_buffer.instance_variable_set(:@html_safe, @html_safe) new_safe_buffer end