Class: ActiveSupport::SafeBuffer

Inherits:
String show all
Defined in:
lib/ro/html_safe.rb

Direct Known Subclasses

Ro::HTML

Defined Under Namespace

Classes: SafeConcatError

Constant Summary collapse

UNSAFE_STRING_METHODS =
%w(
  capitalize chomp chop delete downcase gsub lstrip next reverse rstrip
  slice squeeze strip sub succ swapcase tr tr_s upcase
)

Instance Method Summary collapse

Methods inherited from String

#html_safe

Constructor Details

#initialize(str = "") ⇒ SafeBuffer



55
56
57
58
# File 'lib/ro/html_safe.rb', line 55

def initialize(str = "")
  @html_safe = true
  super
end

Instance Method Details

#%(args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/ro/html_safe.rb', line 82

def %(args)
  case args
  when Hash
    escaped_args = Hash[args.map { |k, arg| [k, html_escape_interpolated_argument(arg)] }]
  else
    escaped_args = Array(args).map { |arg| html_escape_interpolated_argument(arg) }
  end

  self.class.new(super(escaped_args))
end

#+(other) ⇒ Object



78
79
80
# File 'lib/ro/html_safe.rb', line 78

def +(other)
  dup.concat(other)
end

#[](*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ro/html_safe.rb', line 34

def [](*args)
  if args.size < 2
    super
  elsif html_safe?
    new_safe_buffer = super

    if new_safe_buffer
      new_safe_buffer.instance_variable_set :@html_safe, true
    end

    new_safe_buffer
  else
    to_str[*args]
  end
end

#clone_emptyObject



65
66
67
# File 'lib/ro/html_safe.rb', line 65

def clone_empty
  self[0, 0]
end

#concat(value) ⇒ Object Also known as: <<



69
70
71
# File 'lib/ro/html_safe.rb', line 69

def concat(value)
  super(html_escape_interpolated_argument(value))
end

#encode_with(coder) ⇒ Object



105
106
107
# File 'lib/ro/html_safe.rb', line 105

def encode_with(coder)
  coder.represent_object nil, to_str
end

#html_safe?Boolean



93
94
95
# File 'lib/ro/html_safe.rb', line 93

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

#initialize_copy(other) ⇒ Object



60
61
62
63
# File 'lib/ro/html_safe.rb', line 60

def initialize_copy(other)
  super
  @html_safe = other.html_safe?
end

#prepend(value) ⇒ Object



74
75
76
# File 'lib/ro/html_safe.rb', line 74

def prepend(value)
  super(html_escape_interpolated_argument(value))
end

#safe_concat(value) ⇒ Object

Raises:



50
51
52
53
# File 'lib/ro/html_safe.rb', line 50

def safe_concat(value)
  raise SafeConcatError unless html_safe?
  original_concat(value)
end

#to_paramObject



101
102
103
# File 'lib/ro/html_safe.rb', line 101

def to_param
  to_str
end

#to_sObject



97
98
99
# File 'lib/ro/html_safe.rb', line 97

def to_s
  self
end