Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config.action_view.image_loading #38452

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions actionview/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -21,6 +21,8 @@ module AssetTagHelper
include AssetUrlHelper
include TagHelper

mattr_accessor :image_loading

# 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
Expand Down Expand Up @@ -365,6 +367,9 @@ def image_tag(source, options = {})
end

options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size]

options[:loading] ||= image_loading if image_loading

tag("img", options)
end

Expand Down
5 changes: 5 additions & 0 deletions actionview/lib/action_view/railtie.rb
Expand Up @@ -10,6 +10,7 @@ class Railtie < Rails::Engine # :nodoc:
config.action_view.embed_authenticity_token_in_remote_forms = nil
config.action_view.debug_missing_translation = true
config.action_view.default_enforce_utf8 = nil
config.action_view.image_loading = nil

config.eager_load_namespaces << ActionView

Expand Down Expand Up @@ -37,6 +38,10 @@ class Railtie < Rails::Engine # :nodoc:
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|
ActiveSupport.on_load(:action_view) do
app.config.action_view.each do |k, v|
Expand Down
10 changes: 10 additions & 0 deletions actionview/test/template/asset_tag_helper_test.rb
Expand Up @@ -569,6 +569,16 @@ def test_image_tag_raises_an_error_for_competing_size_arguments
assert_equal("Cannot pass a :size option with a :height or :width option", exception.message)
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
FaviconLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
Expand Down
2 changes: 2 additions & 0 deletions guides/source/configuring.md
Expand Up @@ -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.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`.

### Configuring Action Mailbox
Expand Down
17 changes: 17 additions & 0 deletions railties/test/application/configuration_test.rb
Expand Up @@ -2322,6 +2322,23 @@ class ::DummySerializer < ActiveJob::Serializers::ObjectSerializer; end
assert_equal true, ActionView::Helpers::FormTagHelper.default_enforce_utf8
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
add_to_config <<-RUBY
config.active_job.unknown = "test"
Expand Down