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.rbindex 05cbaa66070..7fec00fa18d 100644--- a/lib/cooked_processor_mixin.rb+++ b/lib/cooked_processor_mixin.rb@@ -1,7 +1,6 @@# frozen_string_literal: truemodule CookedProcessorMixin- GIF_SOURCES_REGEXP = %r{(giphy|tenor)\.com/}LIGHTBOX_WRAPPER_CSS_CLASS = "lightbox-wrapper"MIN_LIGHTBOX_WIDTH = 100MIN_LIGHTBOX_HEIGHT = 100@@ -192,6 +191,10 @@ module CookedProcessorMixinimage_info(url)&.firstend+ def animated_image?(url)+ !!image_info(url)&.last+ end+# [[width, height], animated]def image_info(url)@image_info_cache ||= {}@@ -445,9 +448,7 @@ module CookedProcessorMixinendend- 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_heightdiff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rbindex 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 docpp.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" doRemoteImage.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)endend@@ -1183,47 +1184,29 @@ RSpec.describe CookedPostProcessor doexpect(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_imagepost =Fabricate(:post,user: user_with_auto_groups,- raw: "",+ raw: "",)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:- "",- )- 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")endenddiff --git a/spec/support/remote_image_helpers.rb b/spec/support/remote_image_helpers.rbindex cd15e870e91..aa2c5002731 100644--- a/spec/support/remote_image_helpers.rb+++ b/spec/support/remote_image_helpers.rb@@ -1,7 +1,7 @@# frozen_string_literal: truemodule 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])endend