Skip to content

Commit

Permalink
Added backwards compatibility for html_safe!
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiago Pastorino and José Ignacio Costa committed Feb 5, 2010
1 parent 9ca6df8 commit 55c1a86
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions activesupport/lib/active_support/core_ext/string/output_safety.rb
Expand Up @@ -111,11 +111,45 @@ def to_s
end

class String
def html_safe!
raise "Use html_safe with your strings instead of html_safe! See http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/ for the full story."
end
alias_method :add_without_safety, :+

def html_safe
ActiveSupport::SafeBuffer.new(self)
end

def html_safe?
defined?(@_rails_html_safe) && @_rails_html_safe
end

def html_safe!
ActiveSupport::Deprecation.warn("Use html_safe with your strings instead of html_safe! See http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/ for the full story.", caller)
@_rails_html_safe = true
self
end

def add_with_safety(other)
result = add_without_safety(other)
if html_safe? && also_html_safe?(other)
result.html_safe!
else
result
end
end
alias_method :+, :add_with_safety

def concat_with_safety(other_or_fixnum)
result = concat_without_safety(other_or_fixnum)
unless html_safe? && also_html_safe?(other_or_fixnum)
@_rails_html_safe = false
end
result
end
alias_method_chain :concat, :safety
undef_method :<<
alias_method :<<, :concat_with_safety

private
def also_html_safe?(other)
other.respond_to?(:html_safe?) && other.html_safe?
end
end

0 comments on commit 55c1a86

Please sign in to comment.