actionview 4.2.11.3 → 5.2.7.1

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

Potentially problematic release.


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

Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +115 -245
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -6
  5. data/lib/action_view/base.rb +38 -28
  6. data/lib/action_view/buffers.rb +3 -1
  7. data/lib/action_view/context.rb +3 -3
  8. data/lib/action_view/dependency_tracker.rb +54 -20
  9. data/lib/action_view/digestor.rb +94 -83
  10. data/lib/action_view/flows.rb +11 -11
  11. data/lib/action_view/gem_version.rb +5 -3
  12. data/lib/action_view/helpers/active_model_helper.rb +17 -11
  13. data/lib/action_view/helpers/asset_tag_helper.rb +244 -62
  14. data/lib/action_view/helpers/asset_url_helper.rb +170 -67
  15. data/lib/action_view/helpers/atom_feed_helper.rb +19 -17
  16. data/lib/action_view/helpers/cache_helper.rb +105 -42
  17. data/lib/action_view/helpers/capture_helper.rb +16 -13
  18. data/lib/action_view/helpers/controller_helper.rb +15 -4
  19. data/lib/action_view/helpers/csp_helper.rb +24 -0
  20. data/lib/action_view/helpers/csrf_helper.rb +7 -5
  21. data/lib/action_view/helpers/date_helper.rb +170 -112
  22. data/lib/action_view/helpers/debug_helper.rb +7 -6
  23. data/lib/action_view/helpers/form_helper.rb +521 -127
  24. data/lib/action_view/helpers/form_options_helper.rb +109 -63
  25. data/lib/action_view/helpers/form_tag_helper.rb +110 -67
  26. data/lib/action_view/helpers/javascript_helper.rb +27 -12
  27. data/lib/action_view/helpers/number_helper.rb +77 -58
  28. data/lib/action_view/helpers/output_safety_helper.rb +36 -4
  29. data/lib/action_view/helpers/record_tag_helper.rb +14 -99
  30. data/lib/action_view/helpers/rendering_helper.rb +6 -5
  31. data/lib/action_view/helpers/sanitize_helper.rb +20 -15
  32. data/lib/action_view/helpers/tag_helper.rb +229 -73
  33. data/lib/action_view/helpers/tags/base.rb +134 -97
  34. data/lib/action_view/helpers/tags/check_box.rb +20 -18
  35. data/lib/action_view/helpers/tags/checkable.rb +4 -2
  36. data/lib/action_view/helpers/tags/collection_check_boxes.rb +12 -33
  37. data/lib/action_view/helpers/tags/collection_helpers.rb +70 -36
  38. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +6 -11
  39. data/lib/action_view/helpers/tags/collection_select.rb +4 -2
  40. data/lib/action_view/helpers/tags/color_field.rb +3 -1
  41. data/lib/action_view/helpers/tags/date_field.rb +2 -0
  42. data/lib/action_view/helpers/tags/date_select.rb +38 -36
  43. data/lib/action_view/helpers/tags/datetime_field.rb +4 -2
  44. data/lib/action_view/helpers/tags/datetime_local_field.rb +2 -0
  45. data/lib/action_view/helpers/tags/datetime_select.rb +2 -0
  46. data/lib/action_view/helpers/tags/email_field.rb +2 -0
  47. data/lib/action_view/helpers/tags/file_field.rb +2 -0
  48. data/lib/action_view/helpers/tags/grouped_collection_select.rb +4 -2
  49. data/lib/action_view/helpers/tags/hidden_field.rb +2 -0
  50. data/lib/action_view/helpers/tags/label.rb +3 -1
  51. data/lib/action_view/helpers/tags/month_field.rb +2 -0
  52. data/lib/action_view/helpers/tags/number_field.rb +2 -0
  53. data/lib/action_view/helpers/tags/password_field.rb +3 -1
  54. data/lib/action_view/helpers/tags/placeholderable.rb +3 -1
  55. data/lib/action_view/helpers/tags/radio_button.rb +7 -5
  56. data/lib/action_view/helpers/tags/range_field.rb +2 -0
  57. data/lib/action_view/helpers/tags/search_field.rb +14 -9
  58. data/lib/action_view/helpers/tags/select.rb +11 -9
  59. data/lib/action_view/helpers/tags/tel_field.rb +2 -0
  60. data/lib/action_view/helpers/tags/text_area.rb +4 -2
  61. data/lib/action_view/helpers/tags/text_field.rb +8 -7
  62. data/lib/action_view/helpers/tags/time_field.rb +2 -0
  63. data/lib/action_view/helpers/tags/time_select.rb +2 -0
  64. data/lib/action_view/helpers/tags/time_zone_select.rb +3 -1
  65. data/lib/action_view/helpers/tags/translator.rb +17 -13
  66. data/lib/action_view/helpers/tags/url_field.rb +2 -0
  67. data/lib/action_view/helpers/tags/week_field.rb +2 -0
  68. data/lib/action_view/helpers/tags.rb +3 -1
  69. data/lib/action_view/helpers/text_helper.rb +55 -36
  70. data/lib/action_view/helpers/translation_helper.rb +74 -32
  71. data/lib/action_view/helpers/url_helper.rb +159 -104
  72. data/lib/action_view/helpers.rb +5 -1
  73. data/lib/action_view/layouts.rb +65 -58
  74. data/lib/action_view/log_subscriber.rb +60 -8
  75. data/lib/action_view/lookup_context.rb +80 -65
  76. data/lib/action_view/model_naming.rb +3 -1
  77. data/lib/action_view/path_set.rb +30 -19
  78. data/lib/action_view/railtie.rb +39 -6
  79. data/lib/action_view/record_identifier.rb +53 -25
  80. data/lib/action_view/renderer/abstract_renderer.rb +21 -15
  81. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +57 -0
  82. data/lib/action_view/renderer/partial_renderer.rb +218 -214
  83. data/lib/action_view/renderer/renderer.rb +8 -6
  84. data/lib/action_view/renderer/streaming_template_renderer.rb +50 -48
  85. data/lib/action_view/renderer/template_renderer.rb +67 -66
  86. data/lib/action_view/rendering.rb +19 -14
  87. data/lib/action_view/routing_url_for.rb +27 -17
  88. data/lib/action_view/tasks/cache_digests.rake +25 -0
  89. data/lib/action_view/template/error.rb +16 -16
  90. data/lib/action_view/template/handlers/builder.rb +10 -11
  91. data/lib/action_view/template/handlers/erb/erubi.rb +83 -0
  92. data/lib/action_view/template/handlers/erb.rb +9 -80
  93. data/lib/action_view/template/handlers/html.rb +11 -0
  94. data/lib/action_view/template/handlers/raw.rb +3 -3
  95. data/lib/action_view/template/handlers.rb +11 -7
  96. data/lib/action_view/template/html.rb +5 -5
  97. data/lib/action_view/template/resolver.rb +140 -115
  98. data/lib/action_view/template/text.rb +8 -9
  99. data/lib/action_view/template/types.rb +18 -18
  100. data/lib/action_view/template.rb +54 -33
  101. data/lib/action_view/test_case.rb +50 -29
  102. data/lib/action_view/testing/resolvers.rb +31 -31
  103. data/lib/action_view/version.rb +3 -1
  104. data/lib/action_view/view_paths.rb +28 -34
  105. data/lib/action_view.rb +8 -7
  106. data/lib/assets/compiled/rails-ujs.js +720 -0
  107. metadata +28 -27
  108. data/lib/action_view/tasks/dependencies.rake +0 -23
@@ -1,7 +1,11 @@
1
- require 'active_support/core_ext/array/extract_options'
2
- require 'active_support/core_ext/hash/keys'
3
- require 'action_view/helpers/asset_url_helper'
4
- require 'action_view/helpers/tag_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/array/extract_options"
4
+ require "active_support/core_ext/hash/keys"
5
+ require "active_support/core_ext/object/inclusion"
6
+ require "active_support/core_ext/object/try"
7
+ require "action_view/helpers/asset_url_helper"
8
+ require "action_view/helpers/tag_helper"
5
9
 
6
10
  module ActionView
7
11
  # = Action View Asset Tag Helpers
@@ -11,7 +15,7 @@ module ActionView
11
15
  # the assets exist before linking to them:
12
16
  #
13
17
  # image_tag("rails.png")
14
- # # => <img alt="Rails" src="/assets/rails.png" />
18
+ # # => <img src="/assets/rails.png" />
15
19
  # stylesheet_link_tag("application")
16
20
  # # => <link href="/assets/application.css?body=1" media="screen" rel="stylesheet" />
17
21
  module AssetTagHelper
@@ -35,33 +39,71 @@ module ActionView
35
39
  # When the Asset Pipeline is enabled, you can pass the name of your manifest as
36
40
  # source, and include other JavaScript or CoffeeScript files inside the manifest.
37
41
  #
42
+ # If the server supports Early Hints header links for these assets will be
43
+ # automatically pushed.
44
+ #
45
+ # ==== Options
46
+ #
47
+ # When the last parameter is a hash you can add HTML attributes using that
48
+ # parameter. The following options are supported:
49
+ #
50
+ # * <tt>:extname</tt> - Append an extension to the generated URL unless the extension
51
+ # already exists. This only applies for relative URLs.
52
+ # * <tt>:protocol</tt> - Sets the protocol of the generated URL. This option only
53
+ # applies when a relative URL and +host+ options are provided.
54
+ # * <tt>:host</tt> - When a relative URL is provided the host is added to the
55
+ # that path.
56
+ # * <tt>:skip_pipeline</tt> - This option is used to bypass the asset pipeline
57
+ # when it is set to true.
58
+ # * <tt>:nonce<tt> - When set to true, adds an automatic nonce value if
59
+ # you have Content Security Policy enabled.
60
+ #
61
+ # ==== Examples
62
+ #
38
63
  # javascript_include_tag "xmlhr"
39
- # # => <script src="/assets/xmlhr.js?1284139606"></script>
64
+ # # => <script src="/assets/xmlhr.debug-1284139606.js"></script>
65
+ #
66
+ # javascript_include_tag "xmlhr", host: "localhost", protocol: "https"
67
+ # # => <script src="https://localhost/assets/xmlhr.debug-1284139606.js"></script>
40
68
  #
41
69
  # javascript_include_tag "template.jst", extname: false
42
- # # => <script src="/assets/template.jst?1284139606"></script>
70
+ # # => <script src="/assets/template.debug-1284139606.jst"></script>
43
71
  #
44
72
  # javascript_include_tag "xmlhr.js"
45
- # # => <script src="/assets/xmlhr.js?1284139606"></script>
73
+ # # => <script src="/assets/xmlhr.debug-1284139606.js"></script>
46
74
  #
47
75
  # javascript_include_tag "common.javascript", "/elsewhere/cools"
48
- # # => <script src="/assets/common.javascript?1284139606"></script>
49
- # # <script src="/elsewhere/cools.js?1423139606"></script>
76
+ # # => <script src="/assets/common.javascript.debug-1284139606.js"></script>
77
+ # # <script src="/elsewhere/cools.debug-1284139606.js"></script>
50
78
  #
51
79
  # javascript_include_tag "http://www.example.com/xmlhr"
52
80
  # # => <script src="http://www.example.com/xmlhr"></script>
53
81
  #
54
82
  # javascript_include_tag "http://www.example.com/xmlhr.js"
55
83
  # # => <script src="http://www.example.com/xmlhr.js"></script>
84
+ #
85
+ # javascript_include_tag "http://www.example.com/xmlhr.js", nonce: true
86
+ # # => <script src="http://www.example.com/xmlhr.js" nonce="..."></script>
56
87
  def javascript_include_tag(*sources)
57
88
  options = sources.extract_options!.stringify_keys
58
- path_options = options.extract!('protocol', 'extname').symbolize_keys
59
- sources.uniq.map { |source|
89
+ path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
90
+ early_hints_links = []
91
+
92
+ sources_tags = sources.uniq.map { |source|
93
+ href = path_to_javascript(source, path_options)
94
+ early_hints_links << "<#{href}>; rel=preload; as=script"
60
95
  tag_options = {
61
- "src" => path_to_javascript(source, path_options)
96
+ "src" => href
62
97
  }.merge!(options)
63
- content_tag(:script, "", tag_options)
98
+ if tag_options["nonce"] == true
99
+ tag_options["nonce"] = content_security_policy_nonce
100
+ end
101
+ content_tag("script".freeze, "", tag_options)
64
102
  }.join("\n").html_safe
103
+
104
+ request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request
105
+
106
+ sources_tags
65
107
  end
66
108
 
67
109
  # Returns a stylesheet link tag for the sources specified as arguments. If
@@ -71,6 +113,9 @@ module ActionView
71
113
  # to "screen", so you must explicitly set it to "all" for the stylesheet(s) to
72
114
  # apply to all media types.
73
115
  #
116
+ # If the server supports Early Hints header links for these assets will be
117
+ # automatically pushed.
118
+ #
74
119
  # stylesheet_link_tag "style"
75
120
  # # => <link href="/assets/style.css" media="screen" rel="stylesheet" />
76
121
  #
@@ -91,22 +136,29 @@ module ActionView
91
136
  # # <link href="/css/stylish.css" media="screen" rel="stylesheet" />
92
137
  def stylesheet_link_tag(*sources)
93
138
  options = sources.extract_options!.stringify_keys
94
- path_options = options.extract!('protocol').symbolize_keys
139
+ path_options = options.extract!("protocol", "host", "skip_pipeline").symbolize_keys
140
+ early_hints_links = []
95
141
 
96
- sources.uniq.map { |source|
142
+ sources_tags = sources.uniq.map { |source|
143
+ href = path_to_stylesheet(source, path_options)
144
+ early_hints_links << "<#{href}>; rel=preload; as=style"
97
145
  tag_options = {
98
146
  "rel" => "stylesheet",
99
147
  "media" => "screen",
100
- "href" => path_to_stylesheet(source, path_options)
148
+ "href" => href
101
149
  }.merge!(options)
102
150
  tag(:link, tag_options)
103
151
  }.join("\n").html_safe
152
+
153
+ request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request
154
+
155
+ sources_tags
104
156
  end
105
157
 
106
158
  # Returns a link tag that browsers and feed readers can use to auto-detect
107
- # an RSS or Atom feed. The +type+ can either be <tt>:rss</tt> (default) or
108
- # <tt>:atom</tt>. Control the link options in url_for format using the
109
- # +url_options+. You can modify the LINK tag itself in +tag_options+.
159
+ # an RSS, Atom, or JSON feed. The +type+ can be <tt>:rss</tt> (default),
160
+ # <tt>:atom</tt>, or <tt>:json</tt>. Control the link options in url_for format
161
+ # using the +url_options+. You can modify the LINK tag itself in +tag_options+.
110
162
  #
111
163
  # ==== Options
112
164
  #
@@ -120,6 +172,8 @@ module ActionView
120
172
  # # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/action" />
121
173
  # auto_discovery_link_tag(:atom)
122
174
  # # => <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.currenthost.com/controller/action" />
175
+ # auto_discovery_link_tag(:json)
176
+ # # => <link rel="alternate" type="application/json" title="JSON" href="http://www.currenthost.com/controller/action" />
123
177
  # auto_discovery_link_tag(:rss, {action: "feed"})
124
178
  # # => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/feed" />
125
179
  # auto_discovery_link_tag(:rss, {action: "feed"}, {title: "My RSS"})
@@ -129,16 +183,16 @@ module ActionView
129
183
  # auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {title: "Example RSS"})
130
184
  # # => <link rel="alternate" type="application/rss+xml" title="Example RSS" href="http://www.example.com/feed.rss" />
131
185
  def auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {})
132
- if !(type == :rss || type == :atom) && tag_options[:type].blank?
133
- raise ArgumentError.new("You should pass :type tag_option key explicitly, because you have passed #{type} type other than :rss or :atom.")
186
+ if !(type == :rss || type == :atom || type == :json) && tag_options[:type].blank?
187
+ raise ArgumentError.new("You should pass :type tag_option key explicitly, because you have passed #{type} type other than :rss, :atom, or :json.")
134
188
  end
135
189
 
136
190
  tag(
137
191
  "link",
138
192
  "rel" => tag_options[:rel] || "alternate",
139
- "type" => tag_options[:type] || Mime::Type.lookup_by_extension(type.to_s).to_s,
193
+ "type" => tag_options[:type] || Template::Types[type].to_s,
140
194
  "title" => tag_options[:title] || type.to_s.upcase,
141
- "href" => url_options.is_a?(Hash) ? url_for(url_options.merge(:only_path => false)) : url_options
195
+ "href" => url_options.is_a?(Hash) ? url_for(url_options.merge(only_path: false)) : url_options
142
196
  )
143
197
  end
144
198
 
@@ -169,49 +223,132 @@ module ActionView
169
223
  #
170
224
  # favicon_link_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png'
171
225
  # # => <link href="/assets/mb-icon.png" rel="apple-touch-icon" type="image/png" />
172
- def favicon_link_tag(source='favicon.ico', options={})
173
- tag('link', {
174
- :rel => 'shortcut icon',
175
- :type => 'image/x-icon',
176
- :href => path_to_image(source)
226
+ def favicon_link_tag(source = "favicon.ico", options = {})
227
+ tag("link", {
228
+ rel: "shortcut icon",
229
+ type: "image/x-icon",
230
+ href: path_to_image(source, skip_pipeline: options.delete(:skip_pipeline))
231
+ }.merge!(options.symbolize_keys))
232
+ end
233
+
234
+ # Returns a link tag that browsers can use to preload the +source+.
235
+ # The +source+ can be the path of a resource managed by asset pipeline,
236
+ # a full path, or an URI.
237
+ #
238
+ # ==== Options
239
+ #
240
+ # * <tt>:type</tt> - Override the auto-generated mime type, defaults to the mime type for +source+ extension.
241
+ # * <tt>:as</tt> - Override the auto-generated value for as attribute, calculated using +source+ extension and mime type.
242
+ # * <tt>:crossorigin</tt> - Specify the crossorigin attribute, required to load cross-origin resources.
243
+ # * <tt>:nopush</tt> - Specify if the use of server push is not desired for the resource. Defaults to +false+.
244
+ #
245
+ # ==== Examples
246
+ #
247
+ # preload_link_tag("custom_theme.css")
248
+ # # => <link rel="preload" href="/assets/custom_theme.css" as="style" type="text/css" />
249
+ #
250
+ # preload_link_tag("/videos/video.webm")
251
+ # # => <link rel="preload" href="/videos/video.mp4" as="video" type="video/webm" />
252
+ #
253
+ # preload_link_tag(post_path(format: :json), as: "fetch")
254
+ # # => <link rel="preload" href="/posts.json" as="fetch" type="application/json" />
255
+ #
256
+ # preload_link_tag("worker.js", as: "worker")
257
+ # # => <link rel="preload" href="/assets/worker.js" as="worker" type="text/javascript" />
258
+ #
259
+ # preload_link_tag("//example.com/font.woff2")
260
+ # # => <link rel="preload" href="//example.com/font.woff2" as="font" type="font/woff2" crossorigin="anonymous"/>
261
+ #
262
+ # preload_link_tag("//example.com/font.woff2", crossorigin: "use-credentials")
263
+ # # => <link rel="preload" href="//example.com/font.woff2" as="font" type="font/woff2" crossorigin="use-credentials" />
264
+ #
265
+ # preload_link_tag("/media/audio.ogg", nopush: true)
266
+ # # => <link rel="preload" href="/media/audio.ogg" as="audio" type="audio/ogg" />
267
+ #
268
+ def preload_link_tag(source, options = {})
269
+ href = asset_path(source, skip_pipeline: options.delete(:skip_pipeline))
270
+ extname = File.extname(source).downcase.delete(".")
271
+ mime_type = options.delete(:type) || Template::Types[extname].try(:to_s)
272
+ as_type = options.delete(:as) || resolve_link_as(extname, mime_type)
273
+ crossorigin = options.delete(:crossorigin)
274
+ crossorigin = "anonymous" if crossorigin == true || (crossorigin.blank? && as_type == "font")
275
+ nopush = options.delete(:nopush) || false
276
+
277
+ link_tag = tag.link({
278
+ rel: "preload",
279
+ href: href,
280
+ as: as_type,
281
+ type: mime_type,
282
+ crossorigin: crossorigin
177
283
  }.merge!(options.symbolize_keys))
284
+
285
+ early_hints_link = "<#{href}>; rel=preload; as=#{as_type}"
286
+ early_hints_link += "; type=#{mime_type}" if mime_type
287
+ early_hints_link += "; crossorigin=#{crossorigin}" if crossorigin
288
+ early_hints_link += "; nopush" if nopush
289
+
290
+ request.send_early_hints("Link" => early_hints_link) if respond_to?(:request) && request
291
+
292
+ link_tag
178
293
  end
179
294
 
180
295
  # Returns an HTML image tag for the +source+. The +source+ can be a full
181
- # path or a file.
296
+ # path, a file, or an Active Storage attachment.
182
297
  #
183
298
  # ==== Options
184
299
  #
185
300
  # You can add HTML attributes using the +options+. The +options+ supports
186
- # two additional keys for convenience and conformance:
301
+ # additional keys for convenience and conformance:
187
302
  #
188
- # * <tt>:alt</tt> - If no alt text is given, the file name part of the
189
- # +source+ is used (capitalized and without the extension)
190
303
  # * <tt>:size</tt> - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes
191
304
  # width="30" and height="45", and "50" becomes width="50" and height="50".
192
305
  # <tt>:size</tt> will be ignored if the value is not in the correct format.
306
+ # * <tt>:srcset</tt> - If supplied as a hash or array of <tt>[source, descriptor]</tt>
307
+ # pairs, each image path will be expanded before the list is formatted as a string.
193
308
  #
194
309
  # ==== Examples
195
310
  #
311
+ # Assets (images that are part of your app):
312
+ #
196
313
  # image_tag("icon")
197
- # # => <img alt="Icon" src="/assets/icon" />
314
+ # # => <img src="/assets/icon" />
198
315
  # image_tag("icon.png")
199
- # # => <img alt="Icon" src="/assets/icon.png" />
316
+ # # => <img src="/assets/icon.png" />
200
317
  # image_tag("icon.png", size: "16x10", alt: "Edit Entry")
201
318
  # # => <img src="/assets/icon.png" width="16" height="10" alt="Edit Entry" />
202
319
  # image_tag("/icons/icon.gif", size: "16")
203
- # # => <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
320
+ # # => <img src="/icons/icon.gif" width="16" height="16" />
204
321
  # image_tag("/icons/icon.gif", height: '32', width: '32')
205
- # # => <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
322
+ # # => <img height="32" src="/icons/icon.gif" width="32" />
206
323
  # image_tag("/icons/icon.gif", class: "menu_icon")
207
- # # => <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
208
- def image_tag(source, options={})
324
+ # # => <img class="menu_icon" src="/icons/icon.gif" />
325
+ # image_tag("/icons/icon.gif", data: { title: 'Rails Application' })
326
+ # # => <img data-title="Rails Application" src="/icons/icon.gif" />
327
+ # image_tag("icon.png", srcset: { "icon_2x.png" => "2x", "icon_4x.png" => "4x" })
328
+ # # => <img src="/assets/icon.png" srcset="/assets/icon_2x.png 2x, /assets/icon_4x.png 4x">
329
+ # image_tag("pic.jpg", srcset: [["pic_1024.jpg", "1024w"], ["pic_1980.jpg", "1980w"]], sizes: "100vw")
330
+ # # => <img src="/assets/pic.jpg" srcset="/assets/pic_1024.jpg 1024w, /assets/pic_1980.jpg 1980w" sizes="100vw">
331
+ #
332
+ # Active Storage (images that are uploaded by the users of your app):
333
+ #
334
+ # image_tag(user.avatar)
335
+ # # => <img src="/rails/active_storage/blobs/.../tiger.jpg" />
336
+ # image_tag(user.avatar.variant(resize: "100x100"))
337
+ # # => <img src="/rails/active_storage/variants/.../tiger.jpg" />
338
+ # image_tag(user.avatar.variant(resize: "100x100"), size: '100')
339
+ # # => <img width="100" height="100" src="/rails/active_storage/variants/.../tiger.jpg" />
340
+ def image_tag(source, options = {})
209
341
  options = options.symbolize_keys
342
+ check_for_image_tag_errors(options)
343
+ skip_pipeline = options.delete(:skip_pipeline)
210
344
 
211
- src = options[:src] = path_to_image(source)
345
+ options[:src] = resolve_image_source(source, skip_pipeline)
212
346
 
213
- unless src =~ /^(?:cid|data):/ || src.blank?
214
- options[:alt] = options.fetch(:alt){ image_alt(src) }
347
+ if options[:srcset] && !options[:srcset].is_a?(String)
348
+ options[:srcset] = options[:srcset].map do |src_path, size|
349
+ src_path = path_to_image(src_path, skip_pipeline: skip_pipeline)
350
+ "#{src_path} #{size}"
351
+ end.join(", ")
215
352
  end
216
353
 
217
354
  options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size]
@@ -236,24 +373,29 @@ module ActionView
236
373
  # image_alt('underscored_file_name.png')
237
374
  # # => Underscored file name
238
375
  def image_alt(src)
239
- File.basename(src, '.*').sub(/-[[:xdigit:]]{32,64}\z/, '').tr('-_', ' ').capitalize
376
+ ActiveSupport::Deprecation.warn("image_alt is deprecated and will be removed from Rails 6.0. You must explicitly set alt text on images.")
377
+
378
+ File.basename(src, ".*".freeze).sub(/-[[:xdigit:]]{32,64}\z/, "".freeze).tr("-_".freeze, " ".freeze).capitalize
240
379
  end
241
380
 
242
381
  # Returns an HTML video tag for the +sources+. If +sources+ is a string,
243
382
  # a single video tag will be returned. If +sources+ is an array, a video
244
383
  # tag with nested source tags for each source will be returned. The
245
- # +sources+ can be full paths or files that exists in your public videos
384
+ # +sources+ can be full paths or files that exist in your public videos
246
385
  # directory.
247
386
  #
248
387
  # ==== Options
249
- # You can add HTML attributes using the +options+. The +options+ supports
250
- # two additional keys for convenience and conformance:
388
+ #
389
+ # When the last parameter is a hash you can add HTML attributes using that
390
+ # parameter. The following options are supported:
251
391
  #
252
392
  # * <tt>:poster</tt> - Set an image (like a screenshot) to be shown
253
393
  # before the video loads. The path is calculated like the +src+ of +image_tag+.
254
394
  # * <tt>:size</tt> - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes
255
395
  # width="30" and height="45", and "50" becomes width="50" and height="50".
256
396
  # <tt>:size</tt> will be ignored if the value is not in the correct format.
397
+ # * <tt>:poster_skip_pipeline</tt> will bypass the asset pipeline when using
398
+ # the <tt>:poster</tt> option instead using an asset in the public folder.
257
399
  #
258
400
  # ==== Examples
259
401
  #
@@ -261,10 +403,12 @@ module ActionView
261
403
  # # => <video src="/videos/trailer"></video>
262
404
  # video_tag("trailer.ogg")
263
405
  # # => <video src="/videos/trailer.ogg"></video>
264
- # video_tag("trailer.ogg", controls: true, autobuffer: true)
265
- # # => <video autobuffer="autobuffer" controls="controls" src="/videos/trailer.ogg" ></video>
406
+ # video_tag("trailer.ogg", controls: true, preload: 'none')
407
+ # # => <video preload="none" controls="controls" src="/videos/trailer.ogg"></video>
266
408
  # video_tag("trailer.m4v", size: "16x10", poster: "screenshot.png")
267
409
  # # => <video src="/videos/trailer.m4v" width="16" height="10" poster="/assets/screenshot.png"></video>
410
+ # video_tag("trailer.m4v", size: "16x10", poster: "screenshot.png", poster_skip_pipeline: true)
411
+ # # => <video src="/videos/trailer.m4v" width="16" height="10" poster="screenshot.png"></video>
268
412
  # video_tag("/trailers/hd.avi", size: "16x16")
269
413
  # # => <video src="/trailers/hd.avi" width="16" height="16"></video>
270
414
  # video_tag("/trailers/hd.avi", size: "16")
@@ -278,15 +422,23 @@ module ActionView
278
422
  # video_tag(["trailer.ogg", "trailer.flv"], size: "160x120")
279
423
  # # => <video height="120" width="160"><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
280
424
  def video_tag(*sources)
281
- multiple_sources_tag('video', sources) do |options|
282
- options[:poster] = path_to_image(options[:poster]) if options[:poster]
283
- options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size]
425
+ options = sources.extract_options!.symbolize_keys
426
+ public_poster_folder = options.delete(:poster_skip_pipeline)
427
+ sources << options
428
+ multiple_sources_tag_builder("video", sources) do |tag_options|
429
+ tag_options[:poster] = path_to_image(tag_options[:poster], skip_pipeline: public_poster_folder) if tag_options[:poster]
430
+ tag_options[:width], tag_options[:height] = extract_dimensions(tag_options.delete(:size)) if tag_options[:size]
284
431
  end
285
432
  end
286
433
 
287
- # Returns an HTML audio tag for the +source+.
288
- # The +source+ can be full path or file that exists in
289
- # your public audios directory.
434
+ # Returns an HTML audio tag for the +sources+. If +sources+ is a string,
435
+ # a single audio tag will be returned. If +sources+ is an array, an audio
436
+ # tag with nested source tags for each source will be returned. The
437
+ # +sources+ can be full paths or files that exist in your public audios
438
+ # directory.
439
+ #
440
+ # When the last parameter is a hash you can add HTML attributes using that
441
+ # parameter.
290
442
  #
291
443
  # audio_tag("sound")
292
444
  # # => <audio src="/audios/sound"></audio>
@@ -297,33 +449,63 @@ module ActionView
297
449
  # audio_tag("sound.wav", "sound.mid")
298
450
  # # => <audio><source src="/audios/sound.wav" /><source src="/audios/sound.mid" /></audio>
299
451
  def audio_tag(*sources)
300
- multiple_sources_tag('audio', sources)
452
+ multiple_sources_tag_builder("audio", sources)
301
453
  end
302
454
 
303
455
  private
304
- def multiple_sources_tag(type, sources)
305
- options = sources.extract_options!.symbolize_keys
456
+ def multiple_sources_tag_builder(type, sources)
457
+ options = sources.extract_options!.symbolize_keys
458
+ skip_pipeline = options.delete(:skip_pipeline)
306
459
  sources.flatten!
307
460
 
308
461
  yield options if block_given?
309
462
 
310
463
  if sources.size > 1
311
464
  content_tag(type, options) do
312
- safe_join sources.map { |source| tag("source", :src => send("path_to_#{type}", source)) }
465
+ safe_join sources.map { |source| tag("source", src: send("path_to_#{type}", source, skip_pipeline: skip_pipeline)) }
313
466
  end
314
467
  else
315
- options[:src] = send("path_to_#{type}", sources.first)
468
+ options[:src] = send("path_to_#{type}", sources.first, skip_pipeline: skip_pipeline)
316
469
  content_tag(type, nil, options)
317
470
  end
318
471
  end
319
472
 
473
+ def resolve_image_source(source, skip_pipeline)
474
+ if source.is_a?(Symbol) || source.is_a?(String)
475
+ path_to_image(source, skip_pipeline: skip_pipeline)
476
+ else
477
+ polymorphic_url(source)
478
+ end
479
+ rescue NoMethodError => e
480
+ raise ArgumentError, "Can't resolve image into URL: #{e}"
481
+ end
482
+
320
483
  def extract_dimensions(size)
321
- if size =~ %r{\A\d+x\d+\z}
322
- size.split('x')
323
- elsif size =~ %r{\A\d+\z}
484
+ size = size.to_s
485
+ if /\A\d+x\d+\z/.match?(size)
486
+ size.split("x")
487
+ elsif /\A\d+\z/.match?(size)
324
488
  [size, size]
325
489
  end
326
490
  end
491
+
492
+ def check_for_image_tag_errors(options)
493
+ if options[:size] && (options[:height] || options[:width])
494
+ raise ArgumentError, "Cannot pass a :size option with a :height or :width option"
495
+ end
496
+ end
497
+
498
+ def resolve_link_as(extname, mime_type)
499
+ if extname == "js"
500
+ "script"
501
+ elsif extname == "css"
502
+ "style"
503
+ elsif extname == "vtt"
504
+ "track"
505
+ elsif (type = mime_type.to_s.split("/")[0]) && type.in?(%w(audio video font))
506
+ type
507
+ end
508
+ end
327
509
  end
328
510
  end
329
511
  end