actionpack 4.0.1 → 4.2.11.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionpack might be problematic. Click here for more details.

Files changed (241) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +402 -1173
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +7 -7
  5. data/lib/abstract_controller/base.rb +39 -7
  6. data/lib/abstract_controller/callbacks.rb +32 -53
  7. data/lib/abstract_controller/collector.rb +11 -1
  8. data/lib/abstract_controller/helpers.rb +26 -16
  9. data/lib/abstract_controller/railties/routes_helpers.rb +3 -3
  10. data/lib/abstract_controller/rendering.rb +57 -127
  11. data/lib/abstract_controller/url_for.rb +1 -1
  12. data/lib/abstract_controller.rb +1 -2
  13. data/lib/action_controller/base.rb +19 -10
  14. data/lib/action_controller/caching/fragments.rb +7 -1
  15. data/lib/action_controller/caching.rb +2 -12
  16. data/lib/action_controller/log_subscriber.rb +29 -20
  17. data/lib/action_controller/metal/conditional_get.rb +37 -12
  18. data/lib/action_controller/metal/data_streaming.rb +1 -1
  19. data/lib/action_controller/metal/etag_with_template_digest.rb +50 -0
  20. data/lib/action_controller/metal/exceptions.rb +1 -1
  21. data/lib/action_controller/metal/flash.rb +17 -0
  22. data/lib/action_controller/metal/force_ssl.rb +2 -2
  23. data/lib/action_controller/metal/head.rb +8 -6
  24. data/lib/action_controller/metal/helpers.rb +6 -2
  25. data/lib/action_controller/metal/http_authentication.rb +45 -23
  26. data/lib/action_controller/metal/instrumentation.rb +9 -6
  27. data/lib/action_controller/metal/live.rb +173 -20
  28. data/lib/action_controller/metal/mime_responds.rb +127 -232
  29. data/lib/action_controller/metal/params_wrapper.rb +16 -9
  30. data/lib/action_controller/metal/rack_delegation.rb +1 -1
  31. data/lib/action_controller/metal/redirecting.rb +34 -26
  32. data/lib/action_controller/metal/renderers.rb +39 -12
  33. data/lib/action_controller/metal/rendering.rb +41 -14
  34. data/lib/action_controller/metal/request_forgery_protection.rb +147 -19
  35. data/lib/action_controller/metal/streaming.rb +19 -21
  36. data/lib/action_controller/metal/strong_parameters.rb +166 -22
  37. data/lib/action_controller/metal/testing.rb +0 -1
  38. data/lib/action_controller/metal/url_for.rb +11 -12
  39. data/lib/action_controller/metal.rb +14 -8
  40. data/lib/action_controller/model_naming.rb +1 -1
  41. data/lib/action_controller/railtie.rb +5 -1
  42. data/lib/action_controller/test_case.rb +160 -94
  43. data/lib/action_controller.rb +2 -18
  44. data/lib/action_dispatch/http/cache.rb +5 -4
  45. data/lib/action_dispatch/http/filter_parameters.rb +2 -2
  46. data/lib/action_dispatch/http/filter_redirect.rb +5 -4
  47. data/lib/action_dispatch/http/headers.rb +46 -10
  48. data/lib/action_dispatch/http/mime_negotiation.rb +31 -4
  49. data/lib/action_dispatch/http/mime_type.rb +25 -26
  50. data/lib/action_dispatch/http/mime_types.rb +1 -0
  51. data/lib/action_dispatch/http/parameter_filter.rb +1 -1
  52. data/lib/action_dispatch/http/parameters.rb +25 -41
  53. data/lib/action_dispatch/http/request.rb +49 -32
  54. data/lib/action_dispatch/http/response.rb +127 -25
  55. data/lib/action_dispatch/http/upload.rb +9 -21
  56. data/lib/action_dispatch/http/url.rb +97 -70
  57. data/lib/action_dispatch/journey/formatter.rb +35 -19
  58. data/lib/action_dispatch/journey/gtg/builder.rb +3 -3
  59. data/lib/action_dispatch/journey/gtg/simulator.rb +10 -7
  60. data/lib/action_dispatch/journey/gtg/transition_table.rb +23 -33
  61. data/lib/action_dispatch/journey/nfa/dot.rb +2 -2
  62. data/lib/action_dispatch/journey/nfa/simulator.rb +1 -1
  63. data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -5
  64. data/lib/action_dispatch/journey/nodes/node.rb +4 -0
  65. data/lib/action_dispatch/journey/parser.rb +51 -59
  66. data/lib/action_dispatch/journey/parser.y +12 -10
  67. data/lib/action_dispatch/journey/path/pattern.rb +16 -19
  68. data/lib/action_dispatch/journey/route.rb +8 -19
  69. data/lib/action_dispatch/journey/router/strexp.rb +9 -6
  70. data/lib/action_dispatch/journey/router/utils.rb +54 -18
  71. data/lib/action_dispatch/journey/router.rb +53 -75
  72. data/lib/action_dispatch/journey/routes.rb +4 -0
  73. data/lib/action_dispatch/journey/scanner.rb +5 -5
  74. data/lib/action_dispatch/journey/visitors.rb +81 -60
  75. data/lib/action_dispatch/journey/visualizer/fsm.css +0 -4
  76. data/lib/action_dispatch/journey/visualizer/index.html.erb +2 -2
  77. data/lib/action_dispatch/middleware/callbacks.rb +7 -7
  78. data/lib/action_dispatch/middleware/cookies.rb +119 -43
  79. data/lib/action_dispatch/middleware/debug_exceptions.rb +32 -13
  80. data/lib/action_dispatch/middleware/exception_wrapper.rb +60 -20
  81. data/lib/action_dispatch/middleware/flash.rb +37 -24
  82. data/lib/action_dispatch/middleware/params_parser.rb +2 -2
  83. data/lib/action_dispatch/middleware/public_exceptions.rb +12 -3
  84. data/lib/action_dispatch/middleware/reloader.rb +11 -2
  85. data/lib/action_dispatch/middleware/remote_ip.rb +40 -54
  86. data/lib/action_dispatch/middleware/request_id.rb +1 -1
  87. data/lib/action_dispatch/middleware/session/cache_store.rb +3 -3
  88. data/lib/action_dispatch/middleware/session/cookie_store.rb +8 -7
  89. data/lib/action_dispatch/middleware/show_exceptions.rb +6 -2
  90. data/lib/action_dispatch/middleware/ssl.rb +10 -7
  91. data/lib/action_dispatch/middleware/static.rb +79 -23
  92. data/lib/action_dispatch/middleware/templates/rescues/{_request_and_response.erb → _request_and_response.html.erb} +0 -0
  93. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +23 -0
  94. data/lib/action_dispatch/middleware/templates/rescues/_source.erb +21 -19
  95. data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +52 -0
  96. data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
  97. data/lib/action_dispatch/middleware/templates/rescues/{diagnostics.erb → diagnostics.html.erb} +1 -1
  98. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
  99. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +6 -0
  100. data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +11 -0
  101. data/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb +3 -0
  102. data/lib/action_dispatch/middleware/templates/rescues/{routing_error.erb → routing_error.html.erb} +3 -1
  103. data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +11 -0
  104. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +20 -0
  105. data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +7 -0
  106. data/lib/action_dispatch/middleware/templates/rescues/{unknown_action.erb → unknown_action.html.erb} +1 -1
  107. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +3 -0
  108. data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +120 -64
  109. data/lib/action_dispatch/railtie.rb +5 -2
  110. data/lib/action_dispatch/request/session.rb +12 -0
  111. data/lib/action_dispatch/request/utils.rb +35 -0
  112. data/lib/action_dispatch/routing/endpoint.rb +10 -0
  113. data/lib/action_dispatch/routing/inspector.rb +11 -17
  114. data/lib/action_dispatch/routing/mapper.rb +519 -312
  115. data/lib/action_dispatch/routing/polymorphic_routes.rb +204 -79
  116. data/lib/action_dispatch/routing/redirection.rb +51 -26
  117. data/lib/action_dispatch/routing/route_set.rb +331 -206
  118. data/lib/action_dispatch/routing/routes_proxy.rb +5 -4
  119. data/lib/action_dispatch/routing/url_for.rb +19 -5
  120. data/lib/action_dispatch/routing.rb +9 -6
  121. data/lib/action_dispatch/testing/assertions/dom.rb +2 -26
  122. data/lib/action_dispatch/testing/assertions/response.rb +9 -15
  123. data/lib/action_dispatch/testing/assertions/routing.rb +22 -22
  124. data/lib/action_dispatch/testing/assertions/selector.rb +2 -429
  125. data/lib/action_dispatch/testing/assertions/tag.rb +2 -134
  126. data/lib/action_dispatch/testing/assertions.rb +11 -7
  127. data/lib/action_dispatch/testing/integration.rb +31 -29
  128. data/lib/action_dispatch/testing/test_request.rb +1 -1
  129. data/lib/action_dispatch/testing/test_response.rb +1 -5
  130. data/lib/action_dispatch.rb +5 -8
  131. data/lib/action_pack/gem_version.rb +15 -0
  132. data/lib/action_pack/version.rb +4 -7
  133. data/lib/action_pack.rb +1 -1
  134. metadata +77 -159
  135. data/lib/abstract_controller/layouts.rb +0 -423
  136. data/lib/abstract_controller/view_paths.rb +0 -96
  137. data/lib/action_controller/deprecated/integration_test.rb +0 -5
  138. data/lib/action_controller/deprecated.rb +0 -7
  139. data/lib/action_controller/metal/responder.rb +0 -287
  140. data/lib/action_controller/record_identifier.rb +0 -31
  141. data/lib/action_controller/vendor/html-scanner.rb +0 -5
  142. data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +0 -24
  143. data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +0 -7
  144. data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +0 -43
  145. data/lib/action_view/base.rb +0 -201
  146. data/lib/action_view/buffers.rb +0 -49
  147. data/lib/action_view/context.rb +0 -36
  148. data/lib/action_view/dependency_tracker.rb +0 -93
  149. data/lib/action_view/digestor.rb +0 -113
  150. data/lib/action_view/flows.rb +0 -76
  151. data/lib/action_view/helpers/active_model_helper.rb +0 -49
  152. data/lib/action_view/helpers/asset_tag_helper.rb +0 -320
  153. data/lib/action_view/helpers/asset_url_helper.rb +0 -355
  154. data/lib/action_view/helpers/atom_feed_helper.rb +0 -203
  155. data/lib/action_view/helpers/cache_helper.rb +0 -196
  156. data/lib/action_view/helpers/capture_helper.rb +0 -216
  157. data/lib/action_view/helpers/controller_helper.rb +0 -25
  158. data/lib/action_view/helpers/csrf_helper.rb +0 -30
  159. data/lib/action_view/helpers/date_helper.rb +0 -1083
  160. data/lib/action_view/helpers/debug_helper.rb +0 -39
  161. data/lib/action_view/helpers/form_helper.rb +0 -1880
  162. data/lib/action_view/helpers/form_options_helper.rb +0 -838
  163. data/lib/action_view/helpers/form_tag_helper.rb +0 -785
  164. data/lib/action_view/helpers/javascript_helper.rb +0 -117
  165. data/lib/action_view/helpers/number_helper.rb +0 -441
  166. data/lib/action_view/helpers/output_safety_helper.rb +0 -38
  167. data/lib/action_view/helpers/record_tag_helper.rb +0 -106
  168. data/lib/action_view/helpers/rendering_helper.rb +0 -90
  169. data/lib/action_view/helpers/sanitize_helper.rb +0 -256
  170. data/lib/action_view/helpers/tag_helper.rb +0 -173
  171. data/lib/action_view/helpers/tags/base.rb +0 -148
  172. data/lib/action_view/helpers/tags/check_box.rb +0 -64
  173. data/lib/action_view/helpers/tags/checkable.rb +0 -16
  174. data/lib/action_view/helpers/tags/collection_check_boxes.rb +0 -44
  175. data/lib/action_view/helpers/tags/collection_helpers.rb +0 -84
  176. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +0 -36
  177. data/lib/action_view/helpers/tags/collection_select.rb +0 -28
  178. data/lib/action_view/helpers/tags/color_field.rb +0 -25
  179. data/lib/action_view/helpers/tags/date_field.rb +0 -13
  180. data/lib/action_view/helpers/tags/date_select.rb +0 -72
  181. data/lib/action_view/helpers/tags/datetime_field.rb +0 -22
  182. data/lib/action_view/helpers/tags/datetime_local_field.rb +0 -19
  183. data/lib/action_view/helpers/tags/datetime_select.rb +0 -8
  184. data/lib/action_view/helpers/tags/email_field.rb +0 -8
  185. data/lib/action_view/helpers/tags/file_field.rb +0 -8
  186. data/lib/action_view/helpers/tags/grouped_collection_select.rb +0 -29
  187. data/lib/action_view/helpers/tags/hidden_field.rb +0 -8
  188. data/lib/action_view/helpers/tags/label.rb +0 -66
  189. data/lib/action_view/helpers/tags/month_field.rb +0 -13
  190. data/lib/action_view/helpers/tags/number_field.rb +0 -18
  191. data/lib/action_view/helpers/tags/password_field.rb +0 -12
  192. data/lib/action_view/helpers/tags/radio_button.rb +0 -31
  193. data/lib/action_view/helpers/tags/range_field.rb +0 -8
  194. data/lib/action_view/helpers/tags/search_field.rb +0 -24
  195. data/lib/action_view/helpers/tags/select.rb +0 -40
  196. data/lib/action_view/helpers/tags/tel_field.rb +0 -8
  197. data/lib/action_view/helpers/tags/text_area.rb +0 -18
  198. data/lib/action_view/helpers/tags/text_field.rb +0 -29
  199. data/lib/action_view/helpers/tags/time_field.rb +0 -13
  200. data/lib/action_view/helpers/tags/time_select.rb +0 -8
  201. data/lib/action_view/helpers/tags/time_zone_select.rb +0 -20
  202. data/lib/action_view/helpers/tags/url_field.rb +0 -8
  203. data/lib/action_view/helpers/tags/week_field.rb +0 -13
  204. data/lib/action_view/helpers/tags.rb +0 -39
  205. data/lib/action_view/helpers/text_helper.rb +0 -443
  206. data/lib/action_view/helpers/translation_helper.rb +0 -107
  207. data/lib/action_view/helpers/url_helper.rb +0 -635
  208. data/lib/action_view/helpers.rb +0 -58
  209. data/lib/action_view/locale/en.yml +0 -56
  210. data/lib/action_view/log_subscriber.rb +0 -30
  211. data/lib/action_view/lookup_context.rb +0 -241
  212. data/lib/action_view/model_naming.rb +0 -12
  213. data/lib/action_view/path_set.rb +0 -77
  214. data/lib/action_view/railtie.rb +0 -43
  215. data/lib/action_view/record_identifier.rb +0 -84
  216. data/lib/action_view/renderer/abstract_renderer.rb +0 -47
  217. data/lib/action_view/renderer/partial_renderer.rb +0 -492
  218. data/lib/action_view/renderer/renderer.rb +0 -50
  219. data/lib/action_view/renderer/streaming_template_renderer.rb +0 -103
  220. data/lib/action_view/renderer/template_renderer.rb +0 -96
  221. data/lib/action_view/routing_url_for.rb +0 -107
  222. data/lib/action_view/tasks/dependencies.rake +0 -17
  223. data/lib/action_view/template/error.rb +0 -138
  224. data/lib/action_view/template/handlers/builder.rb +0 -26
  225. data/lib/action_view/template/handlers/erb.rb +0 -146
  226. data/lib/action_view/template/handlers/raw.rb +0 -11
  227. data/lib/action_view/template/handlers.rb +0 -53
  228. data/lib/action_view/template/resolver.rb +0 -326
  229. data/lib/action_view/template/text.rb +0 -34
  230. data/lib/action_view/template/types.rb +0 -57
  231. data/lib/action_view/template.rb +0 -339
  232. data/lib/action_view/test_case.rb +0 -270
  233. data/lib/action_view/testing/resolvers.rb +0 -50
  234. data/lib/action_view/vendor/html-scanner/html/document.rb +0 -68
  235. data/lib/action_view/vendor/html-scanner/html/node.rb +0 -532
  236. data/lib/action_view/vendor/html-scanner/html/sanitizer.rb +0 -188
  237. data/lib/action_view/vendor/html-scanner/html/selector.rb +0 -830
  238. data/lib/action_view/vendor/html-scanner/html/tokenizer.rb +0 -107
  239. data/lib/action_view/vendor/html-scanner/html/version.rb +0 -11
  240. data/lib/action_view/vendor/html-scanner.rb +0 -20
  241. data/lib/action_view.rb +0 -93
@@ -1,106 +0,0 @@
1
- module ActionView
2
- # = Action View Record Tag Helpers
3
- module Helpers
4
- module RecordTagHelper
5
- include ActionView::RecordIdentifier
6
-
7
- # Produces a wrapper DIV element with id and class parameters that
8
- # relate to the specified Active Record object. Usage example:
9
- #
10
- # <%= div_for(@person, class: "foo") do %>
11
- # <%= @person.name %>
12
- # <% end %>
13
- #
14
- # produces:
15
- #
16
- # <div id="person_123" class="person foo"> Joe Bloggs </div>
17
- #
18
- # You can also pass an array of Active Record objects, which will then
19
- # get iterated over and yield each record as an argument for the block.
20
- # For example:
21
- #
22
- # <%= div_for(@people, class: "foo") do |person| %>
23
- # <%= person.name %>
24
- # <% end %>
25
- #
26
- # produces:
27
- #
28
- # <div id="person_123" class="person foo"> Joe Bloggs </div>
29
- # <div id="person_124" class="person foo"> Jane Bloggs </div>
30
- #
31
- def div_for(record, *args, &block)
32
- content_tag_for(:div, record, *args, &block)
33
- end
34
-
35
- # content_tag_for creates an HTML element with id and class parameters
36
- # that relate to the specified Active Record object. For example:
37
- #
38
- # <%= content_tag_for(:tr, @person) do %>
39
- # <td><%= @person.first_name %></td>
40
- # <td><%= @person.last_name %></td>
41
- # <% end %>
42
- #
43
- # would produce the following HTML (assuming @person is an instance of
44
- # a Person object, with an id value of 123):
45
- #
46
- # <tr id="person_123" class="person">....</tr>
47
- #
48
- # If you require the HTML id attribute to have a prefix, you can specify it:
49
- #
50
- # <%= content_tag_for(:tr, @person, :foo) do %> ...
51
- #
52
- # produces:
53
- #
54
- # <tr id="foo_person_123" class="person">...
55
- #
56
- # You can also pass an array of objects which this method will loop through
57
- # and yield the current object to the supplied block, reducing the need for
58
- # having to iterate through the object (using <tt>each</tt>) beforehand.
59
- # For example (assuming @people is an array of Person objects):
60
- #
61
- # <%= content_tag_for(:tr, @people) do |person| %>
62
- # <td><%= person.first_name %></td>
63
- # <td><%= person.last_name %></td>
64
- # <% end %>
65
- #
66
- # produces:
67
- #
68
- # <tr id="person_123" class="person">...</tr>
69
- # <tr id="person_124" class="person">...</tr>
70
- #
71
- # content_tag_for also accepts a hash of options, which will be converted to
72
- # additional HTML attributes. If you specify a <tt>:class</tt> value, it will be combined
73
- # with the default class name for your object. For example:
74
- #
75
- # <%= content_tag_for(:li, @person, class: "bar") %>...
76
- #
77
- # produces:
78
- #
79
- # <li id="person_123" class="person bar">...
80
- #
81
- def content_tag_for(tag_name, single_or_multiple_records, prefix = nil, options = nil, &block)
82
- options, prefix = prefix, nil if prefix.is_a?(Hash)
83
-
84
- Array(single_or_multiple_records).map do |single_record|
85
- content_tag_for_single_record(tag_name, single_record, prefix, options, &block)
86
- end.join("\n").html_safe
87
- end
88
-
89
- private
90
-
91
- # Called by <tt>content_tag_for</tt> internally to render a content tag
92
- # for each record.
93
- def content_tag_for_single_record(tag_name, record, prefix, options, &block)
94
- options = options ? options.dup : {}
95
- options[:class] = [ dom_class(record, prefix), options[:class] ].compact
96
- options[:id] = dom_id(record, prefix)
97
-
98
- if block_given?
99
- content_tag(tag_name, capture(record, &block), options)
100
- else
101
- content_tag(tag_name, "", options)
102
- end
103
- end
104
- end
105
- end
106
- end
@@ -1,90 +0,0 @@
1
- module ActionView
2
- module Helpers
3
- # = Action View Rendering
4
- #
5
- # Implements methods that allow rendering from a view context.
6
- # In order to use this module, all you need is to implement
7
- # view_renderer that returns an ActionView::Renderer object.
8
- module RenderingHelper
9
- # Returns the result of a render that's dictated by the options hash. The primary options are:
10
- #
11
- # * <tt>:partial</tt> - See <tt>ActionView::PartialRenderer</tt>.
12
- # * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
13
- # * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
14
- # * <tt>:text</tt> - Renders the text passed in out.
15
- #
16
- # If no options hash is passed or :update specified, the default is to render a partial and use the second parameter
17
- # as the locals hash.
18
- def render(options = {}, locals = {}, &block)
19
- case options
20
- when Hash
21
- if block_given?
22
- view_renderer.render_partial(self, options.merge(:partial => options[:layout]), &block)
23
- else
24
- view_renderer.render(self, options)
25
- end
26
- else
27
- view_renderer.render_partial(self, :partial => options, :locals => locals)
28
- end
29
- end
30
-
31
- # Overwrites _layout_for in the context object so it supports the case a block is
32
- # passed to a partial. Returns the contents that are yielded to a layout, given a
33
- # name or a block.
34
- #
35
- # You can think of a layout as a method that is called with a block. If the user calls
36
- # <tt>yield :some_name</tt>, the block, by default, returns <tt>content_for(:some_name)</tt>.
37
- # If the user calls simply +yield+, the default block returns <tt>content_for(:layout)</tt>.
38
- #
39
- # The user can override this default by passing a block to the layout:
40
- #
41
- # # The template
42
- # <%= render layout: "my_layout" do %>
43
- # Content
44
- # <% end %>
45
- #
46
- # # The layout
47
- # <html>
48
- # <%= yield %>
49
- # </html>
50
- #
51
- # In this case, instead of the default block, which would return <tt>content_for(:layout)</tt>,
52
- # this method returns the block that was passed in to <tt>render :layout</tt>, and the response
53
- # would be
54
- #
55
- # <html>
56
- # Content
57
- # </html>
58
- #
59
- # Finally, the block can take block arguments, which can be passed in by +yield+:
60
- #
61
- # # The template
62
- # <%= render layout: "my_layout" do |customer| %>
63
- # Hello <%= customer.name %>
64
- # <% end %>
65
- #
66
- # # The layout
67
- # <html>
68
- # <%= yield Struct.new(:name).new("David") %>
69
- # </html>
70
- #
71
- # In this case, the layout would receive the block passed into <tt>render :layout</tt>,
72
- # and the struct specified would be passed into the block as an argument. The result
73
- # would be
74
- #
75
- # <html>
76
- # Hello David
77
- # </html>
78
- #
79
- def _layout_for(*args, &block)
80
- name = args.first
81
-
82
- if block && !name.is_a?(Symbol)
83
- capture(*args, &block)
84
- else
85
- super
86
- end
87
- end
88
- end
89
- end
90
- end
@@ -1,256 +0,0 @@
1
- require 'active_support/core_ext/object/try'
2
- require 'action_view/vendor/html-scanner'
3
-
4
- module ActionView
5
- # = Action View Sanitize Helpers
6
- module Helpers
7
- # The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements.
8
- # These helper methods extend Action View making them callable within your template files.
9
- module SanitizeHelper
10
- extend ActiveSupport::Concern
11
- # This +sanitize+ helper will html encode all tags and strip all attributes that
12
- # aren't specifically allowed.
13
- #
14
- # It also strips href/src tags with invalid protocols, like javascript: especially.
15
- # It does its best to counter any tricks that hackers may use, like throwing in
16
- # unicode/ascii/hex values to get past the javascript: filters. Check out
17
- # the extensive test suite.
18
- #
19
- # <%= sanitize @article.body %>
20
- #
21
- # You can add or remove tags/attributes if you want to customize it a bit.
22
- # See ActionView::Base for full docs on the available options. You can add
23
- # tags/attributes for single uses of +sanitize+ by passing either the
24
- # <tt>:attributes</tt> or <tt>:tags</tt> options:
25
- #
26
- # Normal Use
27
- #
28
- # <%= sanitize @article.body %>
29
- #
30
- # Custom Use (only the mentioned tags and attributes are allowed, nothing else)
31
- #
32
- # <%= sanitize @article.body, tags: %w(table tr td), attributes: %w(id class style) %>
33
- #
34
- # Add table tags to the default allowed tags
35
- #
36
- # class Application < Rails::Application
37
- # config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
38
- # end
39
- #
40
- # Remove tags to the default allowed tags
41
- #
42
- # class Application < Rails::Application
43
- # config.after_initialize do
44
- # ActionView::Base.sanitized_allowed_tags.delete 'div'
45
- # end
46
- # end
47
- #
48
- # Change allowed default attributes
49
- #
50
- # class Application < Rails::Application
51
- # config.action_view.sanitized_allowed_attributes = 'id', 'class', 'style'
52
- # end
53
- #
54
- # Please note that sanitizing user-provided text does not guarantee that the
55
- # resulting markup is valid (conforming to a document type) or even well-formed.
56
- # The output may still contain e.g. unescaped '<', '>', '&' characters and
57
- # confuse browsers.
58
- #
59
- def sanitize(html, options = {})
60
- self.class.white_list_sanitizer.sanitize(html, options).try(:html_safe)
61
- end
62
-
63
- # Sanitizes a block of CSS code. Used by +sanitize+ when it comes across a style attribute.
64
- def sanitize_css(style)
65
- self.class.white_list_sanitizer.sanitize_css(style)
66
- end
67
-
68
- # Strips all HTML tags from the +html+, including comments. This uses the
69
- # html-scanner tokenizer and so its HTML parsing ability is limited by
70
- # that of html-scanner.
71
- #
72
- # strip_tags("Strip <i>these</i> tags!")
73
- # # => Strip these tags!
74
- #
75
- # strip_tags("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
76
- # # => Bold no more! See more here...
77
- #
78
- # strip_tags("<div id='top-bar'>Welcome to my website!</div>")
79
- # # => Welcome to my website!
80
- def strip_tags(html)
81
- self.class.full_sanitizer.sanitize(html)
82
- end
83
-
84
- # Strips all link tags from +text+ leaving just the link text.
85
- #
86
- # strip_links('<a href="http://www.rubyonrails.org">Ruby on Rails</a>')
87
- # # => Ruby on Rails
88
- #
89
- # strip_links('Please e-mail me at <a href="mailto:me@email.com">me@email.com</a>.')
90
- # # => Please e-mail me at me@email.com.
91
- #
92
- # strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.')
93
- # # => Blog: Visit.
94
- def strip_links(html)
95
- self.class.link_sanitizer.sanitize(html)
96
- end
97
-
98
- module ClassMethods #:nodoc:
99
- attr_writer :full_sanitizer, :link_sanitizer, :white_list_sanitizer
100
-
101
- def sanitized_protocol_separator
102
- white_list_sanitizer.protocol_separator
103
- end
104
-
105
- def sanitized_uri_attributes
106
- white_list_sanitizer.uri_attributes
107
- end
108
-
109
- def sanitized_bad_tags
110
- white_list_sanitizer.bad_tags
111
- end
112
-
113
- def sanitized_allowed_tags
114
- white_list_sanitizer.allowed_tags
115
- end
116
-
117
- def sanitized_allowed_attributes
118
- white_list_sanitizer.allowed_attributes
119
- end
120
-
121
- def sanitized_allowed_css_properties
122
- white_list_sanitizer.allowed_css_properties
123
- end
124
-
125
- def sanitized_allowed_css_keywords
126
- white_list_sanitizer.allowed_css_keywords
127
- end
128
-
129
- def sanitized_shorthand_css_properties
130
- white_list_sanitizer.shorthand_css_properties
131
- end
132
-
133
- def sanitized_allowed_protocols
134
- white_list_sanitizer.allowed_protocols
135
- end
136
-
137
- def sanitized_protocol_separator=(value)
138
- white_list_sanitizer.protocol_separator = value
139
- end
140
-
141
- # Gets the HTML::FullSanitizer instance used by +strip_tags+. Replace with
142
- # any object that responds to +sanitize+.
143
- #
144
- # class Application < Rails::Application
145
- # config.action_view.full_sanitizer = MySpecialSanitizer.new
146
- # end
147
- #
148
- def full_sanitizer
149
- @full_sanitizer ||= HTML::FullSanitizer.new
150
- end
151
-
152
- # Gets the HTML::LinkSanitizer instance used by +strip_links+. Replace with
153
- # any object that responds to +sanitize+.
154
- #
155
- # class Application < Rails::Application
156
- # config.action_view.link_sanitizer = MySpecialSanitizer.new
157
- # end
158
- #
159
- def link_sanitizer
160
- @link_sanitizer ||= HTML::LinkSanitizer.new
161
- end
162
-
163
- # Gets the HTML::WhiteListSanitizer instance used by sanitize and +sanitize_css+.
164
- # Replace with any object that responds to +sanitize+.
165
- #
166
- # class Application < Rails::Application
167
- # config.action_view.white_list_sanitizer = MySpecialSanitizer.new
168
- # end
169
- #
170
- def white_list_sanitizer
171
- @white_list_sanitizer ||= HTML::WhiteListSanitizer.new
172
- end
173
-
174
- # Adds valid HTML attributes that the +sanitize+ helper checks for URIs.
175
- #
176
- # class Application < Rails::Application
177
- # config.action_view.sanitized_uri_attributes = 'lowsrc', 'target'
178
- # end
179
- #
180
- def sanitized_uri_attributes=(attributes)
181
- HTML::WhiteListSanitizer.uri_attributes.merge(attributes)
182
- end
183
-
184
- # Adds to the Set of 'bad' tags for the +sanitize+ helper.
185
- #
186
- # class Application < Rails::Application
187
- # config.action_view.sanitized_bad_tags = 'embed', 'object'
188
- # end
189
- #
190
- def sanitized_bad_tags=(attributes)
191
- HTML::WhiteListSanitizer.bad_tags.merge(attributes)
192
- end
193
-
194
- # Adds to the Set of allowed tags for the +sanitize+ helper.
195
- #
196
- # class Application < Rails::Application
197
- # config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
198
- # end
199
- #
200
- def sanitized_allowed_tags=(attributes)
201
- HTML::WhiteListSanitizer.allowed_tags.merge(attributes)
202
- end
203
-
204
- # Adds to the Set of allowed HTML attributes for the +sanitize+ helper.
205
- #
206
- # class Application < Rails::Application
207
- # config.action_view.sanitized_allowed_attributes = 'onclick', 'longdesc'
208
- # end
209
- #
210
- def sanitized_allowed_attributes=(attributes)
211
- HTML::WhiteListSanitizer.allowed_attributes.merge(attributes)
212
- end
213
-
214
- # Adds to the Set of allowed CSS properties for the #sanitize and +sanitize_css+ helpers.
215
- #
216
- # class Application < Rails::Application
217
- # config.action_view.sanitized_allowed_css_properties = 'expression'
218
- # end
219
- #
220
- def sanitized_allowed_css_properties=(attributes)
221
- HTML::WhiteListSanitizer.allowed_css_properties.merge(attributes)
222
- end
223
-
224
- # Adds to the Set of allowed CSS keywords for the +sanitize+ and +sanitize_css+ helpers.
225
- #
226
- # class Application < Rails::Application
227
- # config.action_view.sanitized_allowed_css_keywords = 'expression'
228
- # end
229
- #
230
- def sanitized_allowed_css_keywords=(attributes)
231
- HTML::WhiteListSanitizer.allowed_css_keywords.merge(attributes)
232
- end
233
-
234
- # Adds to the Set of allowed shorthand CSS properties for the +sanitize+ and +sanitize_css+ helpers.
235
- #
236
- # class Application < Rails::Application
237
- # config.action_view.sanitized_shorthand_css_properties = 'expression'
238
- # end
239
- #
240
- def sanitized_shorthand_css_properties=(attributes)
241
- HTML::WhiteListSanitizer.shorthand_css_properties.merge(attributes)
242
- end
243
-
244
- # Adds to the Set of allowed protocols for the +sanitize+ helper.
245
- #
246
- # class Application < Rails::Application
247
- # config.action_view.sanitized_allowed_protocols = 'ssh', 'feed'
248
- # end
249
- #
250
- def sanitized_allowed_protocols=(attributes)
251
- HTML::WhiteListSanitizer.allowed_protocols.merge(attributes)
252
- end
253
- end
254
- end
255
- end
256
- end
@@ -1,173 +0,0 @@
1
- require 'active_support/core_ext/string/output_safety'
2
- require 'set'
3
-
4
- module ActionView
5
- # = Action View Tag Helpers
6
- module Helpers #:nodoc:
7
- # Provides methods to generate HTML tags programmatically when you can't use
8
- # a Builder. By default, they output XHTML compliant tags.
9
- module TagHelper
10
- extend ActiveSupport::Concern
11
- include CaptureHelper
12
-
13
- BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked autobuffer
14
- autoplay controls loop selected hidden scoped async
15
- defer reversed ismap seemless muted required
16
- autofocus novalidate formnovalidate open pubdate itemscope).to_set
17
- BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map {|attribute| attribute.to_sym })
18
-
19
- PRE_CONTENT_STRINGS = {
20
- :textarea => "\n"
21
- }
22
-
23
- # Returns an empty HTML tag of type +name+ which by default is XHTML
24
- # compliant. Set +open+ to true to create an open tag compatible
25
- # with HTML 4.0 and below. Add HTML attributes by passing an attributes
26
- # hash to +options+. Set +escape+ to false to disable attribute value
27
- # escaping.
28
- #
29
- # ==== Options
30
- # You can use symbols or strings for the attribute names.
31
- #
32
- # Use +true+ with boolean attributes that can render with no value, like
33
- # +disabled+ and +readonly+.
34
- #
35
- # HTML5 <tt>data-*</tt> attributes can be set with a single +data+ key
36
- # pointing to a hash of sub-attributes.
37
- #
38
- # To play nicely with JavaScript conventions sub-attributes are dasherized.
39
- # For example, a key +user_id+ would render as <tt>data-user-id</tt> and
40
- # thus accessed as <tt>dataset.userId</tt>.
41
- #
42
- # Values are encoded to JSON, with the exception of strings and symbols.
43
- # This may come in handy when using jQuery's HTML5-aware <tt>.data()</tt>
44
- # from 1.4.3.
45
- #
46
- # ==== Examples
47
- # tag("br")
48
- # # => <br />
49
- #
50
- # tag("br", nil, true)
51
- # # => <br>
52
- #
53
- # tag("input", type: 'text', disabled: true)
54
- # # => <input type="text" disabled="disabled" />
55
- #
56
- # tag("img", src: "open & shut.png")
57
- # # => <img src="open &amp; shut.png" />
58
- #
59
- # tag("img", {src: "open &amp; shut.png"}, false, false)
60
- # # => <img src="open &amp; shut.png" />
61
- #
62
- # tag("div", data: {name: 'Stephen', city_state: %w(Chicago IL)})
63
- # # => <div data-name="Stephen" data-city-state="[&quot;Chicago&quot;,&quot;IL&quot;]" />
64
- def tag(name, options = nil, open = false, escape = true)
65
- "<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}".html_safe
66
- end
67
-
68
- # Returns an HTML block tag of type +name+ surrounding the +content+. Add
69
- # HTML attributes by passing an attributes hash to +options+.
70
- # Instead of passing the content as an argument, you can also use a block
71
- # in which case, you pass your +options+ as the second parameter.
72
- # Set escape to false to disable attribute value escaping.
73
- #
74
- # ==== Options
75
- # The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
76
- # <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
77
- # symbols or strings for the attribute names.
78
- #
79
- # ==== Examples
80
- # content_tag(:p, "Hello world!")
81
- # # => <p>Hello world!</p>
82
- # content_tag(:div, content_tag(:p, "Hello world!"), class: "strong")
83
- # # => <div class="strong"><p>Hello world!</p></div>
84
- # content_tag("select", options, multiple: true)
85
- # # => <select multiple="multiple">...options...</select>
86
- #
87
- # <%= content_tag :div, class: "strong" do -%>
88
- # Hello world!
89
- # <% end -%>
90
- # # => <div class="strong">Hello world!</div>
91
- def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
92
- if block_given?
93
- options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
94
- content_tag_string(name, capture(&block), options, escape)
95
- else
96
- content_tag_string(name, content_or_options_with_block, options, escape)
97
- end
98
- end
99
-
100
- # Returns a CDATA section with the given +content+. CDATA sections
101
- # are used to escape blocks of text containing characters which would
102
- # otherwise be recognized as markup. CDATA sections begin with the string
103
- # <tt><![CDATA[</tt> and end with (and may not contain) the string <tt>]]></tt>.
104
- #
105
- # cdata_section("<hello world>")
106
- # # => <![CDATA[<hello world>]]>
107
- #
108
- # cdata_section(File.read("hello_world.txt"))
109
- # # => <![CDATA[<hello from a text file]]>
110
- #
111
- # cdata_section("hello]]>world")
112
- # # => <![CDATA[hello]]]]><![CDATA[>world]]>
113
- def cdata_section(content)
114
- splitted = content.gsub(']]>', ']]]]><![CDATA[>')
115
- "<![CDATA[#{splitted}]]>".html_safe
116
- end
117
-
118
- # Returns an escaped version of +html+ without affecting existing escaped entities.
119
- #
120
- # escape_once("1 < 2 &amp; 3")
121
- # # => "1 &lt; 2 &amp; 3"
122
- #
123
- # escape_once("&lt;&lt; Accept & Checkout")
124
- # # => "&lt;&lt; Accept &amp; Checkout"
125
- def escape_once(html)
126
- ERB::Util.html_escape_once(html)
127
- end
128
-
129
- private
130
-
131
- def content_tag_string(name, content, options, escape = true)
132
- tag_options = tag_options(options, escape) if options
133
- content = ERB::Util.h(content) if escape
134
- "<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name.to_sym]}#{content}</#{name}>".html_safe
135
- end
136
-
137
- def tag_options(options, escape = true)
138
- return if options.blank?
139
- attrs = []
140
- options.each_pair do |key, value|
141
- if key.to_s == 'data' && value.is_a?(Hash)
142
- value.each_pair do |k, v|
143
- attrs << data_tag_option(k, v, escape)
144
- end
145
- elsif BOOLEAN_ATTRIBUTES.include?(key)
146
- attrs << boolean_tag_option(key) if value
147
- elsif !value.nil?
148
- attrs << tag_option(key, value, escape)
149
- end
150
- end
151
- " #{attrs.sort * ' '}".html_safe unless attrs.empty?
152
- end
153
-
154
- def data_tag_option(key, value, escape)
155
- key = "data-#{key.to_s.dasherize}"
156
- unless value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(BigDecimal)
157
- value = value.to_json
158
- end
159
- tag_option(key, value, escape)
160
- end
161
-
162
- def boolean_tag_option(key)
163
- %(#{key}="#{key}")
164
- end
165
-
166
- def tag_option(key, value, escape)
167
- value = value.join(" ") if value.is_a?(Array)
168
- value = ERB::Util.h(value) if escape
169
- %(#{key}="#{value}")
170
- end
171
- end
172
- end
173
- end