2011-09-24 3 views
2

어떻게 위생 처리를 사용하겠습니까?하지만 기본 태그에 의해 활성화 된 태그를 허용하지 않으려면 어떻게해야합니까? documentation 나는이를 넣을 수 주 내 application.rbRails가 기본 허용 태그를 삭제합니다.

config.after_initialize do 
    ActionView::Base.sanitized_allowed_tags.delete 'div' 
end 

내가 대신 소독하는 인수로이 통과 할 수 있습니까?

답변

15

예 통화 단위로 허용 할 태그와 속성을 지정할 수 있습니다. fine manual에서 :

사용자 정의 사용 (만을 언급 한 태그와 속성을 사용할 수 있습니다, 다른 1 개)

<%= sanitize @article.body, :tags => %w(table tr td), :attributes => %w(id class style) %> 

그러나 문제는 :tags모든 태그를 포함한다는 것이다 너는 허락하고 싶어.

sanitize 문서는 사용 가능한 옵션에 대한 전체 문서에 대한

참조 ActionView :: 자료를 말한다.

그러나 설명서는 거짓말입니다. ActionView::Base에는 사용 가능한 옵션에 대해 아무것도 나와 있지 않습니다.

그래서 평상시처럼 소스를 파고 가서 인터페이스를 자동 변경하지 않기를 바랍니다. 코드를 통해 추적 조금 yields this :

def tokenize(text, options) 
    options[:parent] = [] 
    options[:attributes] ||= allowed_attributes 
    options[:tags]  ||= allowed_tags 
    super 
end 

def process_node(node, result, options) 
    result << case node 
    when HTML::Tag 
     if node.closing == :close 
     options[:parent].shift 
     else 
     options[:parent].unshift node.name 
     end 

     process_attributes_for node, options 

     options[:tags].include?(node.name) ? node : nil 
    else 
     bad_tags.include?(options[:parent].first) ? nil : node.to_s.gsub(/</, "&lt;") 
    end 
end 

tokenize에서 options[:tags]options[:tags]process_node에 사용되는 방법의 기본값은 관심있는 및 options[:tags] 후 아무것도 경우는 전체 집합을 포함해야한다는 것을 우리에게 말해 태그 집합을 제어하기위한 다른 옵션은 없습니다. (

def sanitized_allowed_tags 
    white_list_sanitizer.allowed_tags 
end 

당신은 다음과 같은 무언가를 자신의 도우미를 추가 할 수 있어야한다 : 우리가 sanitize_helper.rb 보면

또한, 우리는 sanitized_allowed_tags는 소독제 화이트리스트에 allowed_tags 단지 래퍼 것을 볼 테스트되지 않은 코드) - 최고의 - 내 - 헤드 - 오프 :

def sensible_sanitize(html, options) 
    if options.include? :not_tags 
     options[:tags] = ActionView::Base.sanitized_allowed_tags - options[:not_tags] 
    end 
    sanitize html, options 
end 

다음 수

<%= sensible_sanitize @stuff, :not_tags => [ 'div' ] %> 

<div>을 제외한 표준 기본 태그를 사용하십시오.

+0

와우, 그런 깊은 답장을 보내 주셔서 감사합니다! – LanguagesNamedAfterCofee

+0

@ Lang : 나는 내 결론을 정당화하기 위해 소스를 파헤쳐 야했다. 모든 것을 공유하는 것이 아니라 무언가를 공유하지 말라. 나는 사람들에게 물고기를 가르쳐주는 방법을 선호한다. (http://en.wikiquote.org/wiki/English_proverbs # Give_a_man_a_fish). –

1

나는 태그를 전달할 방법을 찾고 있으며 그 무의 대답은 좋아 보인다.

저는 ActionView :: Base가 대체하지 않고 sanitized_allowed_tags =를 재정의하여 기대했던 것보다 더 까다롭기 때문에 전역 적으로 설정하려고했습니다.

나는 내 응용 프로그램에 다음과 같이 끝났다.rb :

SANITIZE_ALLOWED_TAGS = %w{a ul ol li b i} 
config.after_initialize do 
    ActionView::Base.sanitized_allowed_tags.clear 
    ActionView::Base.sanitized_allowed_tags += SANITIZE_ALLOWED_TAGS 
end 
관련 문제