2009-07-21 34 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