Module: Sanitizer
- Defined in:
- lib/sanitizer/version.rb,
lib/sanitizer/sanitizer.rb
Constant Summary collapse
- VERSION =
"0.1.8"
- @@htmle =
HTMLEntris
HTMLEntities.new
Class Method Summary collapse
- .clean_spaces(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
- .strip_comments(text) ⇒ Object
-
.strip_disallowed_tags(text) ⇒ Object
Remove all <script> and <style> tags.
-
.strip_tags(text, *tags) ⇒ Object
Remove all tags from from text.
Class Method Details
.clean_spaces(text) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/sanitizer/sanitizer.rb', line 15 def clean_spaces(text) output = text.dup output.gsub!(/\s+/, " ") output.strip! output 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
75 76 77 78 79 80 |
# File 'lib/sanitizer/sanitizer.rb', line 75 def entities_to_chars(text) output = text.dup output.gsub!(/\</uim, "<") output.gsub!(/\>/uim, ">") output end |
.html_decode(text) ⇒ Object
Convert invalid chars to HTML Entries
68 69 70 71 |
# File 'lib/sanitizer/sanitizer.rb', line 68 def html_decode(text) text = text.to_s @@htmle.decode(text, :named) end |
.html_encode(text) ⇒ Object
Convert invalid chars to HTML Entries
62 63 64 65 |
# File 'lib/sanitizer/sanitizer.rb', line 62 def html_encode(text) text = text.to_s @@htmle.encode(text, :named) end |
.sanitize(text) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/sanitizer/sanitizer.rb', line 8 def sanitize(text) text = (text) text = clean_spaces(text) text = html_encode(text) text end |
.strip_comments(text) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/sanitizer/sanitizer.rb', line 22 def strip_comments(text) output = text.dup output.gsub!(/(\<\!\-\-\b*[^\-\-\>]*.*?\-\-\>)/ui, "") output.gsub!(/(\<\s?\!--.*\s?--\>)/uim, "") output end |
.strip_disallowed_tags(text) ⇒ Object
Remove all <script> and <style> tags
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sanitizer/sanitizer.rb', line 30 def (text) output = text output.gsub!(/(<script\s*.*>.*<\/script>)/uim, "") output.gsub!(/(<script\s*.*\/?>)/uim, "") output.gsub!(/(<link\s*.*\/?>)/uim, "") output.gsub!(/(<style\s*.*>.*<\/style>)/uim, "") # Stripping html entities too output.gsub!(/(\<script\s*.*\>.*\<\/script\>)/uim, "") output.gsub!(/(\<script\s*.*\/?\>)/uim, "") output.gsub!(/(\<link\s*.*\/?\>)/uim, "") output.gsub!(/(\<style\s*.*\>.*\<\/style\>)/uim, "") output end |
.strip_tags(text, *tags) ⇒ Object
Remove all tags from from text
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sanitizer/sanitizer.rb', line 46 def (text, *) output = text.dup if .empty? # clear all tags by default output.gsub!(/<\/?[^>]*>/uim, "") output.gsub!(/\<\/?[^\>]*\>/uim, "") else # clean only selected tags strip = .map do |tag| %Q{(#{tag})} end.join('|') output.gsub!(/<\/?(#{strip})[^>]*>/uim, "") output.gsub!(/\<\/?(#{strip})[^\>]*\>/uim, "") end output end |