如何最好地使用rails来清理丰富的html?

Chr*_*ams 4 ruby-on-rails sanitize richtext

我正在寻找有关如何在Web应用程序中清理提交的html的建议,以便将来可以重新显示样式或未封闭的标签破坏应用程序的布局.

在我的应用程序上,用户使用YUI Rich文本编辑器提交了丰富的HTML,默认情况下会运行一些正则表达式来清理输入,而且我还调用[ filter_MSWord][1]捕获从办公室发送的任何废话

在后端,我正在运行ruby-tidy以在显示为评论之前清理html,但有时粘贴不好的html仍会影响我正在使用的应用程序的布局 - 我该如何防范这个?

FWIW这里是我正在使用的消毒剂设置 -

module HTMLSanitizer


  def tidy_html(input)

    cleaned_html = Tidy.open(:show_warnings=>false) do |tidy|
      # don’t output body and html tags
      tidy.options.show_body_only = true 
      # output xhtml
      tidy.options.output_html = true
      # don’t write newlines all over the place
      tidy.options.wrap = 0
      # use utf8 to play nice with rails
      tidy.options.char_encoding = 'utf8'
      xml = tidy.clean(input)
      xml
    end
  end

end
Run Code Online (Sandbox Code Playgroud)

我还有什么选择呢?

Sin*_*our 8

我个人使用消毒宝石.

require 'sanitize'
op = Sanitize.clean("<html><body>wow!</body></hhhh>") # Notice the incorrect HTML. It still outputs "wow!"
Run Code Online (Sandbox Code Playgroud)