module Pakyow::Support::SafeStringHelpers

Helper methods for ensuring string safety.

Public Instance Methods

ensure_html_safety(string) click to toggle source

Escapes the string unless it's marked as safe.

# File lib/pakyow/support/safe_string.rb, line 24
def ensure_html_safety(string)
  html_safe?(string) ? string : html_escape(string)
end
html_escape(string) click to toggle source

Escapes html characters in the string.

# File lib/pakyow/support/safe_string.rb, line 42
def html_escape(string)
  html_safe(CGI.escape_html(string.to_s))
end
html_safe(string) click to toggle source

Marks a string as safe.

# File lib/pakyow/support/safe_string.rb, line 36
def html_safe(string)
  html_safe?(string) ? string : SafeString.new(string)
end
html_safe?(string) click to toggle source

Returns true if the string is marked as safe.

# File lib/pakyow/support/safe_string.rb, line 30
def html_safe?(string)
  string.is_a?(SafeString)
end
sanitize(string, tags: []) click to toggle source

Strips html tags from the string, except for tags specified.

# File lib/pakyow/support/safe_string.rb, line 54
def sanitize(string, tags: [])
  return strip_tags(string) if tags.empty?
  html_safe(string.to_s.gsub(/((?!<((\/)?#{tags.join("|")}))<[^>]*>)/i, ""))
end
strip_tags(string) click to toggle source

Strips html tags from the string.

# File lib/pakyow/support/safe_string.rb, line 48
def strip_tags(string)
  html_safe(string.to_s.gsub(/<[^>]*>/ui, ""))
end