Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / actionpack / lib / action_view / helpers / sanitize_helper.rb
1 require 'action_view/helpers/tag_helper'
2
3 module ActionView
4 module Helpers #:nodoc:
5 # The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements.
6 # These helper methods extend ActionView making them callable within your template files.
7 module SanitizeHelper
8 # This +sanitize+ helper will html encode all tags and strip all attributes that aren't specifically allowed.
9 # It also strips href/src tags with invalid protocols, like javascript: especially. It does its best to counter any
10 # tricks that hackers may use, like throwing in unicode/ascii/hex values to get past the javascript: filters. Check out
11 # the extensive test suite.
12 #
13 # <%= sanitize @article.body %>
14 #
15 # You can add or remove tags/attributes if you want to customize it a bit. See ActionView::Base for full docs on the
16 # available options. You can add tags/attributes for single uses of +sanitize+ by passing either the <tt>:attributes</tt> or <tt>:tags</tt> options:
17 #
18 # Normal Use
19 #
20 # <%= sanitize @article.body %>
21 #
22 # Custom Use (only the mentioned tags and attributes are allowed, nothing else)
23 #
24 # <%= sanitize @article.body, :tags => %w(table tr td), :attributes => %w(id class style)
25 #
26 # Add table tags to the default allowed tags
27 #
28 # Rails::Initializer.run do |config|
29 # config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
30 # end
31 #
32 # Remove tags to the default allowed tags
33 #
34 # Rails::Initializer.run do |config|
35 # config.after_initialize do
36 # ActionView::Base.sanitized_allowed_tags.delete 'div'
37 # end
38 # end
39 #
40 # Change allowed default attributes
41 #
42 # Rails::Initializer.run do |config|
43 # config.action_view.sanitized_allowed_attributes = 'id', 'class', 'style'
44 # end
45 #
46 # Please note that sanitizing user-provided text does not guarantee that the
47 # resulting markup is valid (conforming to a document type) or even well-formed.
48 # The output may still contain e.g. unescaped '<', '>', '&' characters and
49 # confuse browsers.
50 #
51 def sanitize(html, options = {})
52 self.class.white_list_sanitizer.sanitize(html, options)
53 end
54
55 # Sanitizes a block of CSS code. Used by +sanitize+ when it comes across a style attribute.
56 def sanitize_css(style)
57 self.class.white_list_sanitizer.sanitize_css(style)
58 end
59
60 # Strips all HTML tags from the +html+, including comments. This uses the
61 # html-scanner tokenizer and so its HTML parsing ability is limited by
62 # that of html-scanner.
63 #
64 # ==== Examples
65 #
66 # strip_tags("Strip <i>these</i> tags!")
67 # # => Strip these tags!
68 #
69 # strip_tags("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
70 # # => Bold no more! See more here...
71 #
72 # strip_tags("<div id='top-bar'>Welcome to my website!</div>")
73 # # => Welcome to my website!
74 def strip_tags(html)
75 self.class.full_sanitizer.sanitize(html)
76 end
77
78 # Strips all link tags from +text+ leaving just the link text.
79 #
80 # ==== Examples
81 # strip_links('<a href="http://www.rubyonrails.org">Ruby on Rails</a>')
82 # # => Ruby on Rails
83 #
84 # strip_links('Please e-mail me at <a href="mailto:me@email.com">me@email.com</a>.')
85 # # => Please e-mail me at me@email.com.
86 #
87 # strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.')
88 # # => Blog: Visit
89 def strip_links(html)
90 self.class.link_sanitizer.sanitize(html)
91 end
92
93 module ClassMethods #:nodoc:
94 attr_writer :full_sanitizer, :link_sanitizer, :white_list_sanitizer
95
96 def sanitized_protocol_separator
97 white_list_sanitizer.protocol_separator
98 end
99
100 def sanitized_uri_attributes
101 white_list_sanitizer.uri_attributes
102 end
103
104 def sanitized_bad_tags
105 white_list_sanitizer.bad_tags
106 end
107
108 def sanitized_allowed_tags
109 white_list_sanitizer.allowed_tags
110 end
111
112 def sanitized_allowed_attributes
113 white_list_sanitizer.allowed_attributes
114 end
115
116 def sanitized_allowed_css_properties
117 white_list_sanitizer.allowed_css_properties
118 end
119
120 def sanitized_allowed_css_keywords
121 white_list_sanitizer.allowed_css_keywords
122 end
123
124 def sanitized_shorthand_css_properties
125 white_list_sanitizer.shorthand_css_properties
126 end
127
128 def sanitized_allowed_protocols
129 white_list_sanitizer.allowed_protocols
130 end
131
132 def sanitized_protocol_separator=(value)
133 white_list_sanitizer.protocol_separator = value
134 end
135
136 # Gets the HTML::FullSanitizer instance used by +strip_tags+. Replace with
137 # any object that responds to +sanitize+.
138 #
139 # Rails::Initializer.run do |config|
140 # config.action_view.full_sanitizer = MySpecialSanitizer.new
141 # end
142 #
143 def full_sanitizer
144 @full_sanitizer ||= HTML::FullSanitizer.new
145 end
146
147 # Gets the HTML::LinkSanitizer instance used by +strip_links+. Replace with
148 # any object that responds to +sanitize+.
149 #
150 # Rails::Initializer.run do |config|
151 # config.action_view.link_sanitizer = MySpecialSanitizer.new
152 # end
153 #
154 def link_sanitizer
155 @link_sanitizer ||= HTML::LinkSanitizer.new
156 end
157
158 # Gets the HTML::WhiteListSanitizer instance used by sanitize and +sanitize_css+.
159 # Replace with any object that responds to +sanitize+.
160 #
161 # Rails::Initializer.run do |config|
162 # config.action_view.white_list_sanitizer = MySpecialSanitizer.new
163 # end
164 #
165 def white_list_sanitizer
166 @white_list_sanitizer ||= HTML::WhiteListSanitizer.new
167 end
168
169 # Adds valid HTML attributes that the +sanitize+ helper checks for URIs.
170 #
171 # Rails::Initializer.run do |config|
172 # config.action_view.sanitized_uri_attributes = 'lowsrc', 'target'
173 # end
174 #
175 def sanitized_uri_attributes=(attributes)
176 HTML::WhiteListSanitizer.uri_attributes.merge(attributes)
177 end
178
179 # Adds to the Set of 'bad' tags for the +sanitize+ helper.
180 #
181 # Rails::Initializer.run do |config|
182 # config.action_view.sanitized_bad_tags = 'embed', 'object'
183 # end
184 #
185 def sanitized_bad_tags=(attributes)
186 HTML::WhiteListSanitizer.bad_tags.merge(attributes)
187 end
188
189 # Adds to the Set of allowed tags for the +sanitize+ helper.
190 #
191 # Rails::Initializer.run do |config|
192 # config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
193 # end
194 #
195 def sanitized_allowed_tags=(attributes)
196 HTML::WhiteListSanitizer.allowed_tags.merge(attributes)
197 end
198
199 # Adds to the Set of allowed HTML attributes for the +sanitize+ helper.
200 #
201 # Rails::Initializer.run do |config|
202 # config.action_view.sanitized_allowed_attributes = 'onclick', 'longdesc'
203 # end
204 #
205 def sanitized_allowed_attributes=(attributes)
206 HTML::WhiteListSanitizer.allowed_attributes.merge(attributes)
207 end
208
209 # Adds to the Set of allowed CSS properties for the #sanitize and +sanitize_css+ helpers.
210 #
211 # Rails::Initializer.run do |config|
212 # config.action_view.sanitized_allowed_css_properties = 'expression'
213 # end
214 #
215 def sanitized_allowed_css_properties=(attributes)
216 HTML::WhiteListSanitizer.allowed_css_properties.merge(attributes)
217 end
218
219 # Adds to the Set of allowed CSS keywords for the +sanitize+ and +sanitize_css+ helpers.
220 #
221 # Rails::Initializer.run do |config|
222 # config.action_view.sanitized_allowed_css_keywords = 'expression'
223 # end
224 #
225 def sanitized_allowed_css_keywords=(attributes)
226 HTML::WhiteListSanitizer.allowed_css_keywords.merge(attributes)
227 end
228
229 # Adds to the Set of allowed shorthand CSS properties for the +sanitize+ and +sanitize_css+ helpers.
230 #
231 # Rails::Initializer.run do |config|
232 # config.action_view.sanitized_shorthand_css_properties = 'expression'
233 # end
234 #
235 def sanitized_shorthand_css_properties=(attributes)
236 HTML::WhiteListSanitizer.shorthand_css_properties.merge(attributes)
237 end
238
239 # Adds to the Set of allowed protocols for the +sanitize+ helper.
240 #
241 # Rails::Initializer.run do |config|
242 # config.action_view.sanitized_allowed_protocols = 'ssh', 'feed'
243 # end
244 #
245 def sanitized_allowed_protocols=(attributes)
246 HTML::WhiteListSanitizer.allowed_protocols.merge(attributes)
247 end
248 end
249 end
250 end
251 end