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