aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb
diff options
context:
space:
mode:
authorTimm <kaspth@gmail.com>2013-07-02 20:16:10 +0200
committerTimm <kaspth@gmail.com>2014-06-15 23:35:24 +0200
commit2622da17585a58fc75d3f9b5fc80eb03930fa156 (patch)
treed31d308697cfdbd6d5ec3d5641911833ae91f4a0 /actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb
parentd4d13925d3c0510ac6a08d1478e47d2135864ac6 (diff)
downloadrails-2622da17585a58fc75d3f9b5fc80eb03930fa156.tar.gz
rails-2622da17585a58fc75d3f9b5fc80eb03930fa156.tar.bz2
rails-2622da17585a58fc75d3f9b5fc80eb03930fa156.zip
Added PermitScrubber which allows you to permit elements for sanitization.
Diffstat (limited to 'actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb')
-rw-r--r--actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb27
1 files changed, 14 insertions, 13 deletions
diff --git a/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb b/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb
index 91de4c8ba1..cbddf3481c 100644
--- a/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb
+++ b/actionview/lib/action_view/helpers/sanitize_helper/sanitizers.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/class/attribute'
require 'active_support/deprecation'
+require 'action_view/helpers/sanitize_helper/permit_scrubber'
require 'loofah'
module ActionView
@@ -25,13 +26,23 @@ module ActionView
end
class WhiteListSanitizer
+
+ def initialize
+ @permit_scrubber = PermitScrubber.new
+ end
+
def sanitize(html, options = {})
return nil unless html
- validate_options(options)
loofah_fragment = Loofah.fragment(html)
- loofah_fragment.scrub!(:strip)
- loofah_fragment.xpath("./form").each { |form| form.remove }
+ if options[:tags] || options[:attributes]
+ @permit_scrubber.tags = options[:tags]
+ @permit_scrubber.attributes = options[:attributes]
+ loofah_fragment.scrub!(@permit_scrubber)
+ else
+ loofah_fragment.scrub!(:strip)
+ loofah_fragment.xpath("./form").each { |form| form.remove }
+ end
loofah_fragment.to_s
end
@@ -97,16 +108,6 @@ module ActionView
self.allowed_protocols = Loofah::HTML5::WhiteList::ALLOWED_PROTOCOLS
protected
- def validate_options(options)
- if options[:tags] && !options[:tags].is_a?(Enumerable)
- raise ArgumentError, "You should pass :tags as an Enumerable"
- end
-
- if options[:attributes] && !options[:attributes].is_a?(Enumerable)
- raise ArgumentError, "You should pass :attributes as an Enumerable"
- end
- end
-
def contains_bad_protocols?(attr_name, value)
protocol_separator = ':'
self.uri_attributes.include?(attr_name) &&