Module: Pakyow::Support::SafeStringHelpers
- Extended by:
- SafeStringHelpers
- Included in:
- SafeStringHelpers, StringBuilder
- Defined in:
- lib/pakyow/support/safe_string.rb
Overview
Helper methods for ensuring string safety.
Instance Method Summary collapse
-
#ensure_html_safety(string) ⇒ Object
Escapes the string unless it’s marked as safe.
-
#html_escape(string) ⇒ Object
Escapes html characters in the string.
-
#html_safe(string) ⇒ Object
Marks a string as safe.
-
#html_safe?(string) ⇒ Boolean
Returns true if the string is marked as safe.
-
#sanitize(string, tags: []) ⇒ Object
Strips html tags from the string, except for tags specified.
-
#strip_tags(string) ⇒ Object
Strips html tags from the string.
Instance Method Details
#ensure_html_safety(string) ⇒ Object
Escapes the string unless it’s marked as safe.
24 25 26 |
# 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) ⇒ Object
Escapes html characters in the string.
42 43 44 |
# 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) ⇒ Object
Marks a string as safe.
36 37 38 |
# File 'lib/pakyow/support/safe_string.rb', line 36 def html_safe(string) html_safe?(string) ? string : SafeString.new(string) end |
#html_safe?(string) ⇒ Boolean
Returns true if the string is marked as safe.
30 31 32 |
# File 'lib/pakyow/support/safe_string.rb', line 30 def html_safe?(string) string.is_a?(SafeString) end |
#sanitize(string, tags: []) ⇒ Object
Strips html tags from the string, except for tags specified.
54 55 56 57 |
# File 'lib/pakyow/support/safe_string.rb', line 54 def sanitize(string, tags: []) return (string) if .empty? html_safe(string.to_s.gsub(/((?!<((\/)?#{.join("|")}))<[^>]*>)/i, "")) end |
#strip_tags(string) ⇒ Object
Strips html tags from the string.
48 49 50 |
# File 'lib/pakyow/support/safe_string.rb', line 48 def (string) html_safe(string.to_s.gsub(/<[^>]*>/ui, "")) end |