Module: Sanitizer
- Defined in:
- lib/sanitizer/version.rb,
lib/sanitizer/sanitizer.rb
Constant Summary collapse
- VERSION =
"0.2.1"- @@htmle =
HTMLEntris
HTMLEntities.new
Class Method Summary collapse
- .clean_spaces(text) ⇒ Object
-
.clean_spaces!(text) ⇒ Object
Clean retundant spaces.
- .entities_to_chars(text) ⇒ Object
-
.entities_to_chars!(text) ⇒ Object
Alguns feeds retornam tags "escapadas" dentro do conteúdo (ex: <br/>) Este método deve ser utilizado após o stripping e sanitização, para não deixar que essas tags sejam exibidas como conteúdo.
-
.html_decode(text) ⇒ Object
Convert invalid chars to HTML Entries.
-
.html_encode(text) ⇒ Object
Convert invalid chars to HTML Entries.
- .sanitize(text) ⇒ Object
-
.sanitize!(text) ⇒ Object
Sanitize to clean text.
- .strip_comments(text) ⇒ Object
-
.strip_comments!(text) ⇒ Object
remove comments.
- .strip_disallowed_tags(text) ⇒ Object
-
.strip_disallowed_tags!(text) ⇒ Object
Remove all
28 29 30
# File 'lib/sanitizer/sanitizer.rb', line 28 def clean_spaces(text) clean_spaces! text.dup end
.clean_spaces!(text) ⇒ Object
Clean retundant spaces
22 23 24 25 26
# File 'lib/sanitizer/sanitizer.rb', line 22 def clean_spaces!(text) text.gsub!(/\s+/, " ") text.strip! text end
.entities_to_chars(text) ⇒ Object
89 90 91
# File 'lib/sanitizer/sanitizer.rb', line 89 def entities_to_chars(text) entities_to_chars! text.dup end
.entities_to_chars!(text) ⇒ Object
Alguns feeds retornam tags "escapadas" dentro do conteúdo (ex: <br/>) Este método deve ser utilizado após o stripping e sanitização, para não deixar que essas tags sejam exibidas como conteúdo
83 84 85 86 87
# File 'lib/sanitizer/sanitizer.rb', line 83 def entities_to_chars!(text) text.gsub!(/\</uim, "<") text.gsub!(/\>/uim, ">") text end
.html_decode(text) ⇒ Object
Convert invalid chars to HTML Entries
100 101 102 103
# File 'lib/sanitizer/sanitizer.rb', line 100 def html_decode(text) text = text.to_s @@htmle.decode(text) end
.html_encode(text) ⇒ Object
Convert invalid chars to HTML Entries
94 95 96 97
# File 'lib/sanitizer/sanitizer.rb', line 94 def html_encode(text) text = text.to_s @@htmle.encode(text, :named) end
.sanitize(text) ⇒ Object
17 18 19
# File 'lib/sanitizer/sanitizer.rb', line 17 def sanitize(text) sanitize! text.dup end
.sanitize!(text) ⇒ Object
Sanitize to clean text
10 11 12 13 14 15
# File 'lib/sanitizer/sanitizer.rb', line 10 def sanitize!(text) (text) clean_spaces!(text) text.replace html_encode(text) text end
.strip_comments(text) ⇒ Object
39 40 41
# File 'lib/sanitizer/sanitizer.rb', line 39 def strip_comments(text) strip_comments! text.dup end
.strip_comments!(text) ⇒ Object
remove comments
33 34 35 36 37
# File 'lib/sanitizer/sanitizer.rb', line 33 def strip_comments!(text) text.gsub!(/(\<\!\-\-\b*[^\-\-\>]*.*?\-\-\>)/ui, "") text.gsub!(/(\<\s?\!--.*\s?--\>)/uim, "") text end
.strip_disallowed_tags(text) ⇒ Object
58 59 60
# File 'lib/sanitizer/sanitizer.rb', line 58 def (text) text.dup end
.strip_disallowed_tags!(text) ⇒ Object
Remove all
77 78 79
# File 'lib/sanitizer/sanitizer.rb', line 77 def (text, *) text.dup, * end
.strip_tags!(text, *tags) ⇒ Object
Remove all tags from from text
63 64 65 66 67 68 69 70 71 72 73 74 75
# File 'lib/sanitizer/sanitizer.rb', line 63 def (text, *) if .empty? # clear all tags by default text.gsub!(/<\/?[^>]*>/uim, "") text.gsub!(/\<\/?[^\>]*\>/uim, "") else # clean only selected tags strip = .map do |tag| %Q{(#{tag})} end.join('|') text.gsub!(/<\/?(#{strip})[^>]*>/uim, "") text.gsub!(/\<\/?(#{strip})[^\>]*\>/uim, "") end text end