mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
FIX: Optimize quoted images (#8427)
Only images that were part of a lightbox used to be optimized. This patch ensures that quoted images are also optimized.
This commit is contained in:
@ -208,9 +208,7 @@ class CookedPostProcessor
|
||||
# minus emojis
|
||||
@doc.css("img.emoji") -
|
||||
# minus oneboxed images
|
||||
oneboxed_images -
|
||||
# minus images inside quotes
|
||||
@doc.css(".quote img")
|
||||
oneboxed_images
|
||||
end
|
||||
|
||||
def extract_images_for_post
|
||||
@ -348,7 +346,8 @@ class CookedPostProcessor
|
||||
end
|
||||
end
|
||||
|
||||
add_lightbox!(img, original_width, original_height, upload, cropped: crop)
|
||||
add_lightbox!(img, original_width, original_height, upload, cropped: crop) if img.ancestors('.quote').blank?
|
||||
optimize_image!(img, upload, cropped: crop) if upload
|
||||
end
|
||||
|
||||
def loading_image(upload)
|
||||
@ -373,26 +372,9 @@ class CookedPostProcessor
|
||||
.each { |r| yield r if r > 1 }
|
||||
end
|
||||
|
||||
def add_lightbox!(img, original_width, original_height, upload, cropped: false)
|
||||
# first, create a div to hold our lightbox
|
||||
lightbox = create_node("div", LIGHTBOX_WRAPPER_CSS_CLASS)
|
||||
img.add_next_sibling(lightbox)
|
||||
lightbox.add_child(img)
|
||||
|
||||
# then, the link to our larger image
|
||||
a = create_link_node("lightbox", img["src"])
|
||||
img.add_next_sibling(a)
|
||||
|
||||
if upload
|
||||
a["data-download-href"] = Discourse.store.download_url(upload)
|
||||
end
|
||||
|
||||
a.add_child(img)
|
||||
|
||||
# replace the image by its thumbnail
|
||||
def optimize_image!(img, upload, cropped: false)
|
||||
w, h = img["width"].to_i, img["height"].to_i
|
||||
|
||||
if upload
|
||||
thumbnail = upload.thumbnail(w, h)
|
||||
if thumbnail && thumbnail.filesize.to_i < upload.filesize
|
||||
img["src"] = thumbnail.url
|
||||
@ -422,6 +404,22 @@ class CookedPostProcessor
|
||||
end
|
||||
end
|
||||
|
||||
def add_lightbox!(img, original_width, original_height, upload, cropped: false)
|
||||
# first, create a div to hold our lightbox
|
||||
lightbox = create_node("div", LIGHTBOX_WRAPPER_CSS_CLASS)
|
||||
img.add_next_sibling(lightbox)
|
||||
lightbox.add_child(img)
|
||||
|
||||
# then, the link to our larger image
|
||||
a = create_link_node("lightbox", img["src"])
|
||||
img.add_next_sibling(a)
|
||||
|
||||
if upload
|
||||
a["data-download-href"] = Discourse.store.download_url(upload)
|
||||
end
|
||||
|
||||
a.add_child(img)
|
||||
|
||||
# then, some overlay informations
|
||||
meta = create_node("div", "meta")
|
||||
img.add_next_sibling(meta)
|
||||
|
@ -872,6 +872,40 @@ describe CookedPostProcessor do
|
||||
|
||||
end
|
||||
|
||||
context "#convert_to_link" do
|
||||
fab!(:thumbnail) { Fabricate(:optimized_image, upload: upload, width: 512, height: 384) }
|
||||
|
||||
before do
|
||||
CookedPostProcessor.any_instance.stubs(:get_size).with(upload.url).returns([1024, 768])
|
||||
end
|
||||
|
||||
it "adds lightbox and optimizes images" do
|
||||
post = Fabricate(:post, raw: "")
|
||||
|
||||
cpp = CookedPostProcessor.new(post, disable_loading_image: true)
|
||||
cpp.post_process
|
||||
|
||||
doc = Nokogiri::HTML::fragment(cpp.html)
|
||||
expect(doc.css('.lightbox-wrapper').size).to eq(1)
|
||||
expect(doc.css('img').first['srcset']).to_not eq(nil)
|
||||
end
|
||||
|
||||
it "optimizes images in quotes" do
|
||||
post = Fabricate(:post, raw: <<~MD)
|
||||
[quote]
|
||||

|
||||
[/quote]
|
||||
MD
|
||||
|
||||
cpp = CookedPostProcessor.new(post, disable_loading_image: true)
|
||||
cpp.post_process
|
||||
|
||||
doc = Nokogiri::HTML::fragment(cpp.html)
|
||||
expect(doc.css('.lightbox-wrapper').size).to eq(0)
|
||||
expect(doc.css('img').first['srcset']).to_not eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
context "#post_process_oneboxes" do
|
||||
let(:post) { build(:post_with_youtube, id: 123) }
|
||||
let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) }
|
||||
|
Reference in New Issue
Block a user