views:

302

answers:

3

I am attempting to use the Sanitize method from ActionView.

The line r_str = Helper.instance.sanitize(r_str, :tags => @@allowed_tags, :attributes => @@allowed_attribs) is giving me the error

undefined method `white_list_sanitizer' for Parsers::HTML::Helper:Class

This is my code in lib/parsers.rb

module Parsers
  module HTML
    @@allowed_tags = %w(--snip--)
    @@allowed_attribs = %w(--snip--)

    class Helper
        include Singleton
        include ActionView::Helpers::SanitizeHelper
    end

    #Use built-in santizer and the Hpricot plugin
    def self.clean(str)
      rgx = /<code>(.*?)<\/code>/ #All html within a code tag should be escaped.
      r_str = str.gsub(rgx) { |match| "<code>" + CGI.escapeHTML(match[5..-7]) + "</code>" } # TODO: test this.
      r_str = Helper.instance.sanitize(r_str, :tags => @@allowed_tags, :attributes => @@allowed_attribs)
      Hpricot(r_str)
    end

  end

  --snip-- 

end

What am I doing wrong?

(Please do not comment on the dangers of allowing user submitted HTML, I know the risks)

A: 

You need also class methods from sanitize helper

 class Helper
   include Singleton
   include ActionView::Helpers::SanitizeHelper

   class << self
     include SanitizeHelper::ClassMethods
   end
 end
VitalieL
A: 

simply do instead of "include ActionView::Helpers::SanitizeHelper",

 include ActionView::Helpers

this above will mix in the ClassMethods from SanitizeHelper, and your code will work.

Note: I have also seen suggestions to explicitly do:

extend ActionView::Helpers::SanitizeHelper::ClassMethods
Valters Vingolds
A: 

The proper class in rails is HTML::Sanitizer

epochwolf