2009-07-21 45 views
4

我正在寻找关于如何在Web应用程序中清理提交的html的建议,以便将来可以重新显示出样式或未封闭的标签以破坏应用程序的布局。如何最好地消除与轨道丰富的html?

在我的应用程序丰富的HTML是由用户YUI富文本编辑器,它默认运行几个正则表达式来清洁输入提交,我还调用[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 

还有什么是我选择这里?

回答

8

我个人使用消毒宝石。

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

我使用可用的辅助性的sanitize从ActionView

Module ActionView::Helpers::SanitizeHelper

+2

以下计算器问题着眼于Rails的`sanitize`方法的有效性:http://stackoverflow.com/questions/2985600 /多么好,是最Rails的sanitize方法,方法 – Purplejacket 2011-09-08 21:17:39