Skip to content

Commit

Permalink
Avoid checking defined?(@html_safe) (rails#45620)
Browse files Browse the repository at this point in the history
We should not need to check defined? here because we are only interested
in whether @html_safe is truthy or falsy.

We can  use an aliased attr_reader to make this even faster by skipping
both method dispatch.

Co-authored-by: Jean Boussier <[email protected]>
  • Loading branch information
jhawthorn and byroot authored Jul 19, 2022
1 parent 06e9fbd commit b5a758d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ def %(args)
self.class.new(super(escaped_args))
end

def html_safe?
defined?(@html_safe) && @html_safe
end
attr_reader :html_safe
alias_method :html_safe?, :html_safe
remove_method :html_safe

def to_s
self
Expand Down
7 changes: 7 additions & 0 deletions activesupport/test/safe_buffer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ def test_titleize
assert_not_predicate @buffer.dup, :html_safe?
end

test "Can call html_safe on a safe buffer" do
@buffer = "hello".html_safe
extra_safe = @buffer.html_safe
assert_equal "hello", extra_safe
assert_predicate extra_safe, :html_safe?
end

test "Should return safe buffer when added with another safe buffer" do
clean = "<script>".html_safe
result_buffer = @buffer + clean
Expand Down

0 comments on commit b5a758d

Please sign in to comment.