actionview 7.0.8.7 → 7.1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +309 -321
- data/MIT-LICENSE +1 -1
- data/README.rdoc +1 -1
- data/app/assets/javascripts/rails-ujs.esm.js +686 -0
- data/app/assets/javascripts/rails-ujs.js +630 -0
- data/lib/action_view/base.rb +34 -14
- data/lib/action_view/buffers.rb +106 -8
- data/lib/action_view/cache_expiry.rb +40 -43
- data/lib/action_view/context.rb +1 -1
- data/lib/action_view/deprecator.rb +7 -0
- data/lib/action_view/digestor.rb +1 -1
- data/lib/action_view/gem_version.rb +4 -4
- data/lib/action_view/helpers/active_model_helper.rb +1 -1
- data/lib/action_view/helpers/asset_tag_helper.rb +136 -52
- data/lib/action_view/helpers/asset_url_helper.rb +6 -5
- data/lib/action_view/helpers/atom_feed_helper.rb +5 -5
- data/lib/action_view/helpers/cache_helper.rb +7 -13
- data/lib/action_view/helpers/capture_helper.rb +30 -10
- data/lib/action_view/helpers/content_exfiltration_prevention_helper.rb +70 -0
- data/lib/action_view/helpers/controller_helper.rb +6 -0
- data/lib/action_view/helpers/csp_helper.rb +2 -2
- data/lib/action_view/helpers/csrf_helper.rb +2 -2
- data/lib/action_view/helpers/date_helper.rb +17 -19
- data/lib/action_view/helpers/debug_helper.rb +3 -3
- data/lib/action_view/helpers/form_helper.rb +54 -25
- data/lib/action_view/helpers/form_options_helper.rb +2 -1
- data/lib/action_view/helpers/form_tag_helper.rb +49 -15
- data/lib/action_view/helpers/javascript_helper.rb +1 -0
- data/lib/action_view/helpers/number_helper.rb +37 -330
- data/lib/action_view/helpers/output_safety_helper.rb +2 -2
- data/lib/action_view/helpers/rendering_helper.rb +1 -1
- data/lib/action_view/helpers/sanitize_helper.rb +51 -21
- data/lib/action_view/helpers/tag_helper.rb +5 -27
- data/lib/action_view/helpers/tags/base.rb +11 -52
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +1 -0
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +1 -0
- data/lib/action_view/helpers/tags/collection_select.rb +3 -0
- data/lib/action_view/helpers/tags/date_field.rb +1 -1
- data/lib/action_view/helpers/tags/date_select.rb +2 -0
- data/lib/action_view/helpers/tags/datetime_field.rb +14 -6
- data/lib/action_view/helpers/tags/datetime_local_field.rb +11 -2
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +3 -0
- data/lib/action_view/helpers/tags/month_field.rb +1 -1
- data/lib/action_view/helpers/tags/select.rb +3 -0
- data/lib/action_view/helpers/tags/select_renderer.rb +56 -0
- data/lib/action_view/helpers/tags/time_field.rb +1 -1
- data/lib/action_view/helpers/tags/time_zone_select.rb +3 -0
- data/lib/action_view/helpers/tags/week_field.rb +1 -1
- data/lib/action_view/helpers/tags/weekday_select.rb +3 -0
- data/lib/action_view/helpers/tags.rb +2 -0
- data/lib/action_view/helpers/text_helper.rb +156 -84
- data/lib/action_view/helpers/translation_helper.rb +3 -3
- data/lib/action_view/helpers/url_helper.rb +46 -17
- data/lib/action_view/helpers.rb +2 -0
- data/lib/action_view/layouts.rb +8 -6
- data/lib/action_view/log_subscriber.rb +49 -32
- data/lib/action_view/lookup_context.rb +29 -13
- data/lib/action_view/path_registry.rb +57 -0
- data/lib/action_view/path_set.rb +13 -14
- data/lib/action_view/railtie.rb +26 -3
- data/lib/action_view/record_identifier.rb +15 -8
- data/lib/action_view/renderer/abstract_renderer.rb +1 -1
- data/lib/action_view/renderer/collection_renderer.rb +10 -2
- data/lib/action_view/renderer/partial_renderer/collection_caching.rb +2 -1
- data/lib/action_view/renderer/partial_renderer.rb +2 -1
- data/lib/action_view/renderer/renderer.rb +2 -0
- data/lib/action_view/renderer/streaming_template_renderer.rb +3 -2
- data/lib/action_view/renderer/template_renderer.rb +3 -2
- data/lib/action_view/rendering.rb +22 -4
- data/lib/action_view/ripper_ast_parser.rb +5 -5
- data/lib/action_view/template/error.rb +14 -1
- data/lib/action_view/template/handlers/builder.rb +4 -4
- data/lib/action_view/template/handlers/erb/erubi.rb +23 -27
- data/lib/action_view/template/handlers/erb.rb +73 -1
- data/lib/action_view/template/handlers.rb +1 -1
- data/lib/action_view/template/html.rb +1 -1
- data/lib/action_view/template/raw_file.rb +1 -1
- data/lib/action_view/template/renderable.rb +1 -1
- data/lib/action_view/template/resolver.rb +10 -2
- data/lib/action_view/template/text.rb +1 -1
- data/lib/action_view/template/types.rb +25 -34
- data/lib/action_view/template.rb +249 -54
- data/lib/action_view/template_path.rb +2 -0
- data/lib/action_view/test_case.rb +176 -21
- data/lib/action_view/unbound_template.rb +17 -7
- data/lib/action_view/version.rb +1 -1
- data/lib/action_view/view_paths.rb +15 -24
- data/lib/action_view.rb +4 -1
- metadata +22 -23
- data/lib/assets/compiled/rails-ujs.js +0 -777
@@ -5,8 +5,9 @@ require "active_support/core_ext/string/output_safety"
|
|
5
5
|
require "active_support/number_helper"
|
6
6
|
|
7
7
|
module ActionView
|
8
|
-
# = Action View Number Helpers
|
9
8
|
module Helpers # :nodoc:
|
9
|
+
# = Action View Number \Helpers
|
10
|
+
#
|
10
11
|
# Provides methods for converting numbers into formatted strings.
|
11
12
|
# Methods are provided for phone numbers, currency, percentage,
|
12
13
|
# precision, positional notation, file size, and pretty printing.
|
@@ -23,42 +24,14 @@ module ActionView
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
|
-
#
|
27
|
-
# 123-9876). You can customize the format in the +options+ hash.
|
28
|
-
#
|
29
|
-
# ==== Options
|
30
|
-
#
|
31
|
-
# * <tt>:area_code</tt> - Adds parentheses around the area code.
|
32
|
-
# * <tt>:delimiter</tt> - Specifies the delimiter to use
|
33
|
-
# (defaults to "-").
|
34
|
-
# * <tt>:extension</tt> - Specifies an extension to add to the
|
35
|
-
# end of the generated number.
|
36
|
-
# * <tt>:country_code</tt> - Sets the country code for the phone
|
37
|
-
# number.
|
38
|
-
# * <tt>:pattern</tt> - Specifies how the number is divided into three
|
39
|
-
# groups with the custom regexp to override the default format.
|
40
|
-
# * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
|
41
|
-
# the argument is invalid.
|
42
|
-
#
|
43
|
-
# ==== Examples
|
27
|
+
# Delegates to ActiveSupport::NumberHelper#number_to_phone.
|
44
28
|
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
# number_to_phone(1235551234) # => 123-555-1234
|
48
|
-
# number_to_phone(1235551234, area_code: true) # => (123) 555-1234
|
49
|
-
# number_to_phone(1235551234, delimiter: " ") # => 123 555 1234
|
50
|
-
# number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
|
51
|
-
# number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234
|
52
|
-
# number_to_phone("123a456") # => 123a456
|
53
|
-
# number_to_phone("1234a567", raise: true) # => InvalidNumberError
|
29
|
+
# Additionally, supports a +:raise+ option that will cause
|
30
|
+
# InvalidNumberError to be raised if +number+ is not a valid number:
|
54
31
|
#
|
55
|
-
# number_to_phone(
|
56
|
-
# # =>
|
32
|
+
# number_to_phone("12x34") # => "12x34"
|
33
|
+
# number_to_phone("12x34", raise: true) # => InvalidNumberError
|
57
34
|
#
|
58
|
-
# number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
|
59
|
-
# # => "(755) 6123-4567"
|
60
|
-
# number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
|
61
|
-
# # => "133-1234-5678"
|
62
35
|
def number_to_phone(number, options = {})
|
63
36
|
return unless number
|
64
37
|
options = options.symbolize_keys
|
@@ -67,339 +40,73 @@ module ActionView
|
|
67
40
|
ERB::Util.html_escape(ActiveSupport::NumberHelper.number_to_phone(number, options))
|
68
41
|
end
|
69
42
|
|
70
|
-
#
|
71
|
-
# can customize the format in the +options+ hash.
|
72
|
-
#
|
73
|
-
# The currency unit and number formatting of the current locale will be used
|
74
|
-
# unless otherwise specified in the provided options. No currency conversion
|
75
|
-
# is performed. If the user is given a way to change their locale, they will
|
76
|
-
# also be able to change the relative value of the currency displayed with
|
77
|
-
# this helper. If your application will ever support multiple locales, you
|
78
|
-
# may want to specify a constant <tt>:locale</tt> option or consider
|
79
|
-
# using a library capable of currency conversion.
|
80
|
-
#
|
81
|
-
# ==== Options
|
82
|
-
#
|
83
|
-
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
84
|
-
# (defaults to current locale).
|
85
|
-
# * <tt>:precision</tt> - Sets the level of precision (defaults
|
86
|
-
# to 2).
|
87
|
-
# * <tt>:unit</tt> - Sets the denomination of the currency
|
88
|
-
# (defaults to "$").
|
89
|
-
# * <tt>:separator</tt> - Sets the separator between the units
|
90
|
-
# (defaults to ".").
|
91
|
-
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
92
|
-
# to ",").
|
93
|
-
# * <tt>:format</tt> - Sets the format for non-negative numbers
|
94
|
-
# (defaults to "%u%n"). Fields are <tt>%u</tt> for the
|
95
|
-
# currency, and <tt>%n</tt> for the number.
|
96
|
-
# * <tt>:negative_format</tt> - Sets the format for negative
|
97
|
-
# numbers (defaults to prepending a hyphen to the formatted
|
98
|
-
# number given by <tt>:format</tt>). Accepts the same fields
|
99
|
-
# than <tt>:format</tt>, except <tt>%n</tt> is here the
|
100
|
-
# absolute value of the number.
|
101
|
-
# * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
|
102
|
-
# the argument is invalid.
|
103
|
-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
104
|
-
# insignificant zeros after the decimal separator (defaults to
|
105
|
-
# +false+).
|
106
|
-
#
|
107
|
-
# ==== Examples
|
43
|
+
# Delegates to ActiveSupport::NumberHelper#number_to_currency.
|
108
44
|
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
# number_to_currency(1234567890.506, precision: 3) # => $1,234,567,890.506
|
112
|
-
# number_to_currency(1234567890.506, locale: :fr) # => 1 234 567 890,51 €
|
113
|
-
# number_to_currency("123a456") # => $123a456
|
45
|
+
# Additionally, supports a +:raise+ option that will cause
|
46
|
+
# InvalidNumberError to be raised if +number+ is not a valid number:
|
114
47
|
#
|
115
|
-
# number_to_currency("
|
48
|
+
# number_to_currency("12x34") # => "$12x34"
|
49
|
+
# number_to_currency("12x34", raise: true) # => InvalidNumberError
|
116
50
|
#
|
117
|
-
# number_to_currency(-0.456789, precision: 0)
|
118
|
-
# # => "$0"
|
119
|
-
# number_to_currency(-1234567890.50, negative_format: "(%u%n)")
|
120
|
-
# # => ($1,234,567,890.50)
|
121
|
-
# number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "")
|
122
|
-
# # => R$1234567890,50
|
123
|
-
# number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "", format: "%n %u")
|
124
|
-
# # => 1234567890,50 R$
|
125
|
-
# number_to_currency(1234567890.50, strip_insignificant_zeros: true)
|
126
|
-
# # => "$1,234,567,890.5"
|
127
51
|
def number_to_currency(number, options = {})
|
128
52
|
delegate_number_helper_method(:number_to_currency, number, options)
|
129
53
|
end
|
130
54
|
|
131
|
-
#
|
132
|
-
# customize the format in the +options+ hash.
|
55
|
+
# Delegates to ActiveSupport::NumberHelper#number_to_percentage.
|
133
56
|
#
|
134
|
-
#
|
57
|
+
# Additionally, supports a +:raise+ option that will cause
|
58
|
+
# InvalidNumberError to be raised if +number+ is not a valid number:
|
135
59
|
#
|
136
|
-
#
|
137
|
-
# (
|
138
|
-
# * <tt>:precision</tt> - Sets the precision of the number
|
139
|
-
# (defaults to 3).
|
140
|
-
# * <tt>:significant</tt> - If +true+, precision will be the number
|
141
|
-
# of significant_digits. If +false+, the number of fractional
|
142
|
-
# digits (defaults to +false+).
|
143
|
-
# * <tt>:separator</tt> - Sets the separator between the
|
144
|
-
# fractional and integer digits (defaults to ".").
|
145
|
-
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
146
|
-
# to "").
|
147
|
-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
148
|
-
# insignificant zeros after the decimal separator (defaults to
|
149
|
-
# +false+).
|
150
|
-
# * <tt>:format</tt> - Specifies the format of the percentage
|
151
|
-
# string The number field is <tt>%n</tt> (defaults to "%n%").
|
152
|
-
# * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
|
153
|
-
# the argument is invalid.
|
60
|
+
# number_to_percentage("99x") # => "99x%"
|
61
|
+
# number_to_percentage("99x", raise: true) # => InvalidNumberError
|
154
62
|
#
|
155
|
-
# ==== Examples
|
156
|
-
#
|
157
|
-
# number_to_percentage(100) # => 100.000%
|
158
|
-
# number_to_percentage("98") # => 98.000%
|
159
|
-
# number_to_percentage(100, precision: 0) # => 100%
|
160
|
-
# number_to_percentage(1000, delimiter: '.', separator: ',') # => 1.000,000%
|
161
|
-
# number_to_percentage(302.24398923423, precision: 5) # => 302.24399%
|
162
|
-
# number_to_percentage(1000, locale: :fr) # => 1 000,000%
|
163
|
-
# number_to_percentage("98a") # => 98a%
|
164
|
-
# number_to_percentage(100, format: "%n %") # => 100.000 %
|
165
|
-
#
|
166
|
-
# number_to_percentage("98a", raise: true) # => InvalidNumberError
|
167
63
|
def number_to_percentage(number, options = {})
|
168
64
|
delegate_number_helper_method(:number_to_percentage, number, options)
|
169
65
|
end
|
170
66
|
|
171
|
-
#
|
172
|
-
# (e.g., 12,324). You can customize the format in the +options+
|
173
|
-
# hash.
|
174
|
-
#
|
175
|
-
# ==== Options
|
176
|
-
#
|
177
|
-
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
178
|
-
# (defaults to current locale).
|
179
|
-
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
180
|
-
# to ",").
|
181
|
-
# * <tt>:separator</tt> - Sets the separator between the
|
182
|
-
# fractional and integer digits (defaults to ".").
|
183
|
-
# * <tt>:delimiter_pattern</tt> - Sets a custom regular expression used for
|
184
|
-
# deriving the placement of delimiter. Helpful when using currency formats
|
185
|
-
# like INR.
|
186
|
-
# * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
|
187
|
-
# the argument is invalid.
|
67
|
+
# Delegates to ActiveSupport::NumberHelper#number_to_delimited.
|
188
68
|
#
|
189
|
-
#
|
69
|
+
# Additionally, supports a +:raise+ option that will cause
|
70
|
+
# InvalidNumberError to be raised if +number+ is not a valid number:
|
190
71
|
#
|
191
|
-
# number_with_delimiter(
|
192
|
-
# number_with_delimiter("
|
193
|
-
# number_with_delimiter(12345678.05) # => 12,345,678.05
|
194
|
-
# number_with_delimiter(12345678, delimiter: ".") # => 12.345.678
|
195
|
-
# number_with_delimiter(12345678, delimiter: ",") # => 12,345,678
|
196
|
-
# number_with_delimiter(12345678.05, separator: " ") # => 12,345,678 05
|
197
|
-
# number_with_delimiter(12345678.05, locale: :fr) # => 12 345 678,05
|
198
|
-
# number_with_delimiter("112a") # => 112a
|
199
|
-
# number_with_delimiter(98765432.98, delimiter: " ", separator: ",")
|
200
|
-
# # => 98 765 432,98
|
72
|
+
# number_with_delimiter("12x34") # => "12x34"
|
73
|
+
# number_with_delimiter("12x34", raise: true) # => InvalidNumberError
|
201
74
|
#
|
202
|
-
# number_with_delimiter("123456.78",
|
203
|
-
# delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/) # => "1,23,456.78"
|
204
|
-
#
|
205
|
-
# number_with_delimiter("112a", raise: true) # => raise InvalidNumberError
|
206
75
|
def number_with_delimiter(number, options = {})
|
207
76
|
delegate_number_helper_method(:number_to_delimited, number, options)
|
208
77
|
end
|
209
78
|
|
210
|
-
#
|
211
|
-
# <tt>:precision</tt> (e.g., 112.32 has a precision of 2 if
|
212
|
-
# +:significant+ is +false+, and 5 if +:significant+ is +true+).
|
213
|
-
# You can customize the format in the +options+ hash.
|
214
|
-
#
|
215
|
-
# ==== Options
|
216
|
-
#
|
217
|
-
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
218
|
-
# (defaults to current locale).
|
219
|
-
# * <tt>:precision</tt> - Sets the precision of the number
|
220
|
-
# (defaults to 3).
|
221
|
-
# * <tt>:significant</tt> - If +true+, precision will be the number
|
222
|
-
# of significant_digits. If +false+, the number of fractional
|
223
|
-
# digits (defaults to +false+).
|
224
|
-
# * <tt>:separator</tt> - Sets the separator between the
|
225
|
-
# fractional and integer digits (defaults to ".").
|
226
|
-
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
227
|
-
# to "").
|
228
|
-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
229
|
-
# insignificant zeros after the decimal separator (defaults to
|
230
|
-
# +false+).
|
231
|
-
# * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
|
232
|
-
# the argument is invalid.
|
233
|
-
#
|
234
|
-
# ==== Examples
|
79
|
+
# Delegates to ActiveSupport::NumberHelper#number_to_rounded.
|
235
80
|
#
|
236
|
-
#
|
237
|
-
#
|
238
|
-
# number_with_precision(13, precision: 5) # => 13.00000
|
239
|
-
# number_with_precision(389.32314, precision: 0) # => 389
|
240
|
-
# number_with_precision(111.2345, significant: true) # => 111
|
241
|
-
# number_with_precision(111.2345, precision: 1, significant: true) # => 100
|
242
|
-
# number_with_precision(13, precision: 5, significant: true) # => 13.000
|
243
|
-
# number_with_precision(111.234, locale: :fr) # => 111,234
|
81
|
+
# Additionally, supports a +:raise+ option that will cause
|
82
|
+
# InvalidNumberError to be raised if +number+ is not a valid number:
|
244
83
|
#
|
245
|
-
# number_with_precision(
|
246
|
-
# # =>
|
84
|
+
# number_with_precision("12x34") # => "12x34"
|
85
|
+
# number_with_precision("12x34", raise: true) # => InvalidNumberError
|
247
86
|
#
|
248
|
-
# number_with_precision(389.32314, precision: 4, significant: true) # => 389.3
|
249
|
-
# number_with_precision(1111.2345, precision: 2, separator: ',', delimiter: '.')
|
250
|
-
# # => 1.111,23
|
251
87
|
def number_with_precision(number, options = {})
|
252
88
|
delegate_number_helper_method(:number_to_rounded, number, options)
|
253
89
|
end
|
254
90
|
|
255
|
-
#
|
256
|
-
# representation (e.g., giving it 1500 yields 1.46 KB). This
|
257
|
-
# method is useful for reporting file sizes to users. You can
|
258
|
-
# customize the format in the +options+ hash.
|
91
|
+
# Delegates to ActiveSupport::NumberHelper#number_to_human_size.
|
259
92
|
#
|
260
|
-
#
|
261
|
-
#
|
93
|
+
# Additionally, supports a +:raise+ option that will cause
|
94
|
+
# InvalidNumberError to be raised if +number+ is not a valid number:
|
262
95
|
#
|
263
|
-
#
|
96
|
+
# number_to_human_size("12x34") # => "12x34"
|
97
|
+
# number_to_human_size("12x34", raise: true) # => InvalidNumberError
|
264
98
|
#
|
265
|
-
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
266
|
-
# (defaults to current locale).
|
267
|
-
# * <tt>:precision</tt> - Sets the precision of the number
|
268
|
-
# (defaults to 3).
|
269
|
-
# * <tt>:significant</tt> - If +true+, precision will be the number
|
270
|
-
# of significant_digits. If +false+, the number of fractional
|
271
|
-
# digits (defaults to +true+)
|
272
|
-
# * <tt>:separator</tt> - Sets the separator between the
|
273
|
-
# fractional and integer digits (defaults to ".").
|
274
|
-
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
275
|
-
# to "").
|
276
|
-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
277
|
-
# insignificant zeros after the decimal separator (defaults to
|
278
|
-
# +true+)
|
279
|
-
# * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
|
280
|
-
# the argument is invalid.
|
281
|
-
#
|
282
|
-
# ==== Examples
|
283
|
-
#
|
284
|
-
# number_to_human_size(123) # => 123 Bytes
|
285
|
-
# number_to_human_size(1234) # => 1.21 KB
|
286
|
-
# number_to_human_size(12345) # => 12.1 KB
|
287
|
-
# number_to_human_size(1234567) # => 1.18 MB
|
288
|
-
# number_to_human_size(1234567890) # => 1.15 GB
|
289
|
-
# number_to_human_size(1234567890123) # => 1.12 TB
|
290
|
-
# number_to_human_size(1234567890123456) # => 1.1 PB
|
291
|
-
# number_to_human_size(1234567890123456789) # => 1.07 EB
|
292
|
-
# number_to_human_size(1234567, precision: 2) # => 1.2 MB
|
293
|
-
# number_to_human_size(483989, precision: 2) # => 470 KB
|
294
|
-
# number_to_human_size(1234567, precision: 2, separator: ',') # => 1,2 MB
|
295
|
-
# number_to_human_size(1234567890123, precision: 5) # => "1.1228 TB"
|
296
|
-
# number_to_human_size(524288000, precision: 5) # => "500 MB"
|
297
99
|
def number_to_human_size(number, options = {})
|
298
100
|
delegate_number_helper_method(:number_to_human_size, number, options)
|
299
101
|
end
|
300
102
|
|
301
|
-
#
|
302
|
-
# is more readable by humans (e.g.: 1200000000 becomes "1.2
|
303
|
-
# Billion"). This is useful for numbers that can get very large
|
304
|
-
# (and too hard to read).
|
305
|
-
#
|
306
|
-
# See <tt>number_to_human_size</tt> if you want to print a file
|
307
|
-
# size.
|
308
|
-
#
|
309
|
-
# You can also define your own unit-quantifier names if you want
|
310
|
-
# to use other decimal units (e.g.: 1500 becomes "1.5
|
311
|
-
# kilometers", 0.150 becomes "150 milliliters", etc). You may
|
312
|
-
# define a wide range of unit quantifiers, even fractional ones
|
313
|
-
# (centi, deci, mili, etc).
|
314
|
-
#
|
315
|
-
# ==== Options
|
316
|
-
#
|
317
|
-
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
318
|
-
# (defaults to current locale).
|
319
|
-
# * <tt>:precision</tt> - Sets the precision of the number
|
320
|
-
# (defaults to 3).
|
321
|
-
# * <tt>:significant</tt> - If +true+, precision will be the number
|
322
|
-
# of significant_digits. If +false+, the number of fractional
|
323
|
-
# digits (defaults to +true+)
|
324
|
-
# * <tt>:separator</tt> - Sets the separator between the
|
325
|
-
# fractional and integer digits (defaults to ".").
|
326
|
-
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
327
|
-
# to "").
|
328
|
-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
329
|
-
# insignificant zeros after the decimal separator (defaults to
|
330
|
-
# +true+)
|
331
|
-
# * <tt>:units</tt> - A Hash of unit quantifier names. Or a
|
332
|
-
# string containing an i18n scope where to find this hash. It
|
333
|
-
# might have the following keys:
|
334
|
-
# * *integers*: <tt>:unit</tt>, <tt>:ten</tt>,
|
335
|
-
# <tt>:hundred</tt>, <tt>:thousand</tt>, <tt>:million</tt>,
|
336
|
-
# <tt>:billion</tt>, <tt>:trillion</tt>,
|
337
|
-
# <tt>:quadrillion</tt>
|
338
|
-
# * *fractionals*: <tt>:deci</tt>, <tt>:centi</tt>,
|
339
|
-
# <tt>:mili</tt>, <tt>:micro</tt>, <tt>:nano</tt>,
|
340
|
-
# <tt>:pico</tt>, <tt>:femto</tt>
|
341
|
-
# * <tt>:format</tt> - Sets the format of the output string
|
342
|
-
# (defaults to "%n %u"). The field types are:
|
343
|
-
# * %u - The quantifier (ex.: 'thousand')
|
344
|
-
# * %n - The number
|
345
|
-
# * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
|
346
|
-
# the argument is invalid.
|
347
|
-
#
|
348
|
-
# ==== Examples
|
349
|
-
#
|
350
|
-
# number_to_human(123) # => "123"
|
351
|
-
# number_to_human(1234) # => "1.23 Thousand"
|
352
|
-
# number_to_human(12345) # => "12.3 Thousand"
|
353
|
-
# number_to_human(1234567) # => "1.23 Million"
|
354
|
-
# number_to_human(1234567890) # => "1.23 Billion"
|
355
|
-
# number_to_human(1234567890123) # => "1.23 Trillion"
|
356
|
-
# number_to_human(1234567890123456) # => "1.23 Quadrillion"
|
357
|
-
# number_to_human(1234567890123456789) # => "1230 Quadrillion"
|
358
|
-
# number_to_human(489939, precision: 2) # => "490 Thousand"
|
359
|
-
# number_to_human(489939, precision: 4) # => "489.9 Thousand"
|
360
|
-
# number_to_human(1234567, precision: 4,
|
361
|
-
# significant: false) # => "1.2346 Million"
|
362
|
-
# number_to_human(1234567, precision: 1,
|
363
|
-
# separator: ',',
|
364
|
-
# significant: false) # => "1,2 Million"
|
365
|
-
#
|
366
|
-
# number_to_human(500000000, precision: 5) # => "500 Million"
|
367
|
-
# number_to_human(12345012345, significant: false) # => "12.345 Billion"
|
368
|
-
#
|
369
|
-
# Non-significant zeros after the decimal separator are stripped
|
370
|
-
# out by default (set <tt>:strip_insignificant_zeros</tt> to
|
371
|
-
# +false+ to change that):
|
372
|
-
#
|
373
|
-
# number_to_human(12.00001) # => "12"
|
374
|
-
# number_to_human(12.00001, strip_insignificant_zeros: false) # => "12.0"
|
375
|
-
#
|
376
|
-
# ==== Custom Unit Quantifiers
|
377
|
-
#
|
378
|
-
# You can also use your own custom unit quantifiers:
|
379
|
-
#
|
380
|
-
# number_to_human(500000, units: {unit: "ml", thousand: "lt"}) # => "500 lt"
|
381
|
-
#
|
382
|
-
# If in your I18n locale you have:
|
383
|
-
# distance:
|
384
|
-
# centi:
|
385
|
-
# one: "centimeter"
|
386
|
-
# other: "centimeters"
|
387
|
-
# unit:
|
388
|
-
# one: "meter"
|
389
|
-
# other: "meters"
|
390
|
-
# thousand:
|
391
|
-
# one: "kilometer"
|
392
|
-
# other: "kilometers"
|
393
|
-
# billion: "gazillion-distance"
|
103
|
+
# Delegates to ActiveSupport::NumberHelper#number_to_human.
|
394
104
|
#
|
395
|
-
#
|
105
|
+
# Additionally, supports a +:raise+ option that will cause
|
106
|
+
# InvalidNumberError to be raised if +number+ is not a valid number:
|
396
107
|
#
|
397
|
-
# number_to_human(
|
398
|
-
# number_to_human(
|
399
|
-
# number_to_human(54393498000, units: :distance) # => "54.4 gazillion-distance"
|
400
|
-
# number_to_human(343, units: :distance, precision: 1) # => "300 meters"
|
401
|
-
# number_to_human(1, units: :distance) # => "1 meter"
|
402
|
-
# number_to_human(0.34, units: :distance) # => "34 centimeters"
|
108
|
+
# number_to_human("12x34") # => "12x34"
|
109
|
+
# number_to_human("12x34", raise: true) # => InvalidNumberError
|
403
110
|
#
|
404
111
|
def number_to_human(number, options = {})
|
405
112
|
delegate_number_helper_method(:number_to_human, number, options)
|
@@ -3,11 +3,11 @@
|
|
3
3
|
require "active_support/core_ext/string/output_safety"
|
4
4
|
|
5
5
|
module ActionView # :nodoc:
|
6
|
-
# = Action View Raw Output Helper
|
7
6
|
module Helpers # :nodoc:
|
7
|
+
# = Action View Raw Output \Helpers
|
8
8
|
module OutputSafetyHelper
|
9
9
|
# This method outputs without escaping a string. Since escaping tags is
|
10
|
-
# now default, this can be used when you don't want Rails to automatically
|
10
|
+
# now default, this can be used when you don't want \Rails to automatically
|
11
11
|
# escape tags. This is not recommended if the data is coming from the user's
|
12
12
|
# input.
|
13
13
|
#
|
@@ -3,20 +3,23 @@
|
|
3
3
|
require "rails-html-sanitizer"
|
4
4
|
|
5
5
|
module ActionView
|
6
|
-
# = Action View Sanitize Helpers
|
7
6
|
module Helpers # :nodoc:
|
7
|
+
# = Action View Sanitize \Helpers
|
8
|
+
#
|
8
9
|
# The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements.
|
9
10
|
# These helper methods extend Action View making them callable within your template files.
|
10
11
|
module SanitizeHelper
|
12
|
+
mattr_accessor :sanitizer_vendor, default: Rails::HTML4::Sanitizer
|
13
|
+
|
11
14
|
extend ActiveSupport::Concern
|
15
|
+
|
12
16
|
# Sanitizes HTML input, stripping all but known-safe tags and attributes.
|
13
17
|
#
|
14
|
-
# It also strips href/src attributes with unsafe protocols like
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# All special characters will be escaped.
|
18
|
+
# It also strips +href+ / +src+ attributes with unsafe protocols like +javascript:+, while
|
19
|
+
# also protecting against attempts to use Unicode, ASCII, and hex character references to work
|
20
|
+
# around these protocol filters.
|
18
21
|
#
|
19
|
-
# The default sanitizer is Rails::
|
22
|
+
# The default sanitizer is +Rails::HTML5::SafeListSanitizer+. See {Rails HTML
|
20
23
|
# Sanitizers}[https://github.com/rails/rails-html-sanitizer] for more information.
|
21
24
|
#
|
22
25
|
# Custom sanitization rules can also be provided.
|
@@ -26,26 +29,31 @@ module ActionView
|
|
26
29
|
#
|
27
30
|
# ==== Options
|
28
31
|
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
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]
|
32
40
|
# or {Loofah::Scrubber}[https://github.com/flavorjones/loofah] object that
|
33
41
|
# defines custom sanitization rules. A custom scrubber takes precedence over
|
34
42
|
# custom tags and attributes.
|
35
43
|
#
|
36
44
|
# ==== Examples
|
37
45
|
#
|
38
|
-
# Normal use
|
46
|
+
# ===== Normal use
|
39
47
|
#
|
40
48
|
# <%= sanitize @comment.body %>
|
41
49
|
#
|
42
|
-
# Providing custom lists of permitted tags and attributes
|
50
|
+
# ===== Providing custom lists of permitted tags and attributes
|
43
51
|
#
|
44
52
|
# <%= sanitize @comment.body, tags: %w(strong em a), attributes: %w(href) %>
|
45
53
|
#
|
46
|
-
# Providing a custom Rails::
|
54
|
+
# ===== Providing a custom +Rails::HTML+ scrubber
|
47
55
|
#
|
48
|
-
# class CommentScrubber < Rails::
|
56
|
+
# class CommentScrubber < Rails::HTML::PermitScrubber
|
49
57
|
# def initialize
|
50
58
|
# super
|
51
59
|
# self.tags = %w( form script comment blockquote )
|
@@ -57,32 +65,54 @@ module ActionView
|
|
57
65
|
# end
|
58
66
|
# end
|
59
67
|
#
|
68
|
+
# <code></code>
|
69
|
+
#
|
60
70
|
# <%= sanitize @comment.body, scrubber: CommentScrubber.new %>
|
61
71
|
#
|
62
72
|
# See {Rails HTML Sanitizer}[https://github.com/rails/rails-html-sanitizer] for
|
63
|
-
# documentation about Rails::
|
73
|
+
# documentation about +Rails::HTML+ scrubbers.
|
64
74
|
#
|
65
|
-
# Providing a custom Loofah::Scrubber
|
75
|
+
# ===== Providing a custom +Loofah::Scrubber+
|
66
76
|
#
|
67
77
|
# scrubber = Loofah::Scrubber.new do |node|
|
68
78
|
# node.remove if node.name == 'script'
|
69
79
|
# end
|
70
80
|
#
|
81
|
+
# <code></code>
|
82
|
+
#
|
71
83
|
# <%= sanitize @comment.body, scrubber: scrubber %>
|
72
84
|
#
|
73
85
|
# See {Loofah's documentation}[https://github.com/flavorjones/loofah] for more
|
74
|
-
# information about defining custom Loofah::Scrubber objects.
|
86
|
+
# information about defining custom +Loofah::Scrubber+ objects.
|
87
|
+
#
|
88
|
+
# ==== Global Configuration
|
75
89
|
#
|
76
90
|
# To set the default allowed tags or attributes across your application:
|
77
91
|
#
|
78
92
|
# # In config/application.rb
|
79
93
|
# config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a']
|
80
94
|
# config.action_view.sanitized_allowed_attributes = ['href', 'title']
|
95
|
+
#
|
96
|
+
# The default, starting in \Rails 7.1, is to use an HTML5 parser for sanitization (if it is
|
97
|
+
# available, see NOTE below). If you wish to revert back to the previous HTML4 behavior, you
|
98
|
+
# can do so by setting the following in your application configuration:
|
99
|
+
#
|
100
|
+
# # In config/application.rb
|
101
|
+
# config.action_view.sanitizer_vendor = Rails::HTML4::Sanitizer
|
102
|
+
#
|
103
|
+
# Or, if you're upgrading from a previous version of \Rails and wish to opt into the HTML5
|
104
|
+
# behavior:
|
105
|
+
#
|
106
|
+
# # In config/application.rb
|
107
|
+
# config.action_view.sanitizer_vendor = Rails::HTML5::Sanitizer
|
108
|
+
#
|
109
|
+
# NOTE: +Rails::HTML5::Sanitizer+ is not supported on JRuby, so on JRuby platforms \Rails will
|
110
|
+
# fall back to using +Rails::HTML4::Sanitizer+.
|
81
111
|
def sanitize(html, options = {})
|
82
112
|
self.class.safe_list_sanitizer.sanitize(html, options)&.html_safe
|
83
113
|
end
|
84
114
|
|
85
|
-
# 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.
|
86
116
|
def sanitize_css(style)
|
87
117
|
self.class.safe_list_sanitizer.sanitize_css(style)
|
88
118
|
end
|
@@ -125,7 +155,7 @@ module ActionView
|
|
125
155
|
attr_writer :full_sanitizer, :link_sanitizer, :safe_list_sanitizer
|
126
156
|
|
127
157
|
def sanitizer_vendor
|
128
|
-
|
158
|
+
ActionView::Helpers::SanitizeHelper.sanitizer_vendor
|
129
159
|
end
|
130
160
|
|
131
161
|
def sanitized_allowed_tags
|
@@ -136,7 +166,7 @@ module ActionView
|
|
136
166
|
sanitizer_vendor.safe_list_sanitizer.allowed_attributes
|
137
167
|
end
|
138
168
|
|
139
|
-
# Gets the Rails::
|
169
|
+
# Gets the Rails::HTML::FullSanitizer instance used by +strip_tags+. Replace with
|
140
170
|
# any object that responds to +sanitize+.
|
141
171
|
#
|
142
172
|
# class Application < Rails::Application
|
@@ -146,7 +176,7 @@ module ActionView
|
|
146
176
|
@full_sanitizer ||= sanitizer_vendor.full_sanitizer.new
|
147
177
|
end
|
148
178
|
|
149
|
-
# Gets the Rails::
|
179
|
+
# Gets the Rails::HTML::LinkSanitizer instance used by +strip_links+.
|
150
180
|
# Replace with any object that responds to +sanitize+.
|
151
181
|
#
|
152
182
|
# class Application < Rails::Application
|
@@ -156,7 +186,7 @@ module ActionView
|
|
156
186
|
@link_sanitizer ||= sanitizer_vendor.link_sanitizer.new
|
157
187
|
end
|
158
188
|
|
159
|
-
# Gets the Rails::
|
189
|
+
# Gets the Rails::HTML::SafeListSanitizer instance used by sanitize and +sanitize_css+.
|
160
190
|
# Replace with any object that responds to +sanitize+.
|
161
191
|
#
|
162
192
|
# class Application < Rails::Application
|