output_safety.rb 759 字节
Newer Older
1 2 3 4 5 6 7 8 9
class String
  def html_safe?
    defined?(@_rails_html_safe) && @_rails_html_safe
  end

  def html_safe!
    @_rails_html_safe = true
    self
  end
J
Joshua Peek 已提交
10

11 12 13
  def html_safe
    dup.html_safe!
  end
J
Joshua Peek 已提交
14

15 16 17 18 19 20 21 22 23
  alias original_plus +
  def +(other)
    result = original_plus(other)
    if html_safe? && also_html_safe?(other)
      result.html_safe!
    else
      result
    end
  end
J
Joshua Peek 已提交
24

25
  alias original_concat <<
26
  alias safe_concat <<
27 28 29 30 31 32 33
  def <<(other)
    result = original_concat(other)
    unless html_safe? && also_html_safe?(other)
      @_rails_html_safe = false
    end
    result
  end
J
Joshua Peek 已提交
34 35

  remove_method :concat
36 37 38
  def concat(other)
    self << other
  end
J
Joshua Peek 已提交
39

40 41 42 43
  private
    def also_html_safe?(other)
      other.respond_to?(:html_safe?) && other.html_safe?
    end
J
Joshua Peek 已提交
44

45
end