Module: ActiveSupport::CoreExtensions::String::OutputSafety

Included in:
String
Defined in:
lib/active_support/core_ext/string/output_safety.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/active_support/core_ext/string/output_safety.rb', line 5

def self.included(base)
  base.class_eval do
    alias_method :add_without_safety, :+
    alias_method :+, :add_with_safety
    alias_method_chain :concat, :safety
    undef_method :<<
    alias_method :<<, :concat_with_safety
  end
end

Instance Method Details

#add_with_safety(other) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/active_support/core_ext/string/output_safety.rb', line 24

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

#concat_with_safety(other_or_fixnum) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/active_support/core_ext/string/output_safety.rb', line 33

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

#html_safe!Object



19
20
21
22
# File 'lib/active_support/core_ext/string/output_safety.rb', line 19

def html_safe!
  @_rails_html_safe = true
  self
end

#html_safe?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/active_support/core_ext/string/output_safety.rb', line 15

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