Class: ActiveSupport::SafeBuffer

Inherits:
String show all
Defined in:
lib/active_support/core_ext/string/output_safety.rb

Defined Under Namespace

Classes: SafeConcatError

Constant Summary collapse

UNSAFE_STRING_METHODS =
["capitalize", "chomp", "chop", "delete", "downcase", "gsub", "lstrip", "next", "reverse", "rstrip", "slice", "squeeze", "strip", "sub", "succ", "swapcase", "tr", "tr_s", "upcase", "prepend"].freeze

Constants inherited from String

String::NON_WHITESPACE_REGEXP

Instance Method Summary collapse

Methods inherited from String

#acts_like_string?, #as_json, #at, #blank?, #camelize, #classify, #constantize, #dasherize, #deconstantize, #demodulize, #encode_json, #encoding_aware?, #exclude?, #first, #foreign_key, #from, #html_safe, #humanize, #inquiry, #is_utf8?, #last, #mb_chars, #ord, #parameterize, #pluralize, #safe_constantize, #singularize, #squish, #squish!, #strip_heredoc, #tableize, #titleize, #to, #to_date, #to_datetime, #to_time, #truncate, #underscore

Constructor Details

#initializeSafeBuffer

Returns a new instance of SafeBuffer.



112
113
114
115
# File 'lib/active_support/core_ext/string/output_safety.rb', line 112

def initialize(*)
  @dirty = false
  super
end

Instance Method Details

#+(other) ⇒ Object



131
132
133
# File 'lib/active_support/core_ext/string/output_safety.rb', line 131

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

#[](*args) ⇒ Object



101
102
103
104
105
# File 'lib/active_support/core_ext/string/output_safety.rb', line 101

def[](*args)
  new_safe_buffer = super
  new_safe_buffer.instance_eval { @dirty = false }
  new_safe_buffer
end

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



122
123
124
125
126
127
128
# File 'lib/active_support/core_ext/string/output_safety.rb', line 122

def concat(value)
  if dirty? || value.html_safe?
    super(value)
  else
    super(ERB::Util.h(value))
  end
end

#encode_with(coder) ⇒ Object



147
148
149
# File 'lib/active_support/core_ext/string/output_safety.rb', line 147

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

#html_safe?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/active_support/core_ext/string/output_safety.rb', line 135

def html_safe?
  !dirty?
end

#initialize_copy(other) ⇒ Object



117
118
119
120
# File 'lib/active_support/core_ext/string/output_safety.rb', line 117

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

#safe_concat(value) ⇒ Object

Raises:



107
108
109
110
# File 'lib/active_support/core_ext/string/output_safety.rb', line 107

def safe_concat(value)
  raise SafeConcatError if dirty?
  original_concat(value)
end

#to_paramObject



143
144
145
# File 'lib/active_support/core_ext/string/output_safety.rb', line 143

def to_param
  to_str
end

#to_sObject



139
140
141
# File 'lib/active_support/core_ext/string/output_safety.rb', line 139

def to_s
  self
end

#to_yaml(*args) ⇒ Object



151
152
153
154
# File 'lib/active_support/core_ext/string/output_safety.rb', line 151

def to_yaml(*args)
  return super() if defined?(YAML::ENGINE) && !YAML::ENGINE.syck?
  to_str.to_yaml(*args)
end