class Sanitize

def clean!(html)

made.
Performs clean in place, returning _html_, or +nil+ if no changes were
def clean!(html)
  fragment = Nokogiri::HTML::DocumentFragment.parse(html)
  fragment.traverse do |node|
    if node.comment?
      node.unlink unless @config[:allow_comments]
    elsif node.element?
      name = node.name.to_s.downcase
      # Delete any element that isn't in the whitelist.
      unless @config[:elements].include?(name)
        node.children.each { |n| node.add_previous_sibling(n) }
        node.unlink
        next
      end
      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
    elsif node.cdata?
      node.replace(Nokogiri::XML::Text.new(node.text, node.document))
    end
  end
  if @config[:output] == :xhtml
    output_method = fragment.method(:to_xhtml)
  elsif @config[:output] == :html
    output_method = fragment.method(:to_html)
  else
    raise Error, "unsupported output format: #{@config[:output]}"
  end
  if RUBY_VERSION >= '1.9'
    # Nokogiri 1.3.3 (and possibly earlier versions) always returns a US-ASCII
    # string no matter what we ask for. This will be fixed in 1.4.0, but for
    # now we have to hack around it to prevent errors.
    result = output_method.call(:encoding => 'utf-8', :indent => 0).force_encoding('utf-8')
    result.gsub!(">\n", '>')
  else
    result = output_method.call(:encoding => 'utf-8', :indent => 0).gsub(">\n", '>')
  end
  return result == html ? nil : html[0, html.length] = result
end