Skip to content

A platform for community discussion. Free, open, simple.

FIX: Show the pause button on animated remote images from any host

Régis Hanol870449d4eef1
diff --git a/lib/cooked_processor_mixin.rb b/lib/cooked_processor_mixin.rb
index 05cbaa66070..7fec00fa18d 100644
--- a/lib/cooked_processor_mixin.rb
+++ b/lib/cooked_processor_mixin.rb
@@ -1,7 +1,6 @@
# frozen_string_literal: true
module CookedProcessorMixin
- GIF_SOURCES_REGEXP = %r{(giphy|tenor)\.com/}
LIGHTBOX_WRAPPER_CSS_CLASS = "lightbox-wrapper"
MIN_LIGHTBOX_WIDTH = 100
MIN_LIGHTBOX_HEIGHT = 100
@@ -192,6 +191,10 @@ module CookedProcessorMixin
image_info(url)&.first
end
+ def animated_image?(url)
+ !!image_info(url)&.last
+ end
+
# [[width, height], animated]
def image_info(url)
@image_info_cache ||= {}
@@ -445,9 +448,7 @@ module CookedProcessorMixin
end
end
- if (upload.present? && upload.animated?) || src.match?(GIF_SOURCES_REGEXP)
- img.add_class("animated")
- end
+ img.add_class("animated") if upload.present? ? upload.animated? : animated_image?(src)
generate_thumbnail =
original_width > SiteSetting.max_image_width || original_height > SiteSetting.max_image_height
diff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rb
index 69efbb4f617..49ebb6f542e 100644
--- a/spec/lib/cooked_post_processor_spec.rb
+++ b/spec/lib/cooked_post_processor_spec.rb
@@ -1088,15 +1088,16 @@ RSpec.describe CookedPostProcessor do
cpp.get_size("//schemaless.url/image.jpg")
end
- it "caches the results" do
+ it "caches the size and animation of an image with a single request" do
RemoteImage
.expects(:size_and_animated)
- .with("http://foo.bar/image3.png")
- .returns([[200, 400], nil])
+ .with("http://foo.bar/image.gif")
+ .returns([[200, 400], true])
.once
- cpp.get_size("http://foo.bar/image3.png")
- expect(cpp.get_size("http://foo.bar/image3.png")).to eq([200, 400])
+ cpp.get_size("http://foo.bar/image.gif")
+ expect(cpp.get_size("http://foo.bar/image.gif")).to eq([200, 400])
+ expect(cpp.animated_image?("http://foo.bar/image.gif")).to eq(true)
end
end
@@ -1183,47 +1184,29 @@ RSpec.describe CookedPostProcessor do
expect(doc.css("img.animated").size).to eq(1)
end
- context "with giphy/tenor images" do
- before do
- CookedPostProcessor
- .any_instance
- .stubs(:get_size)
- .with("https://media2.giphy.com/media/7Oifk90VrCdNe/giphy.webp")
- .returns([311, 280])
- CookedPostProcessor
- .any_instance
- .stubs(:get_size)
- .with("https://media1.tenor.com/images/20c7ddd5e84c7427954f430439c5209d/tenor.gif")
- .returns([833, 104])
- end
-
- it "marks giphy images as animated" do
+ context "with remote images" do
+ def cooked_image
post =
Fabricate(
:post,
user: user_with_auto_groups,
- raw: "![tennis-gif|311x280](https://media2.giphy.com/media/7Oifk90VrCdNe/giphy.webp)",
+ raw: "![gif|498x333](https://example.com/image.webp)",
)
cpp = CookedPostProcessor.new(post, disable_dominant_color: true)
cpp.post_process
+ Nokogiri::HTML5.fragment(cpp.html).at_css("img")
+ end
- doc = Nokogiri::HTML5.fragment(cpp.html)
- expect(doc.css("img.animated").size).to eq(1)
+ it "marks animated images as animated" do
+ stub_image_size(width: 498, height: 333, animated: true)
+
+ expect(cooked_image.classes).to include("animated")
end
- it "marks Tenor images as animated" do
- post =
- Fabricate(
- :post,
- user: user_with_auto_groups,
- raw:
- "![cat](https://media1.tenor.com/images/20c7ddd5e84c7427954f430439c5209d/tenor.gif)",
- )
- cpp = CookedPostProcessor.new(post, disable_dominant_color: true)
- cpp.post_process
+ it "doesn't mark static images as animated" do
+ stub_image_size(width: 498, height: 333, animated: false)
- doc = Nokogiri::HTML5.fragment(cpp.html)
- expect(doc.css("img.animated").size).to eq(1)
+ expect(cooked_image.classes).not_to include("animated")
end
end
diff --git a/spec/support/remote_image_helpers.rb b/spec/support/remote_image_helpers.rb
index cd15e870e91..aa2c5002731 100644
--- a/spec/support/remote_image_helpers.rb
+++ b/spec/support/remote_image_helpers.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
module RemoteImageHelpers
- def stub_image_size(width: nil, height: nil)
- RemoteImage.stubs(:size_and_animated).returns([[width, height], nil])
+ def stub_image_size(width: nil, height: nil, animated: nil)
+ RemoteImage.stubs(:size_and_animated).returns([[width, height], animated])
end
end