예 통화 단위로 허용 할 태그와 속성을 지정할 수 있습니다. 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(/</, "<")
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>
을 제외한 표준 기본 태그를 사용하십시오.
와우, 그런 깊은 답장을 보내 주셔서 감사합니다! – LanguagesNamedAfterCofee
@ Lang : 나는 내 결론을 정당화하기 위해 소스를 파헤쳐 야했다. 모든 것을 공유하는 것이 아니라 무언가를 공유하지 말라. 나는 사람들에게 물고기를 가르쳐주는 방법을 선호한다. (http://en.wikiquote.org/wiki/English_proverbs # Give_a_man_a_fish). –