From: Go MAEDA
Date: Tue, 14 Dec 2021 01:24:13 +0000 (+0000)
Subject: Lazy load inline images (#36294).
X-Git-Tag: 5.0.0~153
X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e62358253646f7cc244db660890118e969c84de3;p=redmine.git
Lazy load inline images (#36294).
Patch by Mizuki ISHIKAWA.
git-svn-id: http://svn.redmine.org/redmine/trunk@21313 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index a798261bc..63f80f36a 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -320,7 +320,8 @@ module ApplicationHelper
image_tag(
thumbnail_path(attachment),
:srcset => "#{thumbnail_path(attachment, :size => thumbnail_size * 2)} 2x",
- :style => "max-width: #{thumbnail_size}px; max-height: #{thumbnail_size}px;"
+ :style => "max-width: #{thumbnail_size}px; max-height: #{thumbnail_size}px;",
+ :loading => "lazy"
),
attachment_path(
attachment
@@ -946,7 +947,7 @@ module ApplicationHelper
if !desc.blank? && alttext.blank?
alt = " title=\"#{desc}\" alt=\"#{desc}\""
end
- "src=\"#{image_url}\"#{alt}"
+ "src=\"#{image_url}\"#{alt} loading=\"lazy\""
else
m
end
diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb
index 6e79dce2e..f67a615e5 100644
--- a/test/helpers/application_helper_test.rb
+++ b/test/helpers/application_helper_test.rb
@@ -171,14 +171,14 @@ class ApplicationHelperTest < Redmine::HelperTest
def test_attached_images
to_test = {
'Inline image: !logo.gif!' =>
- 'Inline image: ',
+ 'Inline image: ',
'Inline image: !logo.GIF!' =>
- 'Inline image: ',
+ 'Inline image: ',
'No match: !ogo.gif!' => 'No match: ',
'No match: !ogo.GIF!' => 'No match: ',
# link image
'!logo.gif!:http://foo.bar/' =>
- '',
+ '',
}
attachments = Attachment.all
to_test.each {|text, result| assert_equal "#{result}
", textilizable(text, :attachments => attachments)}
@@ -196,8 +196,8 @@ class ApplicationHelperTest < Redmine::HelperTest
!attached_on_journal.png!'
RAW
- assert textilizable(raw, :object => journal).include?("")
- assert textilizable(raw, :object => journal).include?("")
+ assert textilizable(raw, :object => journal).include?("")
+ assert textilizable(raw, :object => journal).include?("")
end
def test_attached_images_with_textile_and_non_ascii_filename
@@ -208,7 +208,7 @@ class ApplicationHelperTest < Redmine::HelperTest
with_settings :text_formatting => 'textile' do
to_test.each do |filename, result|
attachment = Attachment.generate!(:filename => filename)
- assert_include %(),
+ assert_include %(),
textilizable("!#{filename}!", :attachments => [attachment])
end
end
@@ -224,7 +224,7 @@ class ApplicationHelperTest < Redmine::HelperTest
with_settings :text_formatting => 'markdown' do
to_test.each do |filename, result|
attachment = Attachment.generate!(:filename => filename)
- assert_include %(),
+ assert_include %(),
textilizable("![](#{filename})", :attachments => [attachment])
end
end
@@ -234,7 +234,7 @@ class ApplicationHelperTest < Redmine::HelperTest
attachment = Attachment.generate!(:filename => 'image@2x.png')
assert_equal(
%(),
+ %(srcset="/attachments/download/#{attachment.id}/image@2x.png 2x" alt="" loading="lazy" />
),
textilizable("!image@2x.png!", :attachments => [attachment])
)
end
@@ -286,13 +286,13 @@ class ApplicationHelperTest < Redmine::HelperTest
to_test = {
'Inline image: !testtest.jpg!' =>
- 'Inline image: ',
+ 'Inline image: ',
'Inline image: !testtest.jpeg!' =>
- 'Inline image: ',
+ 'Inline image: ',
'Inline image: !testtest.jpe!' =>
- 'Inline image: ',
+ 'Inline image: ',
'Inline image: !testtest.bmp!' =>
- 'Inline image: ',
+ 'Inline image: ',
}
attachments = [a1, a2, a3, a4]
@@ -315,9 +315,9 @@ class ApplicationHelperTest < Redmine::HelperTest
to_test = {
'Inline image: !testfile.png!' =>
- 'Inline image: ',
+ 'Inline image: ',
'Inline image: !Testfile.PNG!' =>
- 'Inline image: ',
+ 'Inline image: ',
}
attachments = [a1, a2]
to_test.each {|text, result| assert_equal "#{result}
", textilizable(text, :attachments => attachments)}
@@ -1794,7 +1794,7 @@ class ApplicationHelperTest < Redmine::HelperTest
a = Attachment.find(3)
assert_select_in(
thumbnail_tag(a),
- 'a[href=?][title=?] img[src=?]',
+ 'a[href=?][title=?] img[src=?][loading="lazy"]',
"/attachments/3", "logo.gif", "/attachments/thumbnail/3")
end
diff --git a/test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb b/test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb
index fecafd660..80df0572b 100644
--- a/test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb
+++ b/test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb
@@ -49,7 +49,7 @@ class Redmine::WikiFormatting::CommonMark::ApplicationHelperTest < Redmine::Help
with_settings :text_formatting => 'common_mark' do
to_test.each do |filename, result|
attachment = Attachment.generate!(:filename => filename)
- assert_include %(), textilizable("![](#{filename})", :attachments => [attachment])
+ assert_include %(), textilizable("![](#{filename})", :attachments => [attachment])
end
end
end