mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 07:53:49 +08:00
FIX: Remove dead and large images from oneboxes (#17868)
Dead and large images are replaced with a placeholder, either a broken chain icon or a short text. This commit no longer applies this transformation for images inside Oneboxes, but removes them instead.
This commit is contained in:
@ -378,10 +378,20 @@ class CookedPostProcessor
|
|||||||
still_an_image = true
|
still_an_image = true
|
||||||
|
|
||||||
if info&.too_large?
|
if info&.too_large?
|
||||||
add_large_image_placeholder!(img)
|
if img.ancestors('.onebox, .onebox-body').blank?
|
||||||
|
add_large_image_placeholder!(img)
|
||||||
|
else
|
||||||
|
img.remove
|
||||||
|
end
|
||||||
|
|
||||||
still_an_image = false
|
still_an_image = false
|
||||||
elsif info&.download_failed?
|
elsif info&.download_failed?
|
||||||
add_broken_image_placeholder!(img)
|
if img.ancestors('.onebox, .onebox-body').blank?
|
||||||
|
add_broken_image_placeholder!(img)
|
||||||
|
else
|
||||||
|
img.remove
|
||||||
|
end
|
||||||
|
|
||||||
still_an_image = false
|
still_an_image = false
|
||||||
elsif info&.downloaded? && upload = info&.upload
|
elsif info&.downloaded? && upload = info&.upload
|
||||||
img["src"] = UrlHelper.cook_url(upload.url, secure: @with_secure_media)
|
img["src"] = UrlHelper.cook_url(upload.url, secure: @with_secure_media)
|
||||||
|
@ -1132,6 +1132,44 @@ RSpec.describe CookedPostProcessor do
|
|||||||
expect(cpp.doc.to_s).to include(I18n.t("upload.placeholders.too_large_humanized", max_size: "4 MB"))
|
expect(cpp.doc.to_s).to include(I18n.t("upload.placeholders.too_large_humanized", max_size: "4 MB"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "removes large images from onebox" do
|
||||||
|
url = 'https://example.com/article'
|
||||||
|
|
||||||
|
Oneboxer.stubs(:onebox).with(url, anything).returns <<~HTML
|
||||||
|
<aside class="onebox allowlistedgeneric" data-onebox-src="https://example.com/article">
|
||||||
|
<header class="source">
|
||||||
|
<img src="https://example.com/favicon.ico" class="site-icon">
|
||||||
|
<a href="https://example.com/article" target="_blank" rel="nofollow ugc noopener">Example Site</a>
|
||||||
|
</header>
|
||||||
|
<article class="onebox-body">
|
||||||
|
<img src="https://example.com/article.jpeg" class="thumbnail">
|
||||||
|
<h3><a href="https://example.com/article" target="_blank" rel="nofollow ugc noopener">Lorem Ispum</a></h3>
|
||||||
|
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus neque, malesuada ac neque ac, tempus tincidunt lectus.</p>
|
||||||
|
</article>
|
||||||
|
</aside>
|
||||||
|
HTML
|
||||||
|
|
||||||
|
post = Fabricate(:post, raw: url)
|
||||||
|
|
||||||
|
PostHotlinkedMedia.create!(url: "//example.com/favicon.ico", post: post, status: 'too_large')
|
||||||
|
PostHotlinkedMedia.create!(url: "//example.com/article.jpeg", post: post, status: 'too_large')
|
||||||
|
|
||||||
|
cpp = CookedPostProcessor.new(post, invalidate_oneboxes: true)
|
||||||
|
cpp.post_process
|
||||||
|
|
||||||
|
expect(cpp.doc).to match_html <<~HTML
|
||||||
|
<aside class="onebox allowlistedgeneric" data-onebox-src="https://example.com/article">
|
||||||
|
<header class="source">
|
||||||
|
<a href="https://example.com/article" target="_blank" rel="noopener nofollow ugc">Example Site</a>
|
||||||
|
</header>
|
||||||
|
<article class="onebox-body">
|
||||||
|
<h3><a href="https://example.com/article" target="_blank" rel="noopener nofollow ugc">Lorem Ispum</a></h3>
|
||||||
|
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus neque, malesuada ac neque ac, tempus tincidunt lectus.</p>
|
||||||
|
</article>
|
||||||
|
</aside>
|
||||||
|
HTML
|
||||||
|
end
|
||||||
|
|
||||||
it "replaces broken image placeholder" do
|
it "replaces broken image placeholder" do
|
||||||
url = 'https://image.com/my-avatar'
|
url = 'https://image.com/my-avatar'
|
||||||
image_url = 'https://image.com/avatar.png'
|
image_url = 'https://image.com/avatar.png'
|
||||||
@ -1148,6 +1186,44 @@ RSpec.describe CookedPostProcessor do
|
|||||||
expect(cpp.doc.to_s).to have_tag("span.broken-image")
|
expect(cpp.doc.to_s).to have_tag("span.broken-image")
|
||||||
expect(cpp.doc.to_s).to include(I18n.t("post.image_placeholder.broken"))
|
expect(cpp.doc.to_s).to include(I18n.t("post.image_placeholder.broken"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "removes broken images from onebox" do
|
||||||
|
url = 'https://example.com/article'
|
||||||
|
|
||||||
|
Oneboxer.stubs(:onebox).with(url, anything).returns <<~HTML
|
||||||
|
<aside class="onebox allowlistedgeneric" data-onebox-src="https://example.com/article">
|
||||||
|
<header class="source">
|
||||||
|
<img src="https://example.com/favicon.ico" class="site-icon">
|
||||||
|
<a href="https://example.com/article" target="_blank" rel="nofollow ugc noopener">Example Site</a>
|
||||||
|
</header>
|
||||||
|
<article class="onebox-body">
|
||||||
|
<img src="https://example.com/article.jpeg" class="thumbnail">
|
||||||
|
<h3><a href="https://example.com/article" target="_blank" rel="nofollow ugc noopener">Lorem Ispum</a></h3>
|
||||||
|
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus neque, malesuada ac neque ac, tempus tincidunt lectus.</p>
|
||||||
|
</article>
|
||||||
|
</aside>
|
||||||
|
HTML
|
||||||
|
|
||||||
|
post = Fabricate(:post, raw: url)
|
||||||
|
|
||||||
|
PostHotlinkedMedia.create!(url: "//example.com/favicon.ico", post: post, status: 'download_failed')
|
||||||
|
PostHotlinkedMedia.create!(url: "//example.com/article.jpeg", post: post, status: 'download_failed')
|
||||||
|
|
||||||
|
cpp = CookedPostProcessor.new(post, invalidate_oneboxes: true)
|
||||||
|
cpp.post_process
|
||||||
|
|
||||||
|
expect(cpp.doc).to match_html <<~HTML
|
||||||
|
<aside class="onebox allowlistedgeneric" data-onebox-src="https://example.com/article">
|
||||||
|
<header class="source">
|
||||||
|
<a href="https://example.com/article" target="_blank" rel="noopener nofollow ugc">Example Site</a>
|
||||||
|
</header>
|
||||||
|
<article class="onebox-body">
|
||||||
|
<h3><a href="https://example.com/article" target="_blank" rel="noopener nofollow ugc">Lorem Ispum</a></h3>
|
||||||
|
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus neque, malesuada ac neque ac, tempus tincidunt lectus.</p>
|
||||||
|
</article>
|
||||||
|
</aside>
|
||||||
|
HTML
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#post_process_oneboxes removes nofollow if add_rel_nofollow_to_user_content is disabled" do
|
describe "#post_process_oneboxes removes nofollow if add_rel_nofollow_to_user_content is disabled" do
|
||||||
|
Reference in New Issue
Block a user