mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #38452 from jonathanhefner/config-action_view-image_loading
Add config.action_view.image_loading
This commit is contained in:
commit
3c2a80d8b1
5 changed files with 39 additions and 0 deletions
|
@ -21,6 +21,8 @@ module ActionView
|
||||||
include AssetUrlHelper
|
include AssetUrlHelper
|
||||||
include TagHelper
|
include TagHelper
|
||||||
|
|
||||||
|
mattr_accessor :image_loading
|
||||||
|
|
||||||
# Returns an HTML script tag for each of the +sources+ provided.
|
# Returns an HTML script tag for each of the +sources+ provided.
|
||||||
#
|
#
|
||||||
# Sources may be paths to JavaScript files. Relative paths are assumed to be relative
|
# Sources may be paths to JavaScript files. Relative paths are assumed to be relative
|
||||||
|
@ -365,6 +367,9 @@ module ActionView
|
||||||
end
|
end
|
||||||
|
|
||||||
options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size]
|
options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size]
|
||||||
|
|
||||||
|
options[:loading] ||= image_loading if image_loading
|
||||||
|
|
||||||
tag("img", options)
|
tag("img", options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ module ActionView
|
||||||
config.action_view.embed_authenticity_token_in_remote_forms = nil
|
config.action_view.embed_authenticity_token_in_remote_forms = nil
|
||||||
config.action_view.debug_missing_translation = true
|
config.action_view.debug_missing_translation = true
|
||||||
config.action_view.default_enforce_utf8 = nil
|
config.action_view.default_enforce_utf8 = nil
|
||||||
|
config.action_view.image_loading = nil
|
||||||
|
|
||||||
config.eager_load_namespaces << ActionView
|
config.eager_load_namespaces << ActionView
|
||||||
|
|
||||||
|
@ -37,6 +38,10 @@ module ActionView
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
config.after_initialize do |app|
|
||||||
|
ActionView::Helpers::AssetTagHelper.image_loading = app.config.action_view.delete(:image_loading)
|
||||||
|
end
|
||||||
|
|
||||||
config.after_initialize do |app|
|
config.after_initialize do |app|
|
||||||
ActiveSupport.on_load(:action_view) do
|
ActiveSupport.on_load(:action_view) do
|
||||||
app.config.action_view.each do |k, v|
|
app.config.action_view.each do |k, v|
|
||||||
|
|
|
@ -569,6 +569,16 @@ class AssetTagHelperTest < ActionView::TestCase
|
||||||
assert_equal("Cannot pass a :size option with a :height or :width option", exception.message)
|
assert_equal("Cannot pass a :size option with a :height or :width option", exception.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_image_tag_loading_attribute_default_value
|
||||||
|
original_image_loading = ActionView::Helpers::AssetTagHelper.image_loading
|
||||||
|
ActionView::Helpers::AssetTagHelper.image_loading = "lazy"
|
||||||
|
|
||||||
|
assert_dom_equal %(<img src="" loading="lazy" />), image_tag("")
|
||||||
|
assert_dom_equal %(<img src="" loading="eager" />), image_tag("", loading: "eager")
|
||||||
|
ensure
|
||||||
|
ActionView::Helpers::AssetTagHelper.image_loading = original_image_loading
|
||||||
|
end
|
||||||
|
|
||||||
def test_favicon_link_tag
|
def test_favicon_link_tag
|
||||||
FaviconLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
|
FaviconLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -706,6 +706,8 @@ Defaults to `'signed cookie'`.
|
||||||
|
|
||||||
* `config.action_view.default_enforce_utf8` determines whether forms are generated with a hidden tag that forces older versions of Internet Explorer to submit forms encoded in UTF-8. This defaults to `false`.
|
* `config.action_view.default_enforce_utf8` determines whether forms are generated with a hidden tag that forces older versions of Internet Explorer to submit forms encoded in UTF-8. This defaults to `false`.
|
||||||
|
|
||||||
|
* `config.action_view.image_loading` specifies a default value for the `loading` attribute of `<img>` tags rendered by the `image_tag` helper. For example, when set to `"lazy"`, `<img>` tags rendered by `image_tag` will include `loading="lazy"`, which [instructs the browser to wait until an image is near the viewport to load it](https://html.spec.whatwg.org/#lazy-loading-attributes). (This value can still be overridden per image by passing e.g. `loading: "eager"` to `image_tag`.) Defaults to `nil`.
|
||||||
|
|
||||||
* `config.action_view.annotate_rendered_view_with_filenames` determines whether to annotate rendered view with template file names. This defaults to `false`.
|
* `config.action_view.annotate_rendered_view_with_filenames` determines whether to annotate rendered view with template file names. This defaults to `false`.
|
||||||
|
|
||||||
### Configuring Action Mailbox
|
### Configuring Action Mailbox
|
||||||
|
|
|
@ -2345,6 +2345,23 @@ module ApplicationTests
|
||||||
assert_equal true, ActionView::Helpers::FormTagHelper.default_enforce_utf8
|
assert_equal true, ActionView::Helpers::FormTagHelper.default_enforce_utf8
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "ActionView::Helpers::AssetTagHelper.image_loading is nil by default" do
|
||||||
|
app "development"
|
||||||
|
assert_nil ActionView::Helpers::AssetTagHelper.image_loading
|
||||||
|
end
|
||||||
|
|
||||||
|
test "ActionView::Helpers::AssetTagHelper.image_loading can be configured via config.action_view.image_loading" do
|
||||||
|
app_file "config/environments/development.rb", <<-RUBY
|
||||||
|
Rails.application.configure do
|
||||||
|
config.action_view.image_loading = "lazy"
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
app "development"
|
||||||
|
|
||||||
|
assert_equal "lazy", ActionView::Helpers::AssetTagHelper.image_loading
|
||||||
|
end
|
||||||
|
|
||||||
test "raises when unknown configuration option is set for ActiveJob" do
|
test "raises when unknown configuration option is set for ActiveJob" do
|
||||||
add_to_config <<-RUBY
|
add_to_config <<-RUBY
|
||||||
config.active_job.unknown = "test"
|
config.active_job.unknown = "test"
|
||||||
|
|
Loading…
Reference in a new issue