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.
This commit is contained in:
Bianca Nenciu 2023-04-26 19:05:22 +02:00 committed by GitHub
parent a1b35601fc
commit 024b8b2640
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -385,6 +385,8 @@ class CookedPostProcessor
end end
def process_hotlinked_image(img) 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 @hotlinked_map ||= @post.post_hotlinked_media.preload(:upload).map { |r| [r.url, r] }.to_h
normalized_src = normalized_src =
PostHotlinkedMedia.normalize_src(img["src"] || img[PrettyText::BLOCKED_HOTLINKED_SRC_ATTR]) PostHotlinkedMedia.normalize_src(img["src"] || img[PrettyText::BLOCKED_HOTLINKED_SRC_ATTR])
@ -393,7 +395,7 @@ class CookedPostProcessor
still_an_image = true still_an_image = true
if info&.too_large? if info&.too_large?
if img.ancestors(".onebox, .onebox-body").blank? if !onebox || onebox.element_children.size == 1
add_large_image_placeholder!(img) add_large_image_placeholder!(img)
else else
img.remove img.remove
@ -401,7 +403,7 @@ class CookedPostProcessor
still_an_image = false still_an_image = false
elsif info&.download_failed? elsif info&.download_failed?
if img.ancestors(".onebox, .onebox-body").blank? if !onebox || onebox.element_children.size == 1
add_broken_image_placeholder!(img) add_broken_image_placeholder!(img)
else else
img.remove img.remove

View File

@ -1152,13 +1152,13 @@ RSpec.describe CookedPostProcessor do
it "replaces large image placeholder" do it "replaces large image placeholder" do
SiteSetting.max_image_size_kb = 4096 SiteSetting.max_image_size_kb = 4096
url = "https://image.com/my-avatar" url = "https://image.com/avatar.png"
image_url = "https://image.com/avatar.png"
Oneboxer Oneboxer.stubs(:onebox).with(url, anything).returns <<~HTML
.stubs(:onebox) <a href="#{url}" target="_blank" rel="noopener" class="onebox">
.with(url, anything) <img class='onebox' src='#{url}' />
.returns("<img class='onebox' src='#{image_url}' />") </a>
HTML
post = Fabricate(:post, raw: url) post = Fabricate(:post, raw: url)