class Sanitize
def clean_element!(node)
def clean_element!(node) # Run this node through all configured transformers. transform = transform_element!(node) # If this node is in the dynamic whitelist array (built at runtime by # transformers), let it live with all of its attributes intact. return if @whitelist_nodes.include?(node) name = node.name.to_s.downcase # Delete any element that isn't in the whitelist. unless transform[:whitelist] || @config[:elements].include?(name) unless @config[:remove_contents] node.children.each { |n| node.add_previous_sibling(n) } end node.unlink return end attr_whitelist = (transform[:attr_whitelist] + (@config[:attributes][name] || []) + (@config[:attributes][:all] || [])).uniq if attr_whitelist.empty? # Delete all attributes from elements with no whitelisted attributes. node.attribute_nodes.each {|attr| attr.remove } else # Delete any attribute that isn't in the whitelist for this element. node.attribute_nodes.each do |attr| attr.unlink unless attr_whitelist.include?(attr.name.downcase) end # Delete remaining attributes that use unacceptable protocols. if @config[:protocols].has_key?(name) protocol = @config[:protocols][name] node.attribute_nodes.each do |attr| attr_name = attr.name.downcase next false unless protocol.has_key?(attr_name) del = if attr.value.to_s.downcase =~ REGEX_PROTOCOL !protocol[attr_name].include?($1.downcase) else !protocol[attr_name].include?(:relative) end attr.unlink if del end end end # Add required attributes. if @config[:add_attributes].has_key?(name) @config[:add_attributes][name].each do |key, val| node[key] = val end end transform end