Module: Hanami::Helpers::AssetsHelper Private
- Includes:
- View::Helpers::TagHelper
- Included in:
- Extensions::View::StandardHelpers
- Defined in:
- lib/hanami/helpers/assets_helper.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
HTML assets helpers
Inject these helpers in a view
Constant Summary collapse
- NEW_LINE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"\n"- WILDCARD_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
".*"- JAVASCRIPT_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
".js"- STYLESHEET_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
".css"- JAVASCRIPT_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"text/javascript"- STYLESHEET_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"text/css"- FAVICON_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"image/x-icon"- STYLESHEET_REL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"stylesheet"- FAVICON_REL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"shortcut icon"- DEFAULT_FAVICON =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"favicon.ico"- CROSSORIGIN_ANONYMOUS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anonymous"- ABSOLUTE_URL_MATCHER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
TODO: we can drop the defined?-check and fallback once Ruby 3.3 becomes our minimum required version
(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER ).make_regexp
- QUERY_STRING_MATCHER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
/\?/
Instance Method Summary collapse
-
#asset_url(source) ⇒ String
private
It generates the relative or absolute URL for the given asset.
-
#audio_tag(source = nil, options = {}, &blk) ⇒ Hanami::View::HTML::SafeString
private
Generate
audiotag for given source. -
#content_security_policy_nonce ⇒ String?
private
Random per request nonce value for Content Security Policy (CSP) rules.
-
#favicon_tag(source = DEFAULT_FAVICON, options = {}) ⇒ Hanami::View::HTML::SafeString
private
Generate
linktag application favicon. -
#image_tag(source, options = {}) ⇒ Hanami::View::HTML::SafeString
private
Generate
imgtag for given source. -
#javascript_tag(*sources, **options) ⇒ Hanami::View::HTML::SafeString
private
Generate
scripttag for given source(s). -
#stylesheet_tag(*sources, **options) ⇒ Hanami::View::HTML::SafeString
private
Generate
linktag for given source(s). -
#video_tag(source = nil, options = {}, &blk) ⇒ Hanami::View::HTML::SafeString
private
Generate
videotag for given source.
Instance Method Details
#asset_url(source) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
It generates the relative or absolute URL for the given asset. It automatically decides if it has to use the relative or absolute depending on the configuration and current environment.
Absolute URLs are returned as they are.
It can be the name of the asset, coming from the sources or third party gems.
If Fingerprint mode is on, it returns the fingerprinted path of the source
If CDN mode is on, it returns the absolute URL of the asset.
subresource_integrity modes are on and the asset is missing from the manifest
694 695 696 697 698 699 |
# File 'lib/hanami/helpers/assets_helper.rb', line 694 def asset_url(source) return source.url if source.respond_to?(:url) return source if _absolute_url?(source) _context.assets[source].url end |
#audio_tag(source = nil, options = {}, &blk) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate audio tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
Alternatively, it accepts a block that allows to specify one or more sources via the source tag.
If the “fingerprint mode” is on, src is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the src is an absolute URL of the application CDN.
subresource_integrity modes are on and the audio file is missing from the manifest
637 638 639 640 |
# File 'lib/hanami/helpers/assets_helper.rb', line 637 def audio_tag(source = nil, = {}, &blk) = (source, , &blk) tag.audio(**, &blk) end |
#content_security_policy_nonce ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Random per request nonce value for Content Security Policy (CSP) rules.
If the Hanami::Middleware::ContentSecurityPolicyNonce middleware is in use, this helper returns the nonce value for the current request or nil otherwise.
For this policy to work in the browser, you have to add the ‘’nonce’‘ placeholder to the script and/or style source policy rule. It will be substituted by the current nonce value like `’nonce-A12OggyZ’.
724 725 726 727 728 |
# File 'lib/hanami/helpers/assets_helper.rb', line 724 def content_security_policy_nonce return unless _context.request? _context.request.env[CONTENT_SECURITY_POLICY_NONCE_REQUEST_KEY] end |
#favicon_tag(source = DEFAULT_FAVICON, options = {}) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate link tag application favicon.
If no argument is given, it assumes favico.ico from the application.
It accepts one string representing the name of the asset.
If the “fingerprint mode” is on, href is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the href is an absolute URL of the application CDN.
subresource_integrity modes are on and the favicon is file missing from the manifest
425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/hanami/helpers/assets_helper.rb', line 425 def favicon_tag(source = DEFAULT_FAVICON, = {}) = .reject { |k, _| k.to_sym == :href } attributes = { href: asset_url(source), rel: FAVICON_REL, type: FAVICON_MIME_TYPE } attributes.merge!() tag.link(**attributes) end |
#image_tag(source, options = {}) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate img tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
alt Attribute is auto generated from src. You can specify a different value, by passing the :src option.
If the “fingerprint mode” is on, src is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the src is an absolute URL of the application CDN.
subresource_integrity modes are on and the image file is missing from the manifest
359 360 361 362 363 364 365 366 367 368 |
# File 'lib/hanami/helpers/assets_helper.rb', line 359 def image_tag(source, = {}) = .reject { |k, _| k.to_sym == :src } attributes = { src: asset_url(source), alt: _context.inflector.humanize(::File.basename(source, WILDCARD_EXT)) } attributes.merge!() tag.img(**attributes) end |
#javascript_tag(*sources, **options) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate script tag for given source(s)
It accepts one or more strings representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. jQuery CDN).
If the “fingerprint mode” is on, src is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the src is an absolute URL of the application CDN.
If the “subresource integrity mode” is on, integrity is the name of the algorithm, then a hyphen, then the hash value of the file. If more than one algorithm is used, they“ll be separated by a space.
If the Content Security Policy uses ‘nonce’ and the source is not absolute, the nonce value of the current request is automatically added as an attribute. You can override this with the ‘nonce: false` option. See #content_security_policy_nonce for more.
subresource_integrity modes are on and the javascript file is missing from the manifest
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/hanami/helpers/assets_helper.rb', line 170 def javascript_tag(*sources, **) = .reject { |k, _| k.to_sym == :src } nonce_option = .delete(:nonce) (*sources) do |source| attributes = { src: _typed_url(source, JAVASCRIPT_EXT), type: JAVASCRIPT_MIME_TYPE, nonce: _nonce(source, nonce_option) } attributes.merge!() if _context.assets.subresource_integrity? || attributes.include?(:integrity) attributes[:integrity] ||= _subresource_integrity_value(source, JAVASCRIPT_EXT) attributes[:crossorigin] ||= CROSSORIGIN_ANONYMOUS end tag.script(**attributes).to_s end end |
#stylesheet_tag(*sources, **options) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate link tag for given source(s)
It accepts one or more strings representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
If the “fingerprint mode” is on, href is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the href is an absolute URL of the application CDN.
If the “subresource integrity mode” is on, integrity is the name of the algorithm, then a hyphen, then the hashed value of the file. If more than one algorithm is used, they“ll be separated by a space.
If the Content Security Policy uses ‘nonce’ and the source is not absolute, the nonce value of the current request is automatically added as an attribute. You can override this with the ‘nonce: false` option. See #content_security_policy_nonce for more.
subresource_integrity modes are on and the stylesheet file is missing from the manifest
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/hanami/helpers/assets_helper.rb', line 274 def stylesheet_tag(*sources, **) = .reject { |k, _| k.to_sym == :href } nonce_option = .delete(:nonce) (*sources) do |source| attributes = { href: _typed_url(source, STYLESHEET_EXT), type: STYLESHEET_MIME_TYPE, rel: STYLESHEET_REL, nonce: _nonce(source, nonce_option) } attributes.merge!() if _context.assets.subresource_integrity? || attributes.include?(:integrity) attributes[:integrity] ||= _subresource_integrity_value(source, STYLESHEET_EXT) attributes[:crossorigin] ||= CROSSORIGIN_ANONYMOUS end tag.link(**attributes).to_s end end |
#video_tag(source = nil, options = {}, &blk) ⇒ Hanami::View::HTML::SafeString
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate video tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
Alternatively, it accepts a block that allows to specify one or more sources via the source tag.
If the “fingerprint mode” is on, src is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the src is an absolute URL of the application CDN.
subresource_integrity modes are on and the video file is missing from the manifest
535 536 537 538 |
# File 'lib/hanami/helpers/assets_helper.rb', line 535 def video_tag(source = nil, = {}, &blk) = (source, , &blk) tag.video(**, &blk) end |