actionview 4.2.11.1 → 6.0.4
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +201 -192
- data/MIT-LICENSE +1 -1
- data/README.rdoc +9 -8
- data/lib/action_view/base.rb +144 -37
- data/lib/action_view/buffers.rb +18 -1
- data/lib/action_view/cache_expiry.rb +53 -0
- data/lib/action_view/context.rb +8 -12
- data/lib/action_view/dependency_tracker.rb +54 -20
- data/lib/action_view/digestor.rb +88 -85
- data/lib/action_view/flows.rb +11 -12
- data/lib/action_view/gem_version.rb +6 -4
- data/lib/action_view/helpers/active_model_helper.rb +16 -11
- data/lib/action_view/helpers/asset_tag_helper.rb +241 -82
- data/lib/action_view/helpers/asset_url_helper.rb +171 -67
- data/lib/action_view/helpers/atom_feed_helper.rb +19 -17
- data/lib/action_view/helpers/cache_helper.rb +112 -42
- data/lib/action_view/helpers/capture_helper.rb +20 -13
- data/lib/action_view/helpers/controller_helper.rb +15 -4
- data/lib/action_view/helpers/csp_helper.rb +26 -0
- data/lib/action_view/helpers/csrf_helper.rb +8 -6
- data/lib/action_view/helpers/date_helper.rb +230 -129
- data/lib/action_view/helpers/debug_helper.rb +7 -6
- data/lib/action_view/helpers/form_helper.rb +755 -129
- data/lib/action_view/helpers/form_options_helper.rb +130 -75
- data/lib/action_view/helpers/form_tag_helper.rb +116 -71
- data/lib/action_view/helpers/javascript_helper.rb +30 -14
- data/lib/action_view/helpers/number_helper.rb +84 -59
- data/lib/action_view/helpers/output_safety_helper.rb +36 -4
- data/lib/action_view/helpers/rendering_helper.rb +11 -8
- data/lib/action_view/helpers/sanitize_helper.rb +30 -31
- data/lib/action_view/helpers/tag_helper.rb +201 -75
- data/lib/action_view/helpers/tags/base.rb +138 -98
- data/lib/action_view/helpers/tags/check_box.rb +20 -19
- data/lib/action_view/helpers/tags/checkable.rb +4 -2
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +12 -34
- data/lib/action_view/helpers/tags/collection_helpers.rb +69 -36
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +6 -12
- data/lib/action_view/helpers/tags/collection_select.rb +4 -2
- data/lib/action_view/helpers/tags/color_field.rb +4 -3
- data/lib/action_view/helpers/tags/date_field.rb +2 -1
- data/lib/action_view/helpers/tags/date_select.rb +37 -36
- data/lib/action_view/helpers/tags/datetime_field.rb +4 -3
- data/lib/action_view/helpers/tags/datetime_local_field.rb +2 -1
- data/lib/action_view/helpers/tags/datetime_select.rb +2 -0
- data/lib/action_view/helpers/tags/email_field.rb +2 -0
- data/lib/action_view/helpers/tags/file_field.rb +2 -0
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +4 -2
- data/lib/action_view/helpers/tags/hidden_field.rb +2 -0
- data/lib/action_view/helpers/tags/label.rb +3 -2
- data/lib/action_view/helpers/tags/month_field.rb +2 -1
- data/lib/action_view/helpers/tags/number_field.rb +2 -0
- data/lib/action_view/helpers/tags/password_field.rb +3 -1
- data/lib/action_view/helpers/tags/placeholderable.rb +3 -1
- data/lib/action_view/helpers/tags/radio_button.rb +7 -6
- data/lib/action_view/helpers/tags/range_field.rb +2 -0
- data/lib/action_view/helpers/tags/search_field.rb +14 -9
- data/lib/action_view/helpers/tags/select.rb +11 -10
- data/lib/action_view/helpers/tags/tel_field.rb +2 -0
- data/lib/action_view/helpers/tags/text_area.rb +4 -2
- data/lib/action_view/helpers/tags/text_field.rb +8 -8
- data/lib/action_view/helpers/tags/time_field.rb +2 -1
- data/lib/action_view/helpers/tags/time_select.rb +2 -0
- data/lib/action_view/helpers/tags/time_zone_select.rb +3 -1
- data/lib/action_view/helpers/tags/translator.rb +15 -16
- data/lib/action_view/helpers/tags/url_field.rb +2 -0
- data/lib/action_view/helpers/tags/week_field.rb +2 -1
- data/lib/action_view/helpers/tags.rb +3 -1
- data/lib/action_view/helpers/text_helper.rb +56 -38
- data/lib/action_view/helpers/translation_helper.rb +91 -47
- data/lib/action_view/helpers/url_helper.rb +160 -105
- data/lib/action_view/helpers.rb +5 -3
- data/lib/action_view/layouts.rb +65 -61
- data/lib/action_view/log_subscriber.rb +61 -10
- data/lib/action_view/lookup_context.rb +147 -89
- data/lib/action_view/model_naming.rb +3 -1
- data/lib/action_view/path_set.rb +28 -23
- data/lib/action_view/railtie.rb +62 -6
- data/lib/action_view/record_identifier.rb +53 -26
- data/lib/action_view/renderer/abstract_renderer.rb +71 -13
- data/lib/action_view/renderer/partial_renderer/collection_caching.rb +103 -0
- data/lib/action_view/renderer/partial_renderer.rb +239 -225
- data/lib/action_view/renderer/renderer.rb +22 -8
- data/lib/action_view/renderer/streaming_template_renderer.rb +54 -54
- data/lib/action_view/renderer/template_renderer.rb +79 -73
- data/lib/action_view/rendering.rb +68 -44
- data/lib/action_view/routing_url_for.rb +33 -22
- data/lib/action_view/tasks/cache_digests.rake +25 -0
- data/lib/action_view/template/error.rb +44 -29
- data/lib/action_view/template/handlers/builder.rb +12 -13
- data/lib/action_view/template/handlers/erb/erubi.rb +87 -0
- data/lib/action_view/template/handlers/erb.rb +24 -86
- data/lib/action_view/template/handlers/html.rb +11 -0
- data/lib/action_view/template/handlers/raw.rb +4 -4
- data/lib/action_view/template/handlers.rb +38 -8
- data/lib/action_view/template/html.rb +19 -10
- data/lib/action_view/template/inline.rb +22 -0
- data/lib/action_view/template/raw_file.rb +28 -0
- data/lib/action_view/template/resolver.rb +217 -193
- data/lib/action_view/template/sources/file.rb +17 -0
- data/lib/action_view/template/sources.rb +13 -0
- data/lib/action_view/template/text.rb +11 -10
- data/lib/action_view/template/types.rb +18 -18
- data/lib/action_view/template.rb +146 -90
- data/lib/action_view/test_case.rb +52 -32
- data/lib/action_view/testing/resolvers.rb +46 -34
- data/lib/action_view/unbound_template.rb +31 -0
- data/lib/action_view/version.rb +3 -1
- data/lib/action_view/view_paths.rb +48 -31
- data/lib/action_view.rb +11 -8
- data/lib/assets/compiled/rails-ujs.js +746 -0
- metadata +38 -29
- data/lib/action_view/helpers/record_tag_helper.rb +0 -108
- data/lib/action_view/tasks/dependencies.rake +0 -23
@@ -1,7 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
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
|
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
|
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
|
70
|
+
# # => <script src="/assets/template.debug-1284139606.jst"></script>
|
43
71
|
#
|
44
72
|
# javascript_include_tag "xmlhr.js"
|
45
|
-
# # => <script src="/assets/xmlhr.js
|
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
|
49
|
-
# # <script src="/elsewhere/cools.js
|
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!(
|
59
|
-
|
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" =>
|
96
|
+
"src" => href
|
62
97
|
}.merge!(options)
|
63
|
-
|
98
|
+
if tag_options["nonce"] == true
|
99
|
+
tag_options["nonce"] = content_security_policy_nonce
|
100
|
+
end
|
101
|
+
content_tag("script", "", 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!(
|
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" =>
|
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
|
108
|
-
# <tt>:atom</tt>. Control the link options in url_for format
|
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 :
|
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] ||
|
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(:
|
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,91 +223,156 @@ 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=
|
173
|
-
tag(
|
174
|
-
:
|
175
|
-
:
|
176
|
-
:
|
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
|
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
|
-
#
|
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
|
314
|
+
# # => <img src="/assets/icon" />
|
198
315
|
# image_tag("icon.png")
|
199
|
-
# # => <img
|
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"
|
320
|
+
# # => <img src="/icons/icon.gif" width="16" height="16" />
|
204
321
|
# image_tag("/icons/icon.gif", height: '32', width: '32')
|
205
|
-
# # => <img
|
322
|
+
# # => <img height="32" src="/icons/icon.gif" width="32" />
|
206
323
|
# image_tag("/icons/icon.gif", class: "menu_icon")
|
207
|
-
# # => <img
|
208
|
-
|
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 blobs (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_to_limit: [100, 100]))
|
337
|
+
# # => <img src="/rails/active_storage/representations/.../tiger.jpg" />
|
338
|
+
# image_tag(user.avatar.variant(resize_to_limit: [100, 100]), size: '100')
|
339
|
+
# # => <img width="100" height="100" src="/rails/active_storage/representations/.../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
|
-
|
345
|
+
options[:src] = resolve_image_source(source, skip_pipeline)
|
212
346
|
|
213
|
-
|
214
|
-
options[:
|
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]
|
218
355
|
tag("img", options)
|
219
356
|
end
|
220
357
|
|
221
|
-
# Returns a string suitable for an HTML image tag alt attribute.
|
222
|
-
# The +src+ argument is meant to be an image file path.
|
223
|
-
# The method removes the basename of the file path and the digest,
|
224
|
-
# if any. It also removes hyphens and underscores from file names and
|
225
|
-
# replaces them with spaces, returning a space-separated, titleized
|
226
|
-
# string.
|
227
|
-
#
|
228
|
-
# ==== Examples
|
229
|
-
#
|
230
|
-
# image_alt('rails.png')
|
231
|
-
# # => Rails
|
232
|
-
#
|
233
|
-
# image_alt('hyphenated-file-name.png')
|
234
|
-
# # => Hyphenated file name
|
235
|
-
#
|
236
|
-
# image_alt('underscored_file_name.png')
|
237
|
-
# # => Underscored file name
|
238
|
-
def image_alt(src)
|
239
|
-
File.basename(src, '.*').sub(/-[[:xdigit:]]{32,64}\z/, '').tr('-_', ' ').capitalize
|
240
|
-
end
|
241
|
-
|
242
358
|
# Returns an HTML video tag for the +sources+. If +sources+ is a string,
|
243
359
|
# a single video tag will be returned. If +sources+ is an array, a video
|
244
360
|
# tag with nested source tags for each source will be returned. The
|
245
|
-
# +sources+ can be full paths or files that
|
361
|
+
# +sources+ can be full paths or files that exist in your public videos
|
246
362
|
# directory.
|
247
363
|
#
|
248
364
|
# ==== Options
|
249
|
-
#
|
250
|
-
#
|
365
|
+
#
|
366
|
+
# When the last parameter is a hash you can add HTML attributes using that
|
367
|
+
# parameter. The following options are supported:
|
251
368
|
#
|
252
369
|
# * <tt>:poster</tt> - Set an image (like a screenshot) to be shown
|
253
370
|
# before the video loads. The path is calculated like the +src+ of +image_tag+.
|
254
371
|
# * <tt>:size</tt> - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes
|
255
372
|
# width="30" and height="45", and "50" becomes width="50" and height="50".
|
256
373
|
# <tt>:size</tt> will be ignored if the value is not in the correct format.
|
374
|
+
# * <tt>:poster_skip_pipeline</tt> will bypass the asset pipeline when using
|
375
|
+
# the <tt>:poster</tt> option instead using an asset in the public folder.
|
257
376
|
#
|
258
377
|
# ==== Examples
|
259
378
|
#
|
@@ -261,10 +380,12 @@ module ActionView
|
|
261
380
|
# # => <video src="/videos/trailer"></video>
|
262
381
|
# video_tag("trailer.ogg")
|
263
382
|
# # => <video src="/videos/trailer.ogg"></video>
|
264
|
-
# video_tag("trailer.ogg", controls: true,
|
265
|
-
# # => <video
|
383
|
+
# video_tag("trailer.ogg", controls: true, preload: 'none')
|
384
|
+
# # => <video preload="none" controls="controls" src="/videos/trailer.ogg"></video>
|
266
385
|
# video_tag("trailer.m4v", size: "16x10", poster: "screenshot.png")
|
267
386
|
# # => <video src="/videos/trailer.m4v" width="16" height="10" poster="/assets/screenshot.png"></video>
|
387
|
+
# video_tag("trailer.m4v", size: "16x10", poster: "screenshot.png", poster_skip_pipeline: true)
|
388
|
+
# # => <video src="/videos/trailer.m4v" width="16" height="10" poster="screenshot.png"></video>
|
268
389
|
# video_tag("/trailers/hd.avi", size: "16x16")
|
269
390
|
# # => <video src="/trailers/hd.avi" width="16" height="16"></video>
|
270
391
|
# video_tag("/trailers/hd.avi", size: "16")
|
@@ -278,15 +399,23 @@ module ActionView
|
|
278
399
|
# video_tag(["trailer.ogg", "trailer.flv"], size: "160x120")
|
279
400
|
# # => <video height="120" width="160"><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
|
280
401
|
def video_tag(*sources)
|
281
|
-
|
282
|
-
|
283
|
-
|
402
|
+
options = sources.extract_options!.symbolize_keys
|
403
|
+
public_poster_folder = options.delete(:poster_skip_pipeline)
|
404
|
+
sources << options
|
405
|
+
multiple_sources_tag_builder("video", sources) do |tag_options|
|
406
|
+
tag_options[:poster] = path_to_image(tag_options[:poster], skip_pipeline: public_poster_folder) if tag_options[:poster]
|
407
|
+
tag_options[:width], tag_options[:height] = extract_dimensions(tag_options.delete(:size)) if tag_options[:size]
|
284
408
|
end
|
285
409
|
end
|
286
410
|
|
287
|
-
# Returns an HTML audio tag for the +
|
288
|
-
#
|
289
|
-
#
|
411
|
+
# Returns an HTML audio tag for the +sources+. If +sources+ is a string,
|
412
|
+
# a single audio tag will be returned. If +sources+ is an array, an audio
|
413
|
+
# tag with nested source tags for each source will be returned. The
|
414
|
+
# +sources+ can be full paths or files that exist in your public audios
|
415
|
+
# directory.
|
416
|
+
#
|
417
|
+
# When the last parameter is a hash you can add HTML attributes using that
|
418
|
+
# parameter.
|
290
419
|
#
|
291
420
|
# audio_tag("sound")
|
292
421
|
# # => <audio src="/audios/sound"></audio>
|
@@ -297,33 +426,63 @@ module ActionView
|
|
297
426
|
# audio_tag("sound.wav", "sound.mid")
|
298
427
|
# # => <audio><source src="/audios/sound.wav" /><source src="/audios/sound.mid" /></audio>
|
299
428
|
def audio_tag(*sources)
|
300
|
-
|
429
|
+
multiple_sources_tag_builder("audio", sources)
|
301
430
|
end
|
302
431
|
|
303
432
|
private
|
304
|
-
def
|
305
|
-
options
|
433
|
+
def multiple_sources_tag_builder(type, sources)
|
434
|
+
options = sources.extract_options!.symbolize_keys
|
435
|
+
skip_pipeline = options.delete(:skip_pipeline)
|
306
436
|
sources.flatten!
|
307
437
|
|
308
438
|
yield options if block_given?
|
309
439
|
|
310
440
|
if sources.size > 1
|
311
441
|
content_tag(type, options) do
|
312
|
-
safe_join sources.map { |source| tag("source", :
|
442
|
+
safe_join sources.map { |source| tag("source", src: send("path_to_#{type}", source, skip_pipeline: skip_pipeline)) }
|
313
443
|
end
|
314
444
|
else
|
315
|
-
options[:src] = send("path_to_#{type}", sources.first)
|
445
|
+
options[:src] = send("path_to_#{type}", sources.first, skip_pipeline: skip_pipeline)
|
316
446
|
content_tag(type, nil, options)
|
317
447
|
end
|
318
448
|
end
|
319
449
|
|
450
|
+
def resolve_image_source(source, skip_pipeline)
|
451
|
+
if source.is_a?(Symbol) || source.is_a?(String)
|
452
|
+
path_to_image(source, skip_pipeline: skip_pipeline)
|
453
|
+
else
|
454
|
+
polymorphic_url(source)
|
455
|
+
end
|
456
|
+
rescue NoMethodError => e
|
457
|
+
raise ArgumentError, "Can't resolve image into URL: #{e}"
|
458
|
+
end
|
459
|
+
|
320
460
|
def extract_dimensions(size)
|
321
|
-
|
322
|
-
|
323
|
-
|
461
|
+
size = size.to_s
|
462
|
+
if /\A\d+x\d+\z/.match?(size)
|
463
|
+
size.split("x")
|
464
|
+
elsif /\A\d+\z/.match?(size)
|
324
465
|
[size, size]
|
325
466
|
end
|
326
467
|
end
|
468
|
+
|
469
|
+
def check_for_image_tag_errors(options)
|
470
|
+
if options[:size] && (options[:height] || options[:width])
|
471
|
+
raise ArgumentError, "Cannot pass a :size option with a :height or :width option"
|
472
|
+
end
|
473
|
+
end
|
474
|
+
|
475
|
+
def resolve_link_as(extname, mime_type)
|
476
|
+
if extname == "js"
|
477
|
+
"script"
|
478
|
+
elsif extname == "css"
|
479
|
+
"style"
|
480
|
+
elsif extname == "vtt"
|
481
|
+
"track"
|
482
|
+
elsif (type = mime_type.to_s.split("/")[0]) && type.in?(%w(audio video font))
|
483
|
+
type
|
484
|
+
end
|
485
|
+
end
|
327
486
|
end
|
328
487
|
end
|
329
488
|
end
|