Class: ActiveSupport::SafeBuffer
| Relationships & Source Files | |
| Namespace Children | |
|
Exceptions:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::String
|
|
|
Instance Chain:
self,
::String
|
|
| Inherits: | String |
| Defined in: | activesupport/lib/active_support/core_ext/string/output_safety.rb |
Constant Summary
-
UNSAFE_STRING_METHODS =
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 21%w( capitalize chomp chop delete delete_prefix delete_suffix downcase lstrip next reverse rstrip scrub squeeze strip succ swapcase tr tr_s unicode_normalize upcase ).freeze
-
UNSAFE_STRING_METHODS_WITH_BACKREF =
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 27%w(gsub sub).freeze
::String - Inherited
Class Method Summary
- .new(_str = "") ⇒ SafeBuffer constructor
Instance Attribute Summary
- #html_safe? ⇒ Boolean readonly
::String - Inherited
| #acts_like_string? | Enables more predictable duck-typing on String-like classes. |
| #blank? | A string is blank if it's empty or contains whitespaces only: |
| #is_utf8? | Returns |
| #present? | |
Instance Method Summary
- #%(args)
- #*(_)
- #+(other)
-
#<<(value)
Alias for #concat.
- #[] (also: #slice)
- #[]=(arg1, arg2, arg3 = nil)
- #as_json
- #bytesplice(*args, value)
- #chr
- #concat(value) (also: #<<)
- #encode_with(coder)
- #initialize_copy(other)
- #insert(index, value)
- #prepend(value)
- #replace(value)
- #safe_concat(value)
-
#slice
Alias for #[].
- #slice!
- #to_param
- #to_s
- #mark_unsafe! protected
- #explicit_html_escape_interpolated_argument(arg) private
- #implicit_html_escape_interpolated_argument(arg) private
- #original_concat private
- #set_block_back_references(block, match_data) private
- #string_into_safe_buffer(new_string, is_html_safe) private
::String - Inherited
| #at | If you pass a single integer, returns a substring of one character at that position. |
| #camelcase | Alias for String#camelize. |
| #camelize | By default, |
| #classify | Creates a class name from a plural table name like Rails does for table names to models. |
| #constantize |
|
| #dasherize | Replaces underscores with dashes in the string. |
| #deconstantize | Removes the rightmost segment from the constant expression in the string. |
| #demodulize | Removes the module part from the constant expression in the string. |
| #downcase_first | Converts the first character to lowercase. |
| #ends_with?, | |
| #exclude? | The inverse of |
| #first | Returns the first character. |
| #foreign_key | Creates a foreign key name from a class name. |
| #from | Returns a substring from the given position to the end of the string. |
| #html_safe | Marks a string as trusted safe. |
| #humanize | Capitalizes the first word, turns underscores into spaces, and (by default) strips a trailing '_id' if present. |
| #in_time_zone | Converts String to a |
| #indent | Indents the lines in the receiver: |
| #indent! | Same as |
| #inquiry | Wraps the current string in the |
| #last | Returns the last character of the string. |
| #parameterize | Replaces special characters in a string so that it may be used as part of a 'pretty' URL. |
| #pluralize | Returns the plural form of the word in the string. |
| #remove | Returns a new string with all occurrences of the patterns removed. |
| #remove! | Alters the string by removing all occurrences of the patterns. |
| #safe_constantize |
|
| #singularize | The reverse of |
| #squish | Returns the string, first removing all whitespace on both ends of the string, and then changing remaining consecutive whitespace groups into one space each. |
| #squish! | Performs a destructive squish. |
| #starts_with?, | |
| #strip_heredoc | Strips indentation in heredocs. |
| #tableize | Creates the name of a table like Rails does for models to table names. |
| #titlecase | Alias for String#titleize. |
| #titleize | Capitalizes all the words and replaces some characters in the string to create a nicer looking title. |
| #to | Returns a substring from the beginning of the string to the given position. |
| #to_date | Converts a string to a |
| #to_datetime | Converts a string to a |
| #to_time | Converts a string to a |
| #truncate | Truncates a given |
| #truncate_bytes | Truncates |
| #truncate_words | Truncates a given |
| #underscore | The reverse of |
| #upcase_first | Converts the first character to uppercase. |
| #as_json | |
Constructor Details
.new(_str = "") ⇒ SafeBuffer
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 71
def initialize(_str = "") super end
Instance Attribute Details
#html_safe? ⇒ Boolean (readonly)
[ GitHub ]
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 140
def html_safe? @html_unsafe.nil? end
Instance Method Details
#%(args)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 125
def %(args) case args when Hash escaped_args = args.transform_values { |arg| explicit_html_escape_interpolated_argument(arg) } else escaped_args = Array(args).map { |arg| explicit_html_escape_interpolated_argument(arg) } end new_safe_buffer = self.class.new(super(escaped_args)) if @html_unsafe new_safe_buffer.mark_unsafe! end new_safe_buffer end
#*(_)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 116
def *(_) new_string = super new_safe_buffer = new_string.is_a?(SafeBuffer) ? new_string : SafeBuffer.new(new_string) if @html_unsafe new_safe_buffer.mark_unsafe! end new_safe_buffer end
#+(other)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 112
def +(other) dup.concat(other) end
#<<(value)
Alias for #concat.
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 86
alias << concat
#[] Also known as: #slice
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 39
def [](*) if html_safe? new_string = super return unless new_string string_into_safe_buffer(new_string, true) else to_str[*] end end
#[]=(arg1, arg2, arg3 = nil)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 104
def []=(arg1, arg2, arg3 = nil) if arg3 super(arg1, arg2, implicit_html_escape_interpolated_argument(arg3)) else super(arg1, implicit_html_escape_interpolated_argument(arg2)) end end
#as_json
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 148
def as_json(*) to_str end
#bytesplice(*args, value)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 88
def bytesplice(*args, value) super(*args, implicit_html_escape_interpolated_argument(value)) end
#chr
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 60
def chr return super unless html_safe? string_into_safe_buffer(super, true) end
#concat(value) Also known as: #<<
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 80
def concat(value) unless value.nil? super(implicit_html_escape_interpolated_argument(value)) end self end
#encode_with(coder)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 156
def encode_with(coder) coder.represent_object nil, to_str end
#explicit_html_escape_interpolated_argument(arg) (private)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 208
def explicit_html_escape_interpolated_argument(arg) (!html_safe? || arg.html_safe?) ? arg : ERB::Util.unwrapped_html_escape(arg) end
#implicit_html_escape_interpolated_argument(arg) (private)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 212
def implicit_html_escape_interpolated_argument(arg) if !html_safe? || arg.html_safe? arg else ERB::Util.unwrapped_html_escape(arg.to_str) end end
#initialize_copy(other)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 75
def initialize_copy(other) super @html_unsafe = true unless other.html_safe? end
#insert(index, value)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 92
def insert(index, value) super(index, implicit_html_escape_interpolated_argument(value)) end
#mark_unsafe! (protected)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 203
def mark_unsafe! @html_unsafe = true end
#original_concat (private)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 29
alias_method :original_concat, :concat
#prepend(value)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 96
def prepend(value) super(implicit_html_escape_interpolated_argument(value)) end
#replace(value)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 100
def replace(value) super(implicit_html_escape_interpolated_argument(value)) end
#safe_concat(value)
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 66
def safe_concat(value) raise SafeConcatError unless html_safe? original_concat(value) end
#set_block_back_references(block, match_data) (private)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 220
def set_block_back_references(block, match_data) block.binding.eval("proc { |m| $~ = m }").call(match_data) rescue ArgumentError # Can't create binding from C level Proc end
#slice
Alias for #[].
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 50
alias_method :slice, :[]
#slice!
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 52
def slice!(...) new_string = super return new_string if !html_safe? || new_string.nil? string_into_safe_buffer(new_string, true) end
#string_into_safe_buffer(new_string, is_html_safe) (private)
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 226
def string_into_safe_buffer(new_string, is_html_safe) new_safe_buffer = new_string.is_a?(SafeBuffer) ? new_string : SafeBuffer.new(new_string) unless is_html_safe new_safe_buffer.instance_variable_set :@html_unsafe, true end new_safe_buffer end
#to_param
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 152
def to_param to_str end
#to_s
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 144
def to_s self end