actionview 7.1.1 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +49 -0
- data/app/assets/javascripts/rails-ujs.esm.js +0 -7
- data/lib/action_view/gem_version.rb +1 -1
- data/lib/action_view/helpers/asset_tag_helper.rb +4 -4
- data/lib/action_view/helpers/capture_helper.rb +7 -1
- data/lib/action_view/helpers/form_helper.rb +10 -6
- data/lib/action_view/helpers/form_tag_helper.rb +6 -6
- data/lib/action_view/helpers/number_helper.rb +35 -329
- data/lib/action_view/helpers/sanitize_helper.rb +25 -14
- data/lib/action_view/helpers/text_helper.rb +124 -68
- data/lib/action_view/layouts.rb +2 -2
- data/lib/action_view/renderer/collection_renderer.rb +1 -1
- data/lib/action_view/template.rb +26 -12
- data/lib/action_view/test_case.rb +21 -19
- metadata +11 -11
@@ -15,11 +15,11 @@ module ActionView
|
|
15
15
|
|
16
16
|
# Sanitizes HTML input, stripping all but known-safe tags and attributes.
|
17
17
|
#
|
18
|
-
# It also strips href/src attributes with unsafe protocols like
|
18
|
+
# It also strips +href+ / +src+ attributes with unsafe protocols like +javascript:+, while
|
19
19
|
# also protecting against attempts to use Unicode, ASCII, and hex character references to work
|
20
20
|
# around these protocol filters.
|
21
21
|
#
|
22
|
-
# The default sanitizer is Rails::HTML5::SafeListSanitizer
|
22
|
+
# The default sanitizer is +Rails::HTML5::SafeListSanitizer+. See {Rails HTML
|
23
23
|
# Sanitizers}[https://github.com/rails/rails-html-sanitizer] for more information.
|
24
24
|
#
|
25
25
|
# Custom sanitization rules can also be provided.
|
@@ -29,24 +29,29 @@ module ActionView
|
|
29
29
|
#
|
30
30
|
# ==== Options
|
31
31
|
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
32
|
+
# [+:tags+]
|
33
|
+
# An array of allowed tags.
|
34
|
+
#
|
35
|
+
# [+:attributes+]
|
36
|
+
# An array of allowed attributes.
|
37
|
+
#
|
38
|
+
# [+:scrubber+]
|
39
|
+
# A {Rails::HTML scrubber}[https://github.com/rails/rails-html-sanitizer]
|
35
40
|
# or {Loofah::Scrubber}[https://github.com/flavorjones/loofah] object that
|
36
41
|
# defines custom sanitization rules. A custom scrubber takes precedence over
|
37
42
|
# custom tags and attributes.
|
38
43
|
#
|
39
44
|
# ==== Examples
|
40
45
|
#
|
41
|
-
# Normal use
|
46
|
+
# ===== Normal use
|
42
47
|
#
|
43
48
|
# <%= sanitize @comment.body %>
|
44
49
|
#
|
45
|
-
# Providing custom lists of permitted tags and attributes
|
50
|
+
# ===== Providing custom lists of permitted tags and attributes
|
46
51
|
#
|
47
52
|
# <%= sanitize @comment.body, tags: %w(strong em a), attributes: %w(href) %>
|
48
53
|
#
|
49
|
-
# Providing a custom Rails::HTML scrubber
|
54
|
+
# ===== Providing a custom +Rails::HTML+ scrubber
|
50
55
|
#
|
51
56
|
# class CommentScrubber < Rails::HTML::PermitScrubber
|
52
57
|
# def initialize
|
@@ -60,21 +65,27 @@ module ActionView
|
|
60
65
|
# end
|
61
66
|
# end
|
62
67
|
#
|
68
|
+
# <code></code>
|
69
|
+
#
|
63
70
|
# <%= sanitize @comment.body, scrubber: CommentScrubber.new %>
|
64
71
|
#
|
65
72
|
# See {Rails HTML Sanitizer}[https://github.com/rails/rails-html-sanitizer] for
|
66
|
-
# documentation about Rails::HTML scrubbers.
|
73
|
+
# documentation about +Rails::HTML+ scrubbers.
|
67
74
|
#
|
68
|
-
# Providing a custom Loofah::Scrubber
|
75
|
+
# ===== Providing a custom +Loofah::Scrubber+
|
69
76
|
#
|
70
77
|
# scrubber = Loofah::Scrubber.new do |node|
|
71
78
|
# node.remove if node.name == 'script'
|
72
79
|
# end
|
73
80
|
#
|
81
|
+
# <code></code>
|
82
|
+
#
|
74
83
|
# <%= sanitize @comment.body, scrubber: scrubber %>
|
75
84
|
#
|
76
85
|
# See {Loofah's documentation}[https://github.com/flavorjones/loofah] for more
|
77
|
-
# information about defining custom Loofah::Scrubber objects.
|
86
|
+
# information about defining custom +Loofah::Scrubber+ objects.
|
87
|
+
#
|
88
|
+
# ==== Global Configuration
|
78
89
|
#
|
79
90
|
# To set the default allowed tags or attributes across your application:
|
80
91
|
#
|
@@ -95,13 +106,13 @@ module ActionView
|
|
95
106
|
# # In config/application.rb
|
96
107
|
# config.action_view.sanitizer_vendor = Rails::HTML5::Sanitizer
|
97
108
|
#
|
98
|
-
# NOTE: Rails::HTML5::Sanitizer is not supported on JRuby, so on JRuby platforms \Rails will
|
99
|
-
# fall back to
|
109
|
+
# NOTE: +Rails::HTML5::Sanitizer+ is not supported on JRuby, so on JRuby platforms \Rails will
|
110
|
+
# fall back to using +Rails::HTML4::Sanitizer+.
|
100
111
|
def sanitize(html, options = {})
|
101
112
|
self.class.safe_list_sanitizer.sanitize(html, options)&.html_safe
|
102
113
|
end
|
103
114
|
|
104
|
-
# Sanitizes a block of CSS code. Used by
|
115
|
+
# Sanitizes a block of CSS code. Used by #sanitize when it comes across a style attribute.
|
105
116
|
def sanitize_css(style)
|
106
117
|
self.class.safe_list_sanitizer.sanitize_css(style)
|
107
118
|
end
|
@@ -41,21 +41,25 @@ module ActionView
|
|
41
41
|
include OutputSafetyHelper
|
42
42
|
|
43
43
|
# The preferred method of outputting text in your views is to use the
|
44
|
-
#
|
44
|
+
# <tt><%= "text" %></tt> eRuby syntax. The regular +puts+ and +print+ methods
|
45
45
|
# do not operate as expected in an eRuby code block. If you absolutely must
|
46
|
-
# output text within a non-output code block (i.e.,
|
46
|
+
# output text within a non-output code block (i.e., <tt><% %></tt>), you
|
47
|
+
# can use the +concat+ method.
|
48
|
+
#
|
49
|
+
# <% concat "hello" %> is equivalent to <%= "hello" %>
|
47
50
|
#
|
48
51
|
# <%
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
# if logged_in
|
53
|
-
# concat "Logged in!"
|
54
|
-
# else
|
55
|
-
# concat link_to('login', action: :login)
|
56
|
-
# end
|
57
|
-
# # will either display "Logged in!" or a login link
|
52
|
+
# unless signed_in?
|
53
|
+
# concat link_to("Sign In", action: :sign_in)
|
54
|
+
# end
|
58
55
|
# %>
|
56
|
+
#
|
57
|
+
# is equivalent to
|
58
|
+
#
|
59
|
+
# <% unless signed_in? %>
|
60
|
+
# <%= link_to "Sign In", action: :sign_in %>
|
61
|
+
# <% end %>
|
62
|
+
#
|
59
63
|
def concat(string)
|
60
64
|
output_buffer << string
|
61
65
|
end
|
@@ -64,17 +68,36 @@ module ActionView
|
|
64
68
|
output_buffer.respond_to?(:safe_concat) ? output_buffer.safe_concat(string) : concat(string)
|
65
69
|
end
|
66
70
|
|
67
|
-
# Truncates
|
68
|
-
#
|
69
|
-
#
|
71
|
+
# Truncates +text+ if it is longer than a specified +:length+. If +text+
|
72
|
+
# is truncated, an omission marker will be appended to the result for a
|
73
|
+
# total length not exceeding +:length+.
|
74
|
+
#
|
75
|
+
# You can also pass a block to render and append extra content after the
|
76
|
+
# omission marker when +text+ is truncated. However, this content _can_
|
77
|
+
# cause the total length to exceed +:length+ characters.
|
78
|
+
#
|
79
|
+
# The result will be escaped unless <tt>escape: false</tt> is specified.
|
80
|
+
# In any case, the result will be marked HTML-safe. Care should be taken
|
81
|
+
# if +text+ might contain HTML tags or entities, because truncation could
|
82
|
+
# produce invalid HTML, such as unbalanced or incomplete tags.
|
70
83
|
#
|
71
|
-
#
|
84
|
+
# ==== Options
|
85
|
+
#
|
86
|
+
# [+:length+]
|
87
|
+
# The maximum number of characters that should be returned, excluding
|
88
|
+
# any extra content from the block. Defaults to 30.
|
72
89
|
#
|
73
|
-
#
|
90
|
+
# [+:omission+]
|
91
|
+
# The string to append after truncating. Defaults to <tt>"..."</tt>.
|
74
92
|
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
93
|
+
# [+:separator+]
|
94
|
+
# A string or regexp used to find a breaking point at which to truncate.
|
95
|
+
# By default, truncation can occur at any character in +text+.
|
96
|
+
#
|
97
|
+
# [+:escape+]
|
98
|
+
# Whether to escape the result. Defaults to true.
|
99
|
+
#
|
100
|
+
# ==== Examples
|
78
101
|
#
|
79
102
|
# truncate("Once upon a time in a world far far away")
|
80
103
|
# # => "Once upon a time in a world..."
|
@@ -95,7 +118,7 @@ module ActionView
|
|
95
118
|
# # => "<p>Once upon a time in a wo..."
|
96
119
|
#
|
97
120
|
# truncate("Once upon a time in a world far far away") { link_to "Continue", "#" }
|
98
|
-
# # => "Once upon a time in a
|
121
|
+
# # => "Once upon a time in a world...<a href=\"#\">Continue</a>"
|
99
122
|
def truncate(text, options = {}, &block)
|
100
123
|
if text
|
101
124
|
length = options.fetch(:length, 30)
|
@@ -107,33 +130,47 @@ module ActionView
|
|
107
130
|
end
|
108
131
|
end
|
109
132
|
|
110
|
-
# Highlights
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
133
|
+
# Highlights occurrences of +phrases+ in +text+ by formatting them with a
|
134
|
+
# highlighter string. +phrases+ can be one or more strings or regular
|
135
|
+
# expressions. The result will be marked HTML safe. By default, +text+ is
|
136
|
+
# sanitized before highlighting to prevent possible XSS attacks.
|
137
|
+
#
|
138
|
+
# If a block is specified, it will be used instead of the highlighter
|
139
|
+
# string. Each occurrence of a phrase will be passed to the block, and its
|
140
|
+
# return value will be inserted into the final result.
|
141
|
+
#
|
142
|
+
# ==== Options
|
143
|
+
#
|
144
|
+
# [+:highlighter+]
|
145
|
+
# The highlighter string. Uses <tt>\1</tt> as the placeholder for a
|
146
|
+
# phrase, similar to +String#sub+. Defaults to <tt>"<mark>\1</mark>"</tt>.
|
147
|
+
# This option is ignored if a block is specified.
|
148
|
+
#
|
149
|
+
# [+:sanitize+]
|
150
|
+
# Whether to sanitize +text+ before highlighting. Defaults to true.
|
151
|
+
#
|
152
|
+
# ==== Examples
|
116
153
|
#
|
117
154
|
# highlight('You searched for: rails', 'rails')
|
118
|
-
# # => You searched for: <mark>rails</mark>
|
155
|
+
# # => "You searched for: <mark>rails</mark>"
|
119
156
|
#
|
120
157
|
# highlight('You searched for: rails', /for|rails/)
|
121
|
-
# # => You searched <mark>for</mark>: <mark>rails</mark>
|
158
|
+
# # => "You searched <mark>for</mark>: <mark>rails</mark>"
|
122
159
|
#
|
123
160
|
# highlight('You searched for: ruby, rails, dhh', 'actionpack')
|
124
|
-
# # => You searched for: ruby, rails, dhh
|
161
|
+
# # => "You searched for: ruby, rails, dhh"
|
125
162
|
#
|
126
163
|
# highlight('You searched for: rails', ['for', 'rails'], highlighter: '<em>\1</em>')
|
127
|
-
# # => You searched <em>for</em>: <em>rails</em>
|
164
|
+
# # => "You searched <em>for</em>: <em>rails</em>"
|
128
165
|
#
|
129
166
|
# highlight('You searched for: rails', 'rails', highlighter: '<a href="search?q=\1">\1</a>')
|
130
|
-
# # => You searched for: <a href
|
167
|
+
# # => "You searched for: <a href=\"search?q=rails\">rails</a>"
|
131
168
|
#
|
132
169
|
# highlight('You searched for: rails', 'rails') { |match| link_to(search_path(q: match, match)) }
|
133
|
-
# # => You searched for: <a href
|
170
|
+
# # => "You searched for: <a href=\"search?q=rails\">rails</a>"
|
134
171
|
#
|
135
172
|
# highlight('<a href="javascript:alert(\'no!\')">ruby</a> on rails', 'rails', sanitize: false)
|
136
|
-
# # => <a href
|
173
|
+
# # => "<a href=\"javascript:alert('no!')\">ruby</a> on <mark>rails</mark>"
|
137
174
|
def highlight(text, phrases, options = {}, &block)
|
138
175
|
text = sanitize(text) if options.fetch(:sanitize, true)
|
139
176
|
|
@@ -156,30 +193,45 @@ module ActionView
|
|
156
193
|
end.html_safe
|
157
194
|
end
|
158
195
|
|
159
|
-
# Extracts
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
196
|
+
# Extracts the first occurrence of +phrase+ plus surrounding text from
|
197
|
+
# +text+. An omission marker is prepended / appended if the start / end of
|
198
|
+
# the result does not coincide with the start / end of +text+. The result
|
199
|
+
# is always stripped in any case. Returns +nil+ if +phrase+ isn't found.
|
200
|
+
#
|
201
|
+
# ==== Options
|
202
|
+
#
|
203
|
+
# [+:radius+]
|
204
|
+
# The number of characters (or tokens — see +:separator+ option) around
|
205
|
+
# +phrase+ to include in the result. Defaults to 100.
|
206
|
+
#
|
207
|
+
# [+:omission+]
|
208
|
+
# The marker to prepend / append when the start / end of the excerpt
|
209
|
+
# does not coincide with the start / end of +text+. Defaults to
|
210
|
+
# <tt>"..."</tt>.
|
211
|
+
#
|
212
|
+
# [+:separator+]
|
213
|
+
# The separator between tokens to count for +:radius+. Defaults to
|
214
|
+
# <tt>""</tt>, which treats each character as a token.
|
215
|
+
#
|
216
|
+
# ==== Examples
|
165
217
|
#
|
166
218
|
# excerpt('This is an example', 'an', radius: 5)
|
167
|
-
# # => ...s is an exam...
|
219
|
+
# # => "...s is an exam..."
|
168
220
|
#
|
169
221
|
# excerpt('This is an example', 'is', radius: 5)
|
170
|
-
# # => This is a...
|
222
|
+
# # => "This is a..."
|
171
223
|
#
|
172
224
|
# excerpt('This is an example', 'is')
|
173
|
-
# # => This is an example
|
225
|
+
# # => "This is an example"
|
174
226
|
#
|
175
227
|
# excerpt('This next thing is an example', 'ex', radius: 2)
|
176
|
-
# # => ...next...
|
228
|
+
# # => "...next..."
|
177
229
|
#
|
178
230
|
# excerpt('This is also an example', 'an', radius: 8, omission: '<chop> ')
|
179
|
-
# # => <chop> is also an example
|
231
|
+
# # => "<chop> is also an example"
|
180
232
|
#
|
181
233
|
# excerpt('This is a very beautiful morning', 'very', separator: ' ', radius: 1)
|
182
|
-
# # => ...a very beautiful...
|
234
|
+
# # => "...a very beautiful..."
|
183
235
|
def excerpt(text, phrase, options = {})
|
184
236
|
return unless text && phrase
|
185
237
|
|
@@ -215,26 +267,26 @@ module ActionView
|
|
215
267
|
# Attempts to pluralize the +singular+ word unless +count+ is 1. If
|
216
268
|
# +plural+ is supplied, it will use that when count is > 1, otherwise
|
217
269
|
# it will use the Inflector to determine the plural form for the given locale,
|
218
|
-
# which defaults to I18n.locale
|
270
|
+
# which defaults to +I18n.locale+.
|
219
271
|
#
|
220
272
|
# The word will be pluralized using rules defined for the locale
|
221
273
|
# (you must define your own inflection rules for languages other than English).
|
222
274
|
# See ActiveSupport::Inflector.pluralize
|
223
275
|
#
|
224
276
|
# pluralize(1, 'person')
|
225
|
-
# # => 1 person
|
277
|
+
# # => "1 person"
|
226
278
|
#
|
227
279
|
# pluralize(2, 'person')
|
228
|
-
# # => 2 people
|
280
|
+
# # => "2 people"
|
229
281
|
#
|
230
282
|
# pluralize(3, 'person', plural: 'users')
|
231
|
-
# # => 3 users
|
283
|
+
# # => "3 users"
|
232
284
|
#
|
233
285
|
# pluralize(0, 'person')
|
234
|
-
# # => 0 people
|
286
|
+
# # => "0 people"
|
235
287
|
#
|
236
288
|
# pluralize(2, 'Person', locale: :de)
|
237
|
-
# # => 2 Personen
|
289
|
+
# # => "2 Personen"
|
238
290
|
def pluralize(count, singular, plural_arg = nil, plural: plural_arg, locale: I18n.locale)
|
239
291
|
word = if count == 1 || count.to_s.match?(/^1(\.0+)?$/)
|
240
292
|
singular
|
@@ -250,22 +302,24 @@ module ActionView
|
|
250
302
|
# (which is 80 by default).
|
251
303
|
#
|
252
304
|
# word_wrap('Once upon a time')
|
253
|
-
# # => Once upon a time
|
305
|
+
# # => "Once upon a time"
|
254
306
|
#
|
255
307
|
# word_wrap('Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined...')
|
256
|
-
# # => Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\na successor to the throne turned out to be more trouble than anyone could have\nimagined...
|
308
|
+
# # => "Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\na successor to the throne turned out to be more trouble than anyone could have\nimagined..."
|
257
309
|
#
|
258
310
|
# word_wrap('Once upon a time', line_width: 8)
|
259
|
-
# # => Once\nupon a\ntime
|
311
|
+
# # => "Once\nupon a\ntime"
|
260
312
|
#
|
261
313
|
# word_wrap('Once upon a time', line_width: 1)
|
262
|
-
# # => Once\nupon\na\ntime
|
314
|
+
# # => "Once\nupon\na\ntime"
|
263
315
|
#
|
264
|
-
#
|
316
|
+
# You can also specify a custom +break_sequence+ ("\n" by default):
|
265
317
|
#
|
266
318
|
# word_wrap('Once upon a time', line_width: 1, break_sequence: "\r\n")
|
267
|
-
# # => Once\r\nupon\r\na\r\ntime
|
319
|
+
# # => "Once\r\nupon\r\na\r\ntime"
|
268
320
|
def word_wrap(text, line_width: 80, break_sequence: "\n")
|
321
|
+
return +"" if text.empty?
|
322
|
+
|
269
323
|
# Match up to `line_width` characters, followed by one of
|
270
324
|
# (1) non-newline whitespace plus an optional newline
|
271
325
|
# (2) the end of the string, ignoring any trailing newlines
|
@@ -334,7 +388,7 @@ module ActionView
|
|
334
388
|
end
|
335
389
|
end
|
336
390
|
|
337
|
-
# Creates a Cycle object whose
|
391
|
+
# Creates a Cycle object whose +to_s+ method cycles through elements of an
|
338
392
|
# array every time it is called. This can be used for example, to alternate
|
339
393
|
# classes for table rows. You can use named cycles to allow nesting in loops.
|
340
394
|
# Passing a Hash as the last parameter with a <tt>:name</tt> key will create a
|
@@ -343,8 +397,8 @@ module ActionView
|
|
343
397
|
# and passing the name of the cycle. The current cycle string can be obtained
|
344
398
|
# anytime using the current_cycle method.
|
345
399
|
#
|
346
|
-
#
|
347
|
-
# @items = [1,2,3,4]
|
400
|
+
# <%# Alternate CSS classes for even and odd numbers... %>
|
401
|
+
# <% @items = [1,2,3,4] %>
|
348
402
|
# <table>
|
349
403
|
# <% @items.each do |item| %>
|
350
404
|
# <tr class="<%= cycle("odd", "even") -%>">
|
@@ -354,10 +408,12 @@ module ActionView
|
|
354
408
|
# </table>
|
355
409
|
#
|
356
410
|
#
|
357
|
-
#
|
358
|
-
# @items =
|
359
|
-
#
|
360
|
-
#
|
411
|
+
# <%# Cycle CSS classes for rows, and text colors for values within each row %>
|
412
|
+
# <% @items = [
|
413
|
+
# { first: "Robert", middle: "Daniel", last: "James" },
|
414
|
+
# { first: "Emily", middle: "Shannon", maiden: "Pike", last: "Hicks" },
|
415
|
+
# { first: "June", middle: "Dae", last: "Jones" },
|
416
|
+
# ] %>
|
361
417
|
# <% @items.each do |item| %>
|
362
418
|
# <tr class="<%= cycle("odd", "even", name: "row_class") -%>">
|
363
419
|
# <td>
|
@@ -388,8 +444,8 @@ module ActionView
|
|
388
444
|
# for complex table highlighting or any other design need which requires
|
389
445
|
# the current cycle string in more than one place.
|
390
446
|
#
|
391
|
-
#
|
392
|
-
# @items = [1,2,3,4]
|
447
|
+
# <%# Alternate background colors %>
|
448
|
+
# <% @items = [1,2,3,4] %>
|
393
449
|
# <% @items.each do |item| %>
|
394
450
|
# <div style="background-color:<%= cycle("red","white","blue") %>">
|
395
451
|
# <span style="background-color:<%= current_cycle %>"><%= item %></span>
|
@@ -403,8 +459,8 @@ module ActionView
|
|
403
459
|
# Resets a cycle so that it starts from the first element the next time
|
404
460
|
# it is called. Pass in +name+ to reset a named cycle.
|
405
461
|
#
|
406
|
-
#
|
407
|
-
# @items = [[1,2,3,4], [5,6,3], [3,4,5,6,7,4]]
|
462
|
+
# <%# Alternate CSS classes for even and odd numbers... %>
|
463
|
+
# <% @items = [[1,2,3,4], [5,6,3], [3,4,5,6,7,4]] %>
|
408
464
|
# <table>
|
409
465
|
# <% @items.each do |item| %>
|
410
466
|
# <tr class="<%= cycle("even", "odd") -%>">
|
data/lib/action_view/layouts.rb
CHANGED
@@ -152,7 +152,7 @@ module ActionView
|
|
152
152
|
# The template will be looked always in <tt>app/views/layouts/</tt> folder. But you can point
|
153
153
|
# <tt>layouts</tt> folder direct also. <tt>layout "layouts/demo"</tt> is the same as <tt>layout "demo"</tt>.
|
154
154
|
#
|
155
|
-
# Setting the layout to +nil+ forces it to be looked up in the filesystem and
|
155
|
+
# Setting the layout to +nil+ forces it to be looked up in the filesystem and falls back to the parent behavior if none exists.
|
156
156
|
# Setting it to +nil+ is useful to re-enable template lookup overriding a previous configuration set in the parent:
|
157
157
|
#
|
158
158
|
# class ApplicationController < ActionController::Base
|
@@ -164,7 +164,7 @@ module ActionView
|
|
164
164
|
# end
|
165
165
|
#
|
166
166
|
# class CommentsController < ApplicationController
|
167
|
-
# # Will search for "comments" layout and
|
167
|
+
# # Will search for "comments" layout and fall back to "application" layout
|
168
168
|
# layout nil
|
169
169
|
# end
|
170
170
|
#
|
@@ -194,7 +194,7 @@ module ActionView
|
|
194
194
|
|
195
195
|
_template = (cache[path] ||= (template || find_template(path, @locals.keys + [as, counter, iteration])))
|
196
196
|
|
197
|
-
content = _template.render(view, locals)
|
197
|
+
content = _template.render(view, locals, implicit_locals: [counter, iteration])
|
198
198
|
content = layout.render(view, locals) { content } if layout
|
199
199
|
partial_iteration.iterate!
|
200
200
|
build_rendered_template(content, _template)
|
data/lib/action_view/template.rb
CHANGED
@@ -201,6 +201,7 @@ module ActionView
|
|
201
201
|
@variant = variant
|
202
202
|
@compile_mutex = Mutex.new
|
203
203
|
@strict_locals = NONE
|
204
|
+
@strict_local_keys = nil
|
204
205
|
@type = nil
|
205
206
|
end
|
206
207
|
|
@@ -244,9 +245,15 @@ module ActionView
|
|
244
245
|
# This method is instrumented as "!render_template.action_view". Notice that
|
245
246
|
# we use a bang in this instrumentation because you don't want to
|
246
247
|
# consume this in production. This is only slow if it's being listened to.
|
247
|
-
def render(view, locals, buffer = nil, add_to_stack: true, &block)
|
248
|
+
def render(view, locals, buffer = nil, implicit_locals: [], add_to_stack: true, &block)
|
248
249
|
instrument_render_template do
|
249
250
|
compile!(view)
|
251
|
+
|
252
|
+
if strict_locals? && @strict_local_keys && !implicit_locals.empty?
|
253
|
+
locals_to_ignore = implicit_locals - @strict_local_keys
|
254
|
+
locals.except!(*locals_to_ignore)
|
255
|
+
end
|
256
|
+
|
250
257
|
if buffer
|
251
258
|
view._run(method_name, self, locals, buffer, add_to_stack: add_to_stack, has_strict_locals: strict_locals?, &block)
|
252
259
|
nil
|
@@ -283,7 +290,7 @@ module ActionView
|
|
283
290
|
# the same as <tt>Encoding.default_external</tt>.
|
284
291
|
#
|
285
292
|
# The user can also specify the encoding via a comment on the first
|
286
|
-
# line of the template (
|
293
|
+
# line of the template (<tt># encoding: NAME-OF-ENCODING</tt>). This will work
|
287
294
|
# with any template engine, as we process out the encoding comment
|
288
295
|
# before passing the source on to the template engine, leaving a
|
289
296
|
# blank line in its stead.
|
@@ -474,23 +481,30 @@ module ActionView
|
|
474
481
|
|
475
482
|
return unless strict_locals?
|
476
483
|
|
484
|
+
parameters = mod.instance_method(method_name).parameters - [[:req, :output_buffer]]
|
477
485
|
# Check compiled method parameters to ensure that only kwargs
|
478
486
|
# were provided as strict locals, preventing `locals: (foo, *foo)` etc
|
479
487
|
# and allowing `locals: (foo:)`.
|
480
488
|
|
481
|
-
non_kwarg_parameters =
|
482
|
-
|
483
|
-
|
489
|
+
non_kwarg_parameters = parameters.select do |parameter|
|
490
|
+
![:keyreq, :key, :keyrest, :nokey].include?(parameter[0])
|
491
|
+
end
|
484
492
|
|
485
|
-
|
493
|
+
unless non_kwarg_parameters.empty?
|
494
|
+
mod.undef_method(method_name)
|
486
495
|
|
487
|
-
|
496
|
+
raise ArgumentError.new(
|
497
|
+
"#{non_kwarg_parameters.map { |_, name| "`#{name}`" }.to_sentence} set as non-keyword " \
|
498
|
+
"#{'argument'.pluralize(non_kwarg_parameters.length)} for #{short_identifier}. " \
|
499
|
+
"Locals can only be set as keyword arguments."
|
500
|
+
)
|
501
|
+
end
|
488
502
|
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
503
|
+
unless parameters.any? { |type, _| type == :keyrest }
|
504
|
+
parameters.map!(&:last)
|
505
|
+
parameters.sort!
|
506
|
+
@strict_local_keys = parameters.freeze
|
507
|
+
end
|
494
508
|
end
|
495
509
|
|
496
510
|
def offset
|
@@ -82,25 +82,27 @@ module ActionView
|
|
82
82
|
# method, where +[FORMAT]+ corresponds to the value of the
|
83
83
|
# +format+ argument.
|
84
84
|
#
|
85
|
-
#
|
85
|
+
# By default, ActionView::TestCase defines parsers for:
|
86
86
|
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
# value as its only argument.
|
90
|
-
# <tt>block</tt> - Block serves as the parser when the
|
91
|
-
# <tt>callable</tt> is omitted.
|
87
|
+
# * +:html+ - returns an instance of +Nokogiri::XML::Node+
|
88
|
+
# * +:json+ - returns an instance of ActiveSupport::HashWithIndifferentAccess
|
92
89
|
#
|
93
|
-
#
|
90
|
+
# These pre-registered parsers also define corresponding helpers:
|
94
91
|
#
|
95
|
-
#
|
96
|
-
#
|
92
|
+
# * +:html+ - defines +rendered.html+
|
93
|
+
# * +:json+ - defines +rendered.json+
|
97
94
|
#
|
98
|
-
#
|
95
|
+
# ==== Parameters
|
99
96
|
#
|
100
|
-
#
|
101
|
-
#
|
97
|
+
# [+format+]
|
98
|
+
# The name (as a +Symbol+) of the format used to render the content.
|
102
99
|
#
|
103
|
-
#
|
100
|
+
# [+callable+]
|
101
|
+
# The parser. A callable object that accepts the rendered string as
|
102
|
+
# its sole argument. Alternatively, the parser can be specified as a
|
103
|
+
# block.
|
104
|
+
#
|
105
|
+
# ==== Examples
|
104
106
|
#
|
105
107
|
# test "renders HTML" do
|
106
108
|
# article = Article.create!(title: "Hello, world")
|
@@ -118,7 +120,7 @@ module ActionView
|
|
118
120
|
# assert_pattern { rendered.json => { title: "Hello, world" } }
|
119
121
|
# end
|
120
122
|
#
|
121
|
-
# To parse the rendered content into RSS, register a call to
|
123
|
+
# To parse the rendered content into RSS, register a call to +RSS::Parser.parse+:
|
122
124
|
#
|
123
125
|
# register_parser :rss, -> rendered { RSS::Parser.parse(rendered) }
|
124
126
|
#
|
@@ -130,9 +132,8 @@ module ActionView
|
|
130
132
|
# assert_equal "Hello, world", rendered.rss.items.last.title
|
131
133
|
# end
|
132
134
|
#
|
133
|
-
# To parse the rendered content into a Capybara::Simple::Node
|
134
|
-
# re-register an
|
135
|
-
# <tt>Capybara.string</tt>:
|
135
|
+
# To parse the rendered content into a +Capybara::Simple::Node+,
|
136
|
+
# re-register an +:html+ parser with a call to +Capybara.string+:
|
136
137
|
#
|
137
138
|
# register_parser :html, -> rendered { Capybara.string(rendered) }
|
138
139
|
#
|
@@ -143,6 +144,7 @@ module ActionView
|
|
143
144
|
#
|
144
145
|
# rendered.html.assert_css "h1", text: "Hello, world"
|
145
146
|
# end
|
147
|
+
#
|
146
148
|
def register_parser(format, callable = nil, &block)
|
147
149
|
parser = callable || block || :itself.to_proc
|
148
150
|
content_class.redefine_method(format) do
|
@@ -196,7 +198,7 @@ module ActionView
|
|
196
198
|
end
|
197
199
|
|
198
200
|
included do
|
199
|
-
class_attribute :content_class, instance_accessor: false, default:
|
201
|
+
class_attribute :content_class, instance_accessor: false, default: RenderedViewContent
|
200
202
|
|
201
203
|
setup :setup_with_controller
|
202
204
|
|
@@ -297,7 +299,7 @@ module ActionView
|
|
297
299
|
@controller._routes if @controller.respond_to?(:_routes)
|
298
300
|
end
|
299
301
|
|
300
|
-
class
|
302
|
+
class RenderedViewContent < String # :nodoc:
|
301
303
|
end
|
302
304
|
|
303
305
|
# Need to experiment if this priority is the best one: rendered => output_buffer
|