From 024b8b26402e375bace18ea5a4abd02bb4f15e34 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 26 Apr 2023 19:05:22 +0200 Subject: [PATCH] FIX: Show large image placeholder for image onebox (#21237) Large or broken images are removed from oneboxes, but sometimes images were removed when they were oneboxed too. The reason is that images can be oneboxed by the AllowlistedGenericOnebox or ImageOnebox and only AllowlistedGenericOnebox was handled correctly. --- lib/cooked_post_processor.rb | 6 ++++-- spec/lib/cooked_post_processor_spec.rb | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 8415100965a..0e6e15986e7 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -385,6 +385,8 @@ class CookedPostProcessor end def process_hotlinked_image(img) + onebox = img.ancestors(".onebox, .onebox-body").first + @hotlinked_map ||= @post.post_hotlinked_media.preload(:upload).map { |r| [r.url, r] }.to_h normalized_src = PostHotlinkedMedia.normalize_src(img["src"] || img[PrettyText::BLOCKED_HOTLINKED_SRC_ATTR]) @@ -393,7 +395,7 @@ class CookedPostProcessor still_an_image = true if info&.too_large? - if img.ancestors(".onebox, .onebox-body").blank? + if !onebox || onebox.element_children.size == 1 add_large_image_placeholder!(img) else img.remove @@ -401,7 +403,7 @@ class CookedPostProcessor still_an_image = false elsif info&.download_failed? - if img.ancestors(".onebox, .onebox-body").blank? + if !onebox || onebox.element_children.size == 1 add_broken_image_placeholder!(img) else img.remove diff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rb index 566b9726cea..df42d360cb8 100644 --- a/spec/lib/cooked_post_processor_spec.rb +++ b/spec/lib/cooked_post_processor_spec.rb @@ -1152,13 +1152,13 @@ RSpec.describe CookedPostProcessor do it "replaces large image placeholder" do SiteSetting.max_image_size_kb = 4096 - url = "https://image.com/my-avatar" - image_url = "https://image.com/avatar.png" + url = "https://image.com/avatar.png" - Oneboxer - .stubs(:onebox) - .with(url, anything) - .returns("") + Oneboxer.stubs(:onebox).with(url, anything).returns <<~HTML + + + + HTML post = Fabricate(:post, raw: url)