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:
Dan Ungureanu 2019-11-29 15:18:42 +02:00 committed by GitHub
parent 8237e0e001
commit 1e0c2235a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 37 deletions

View File

@ -208,9 +208,7 @@ class CookedPostProcessor
# minus emojis # minus emojis
@doc.css("img.emoji") - @doc.css("img.emoji") -
# minus oneboxed images # minus oneboxed images
oneboxed_images - oneboxed_images
# minus images inside quotes
@doc.css(".quote img")
end end
def extract_images_for_post def extract_images_for_post
@ -348,7 +346,8 @@ class CookedPostProcessor
end end
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 end
def loading_image(upload) def loading_image(upload)
@ -373,6 +372,38 @@ class CookedPostProcessor
.each { |r| yield r if r > 1 } .each { |r| yield r if r > 1 }
end end
def optimize_image!(img, upload, cropped: false)
w, h = img["width"].to_i, img["height"].to_i
thumbnail = upload.thumbnail(w, h)
if thumbnail && thumbnail.filesize.to_i < upload.filesize
img["src"] = thumbnail.url
srcset = +""
each_responsive_ratio do |ratio|
resized_w = (w * ratio).to_i
resized_h = (h * ratio).to_i
if !cropped && upload.width && resized_w > upload.width
cooked_url = UrlHelper.cook_url(upload.url, secure: upload.secure?)
srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0$/, "")}x"
elsif t = upload.thumbnail(resized_w, resized_h)
cooked_url = UrlHelper.cook_url(t.url, secure: upload.secure?)
srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0$/, "")}x"
end
img["srcset"] = "#{UrlHelper.cook_url(img["src"], secure: upload.secure?)}#{srcset}" if srcset.present?
end
else
img["src"] = upload.url
end
if small_upload = loading_image(upload)
img["data-small-upload"] = small_upload.url
end
end
def add_lightbox!(img, original_width, original_height, upload, cropped: false) def add_lightbox!(img, original_width, original_height, upload, cropped: false)
# first, create a div to hold our lightbox # first, create a div to hold our lightbox
lightbox = create_node("div", LIGHTBOX_WRAPPER_CSS_CLASS) lightbox = create_node("div", LIGHTBOX_WRAPPER_CSS_CLASS)
@ -389,39 +420,6 @@ class CookedPostProcessor
a.add_child(img) a.add_child(img)
# replace the image by its thumbnail
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
srcset = +""
each_responsive_ratio do |ratio|
resized_w = (w * ratio).to_i
resized_h = (h * ratio).to_i
if !cropped && upload.width && resized_w > upload.width
cooked_url = UrlHelper.cook_url(upload.url, secure: upload.secure?)
srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0$/, "")}x"
elsif t = upload.thumbnail(resized_w, resized_h)
cooked_url = UrlHelper.cook_url(t.url, secure: upload.secure?)
srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0$/, "")}x"
end
img["srcset"] = "#{UrlHelper.cook_url(img["src"], secure: upload.secure?)}#{srcset}" if srcset.present?
end
else
img["src"] = upload.url
end
if small_upload = loading_image(upload)
img["data-small-upload"] = small_upload.url
end
end
# then, some overlay informations # then, some overlay informations
meta = create_node("div", "meta") meta = create_node("div", "meta")
img.add_next_sibling(meta) img.add_next_sibling(meta)

View File

@ -872,6 +872,40 @@ describe CookedPostProcessor do
end 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: "![image|1024x768, 50%](#{upload.short_url})")
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]
![image|1024x768, 50%](#{upload.short_url})
[/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 context "#post_process_oneboxes" do
let(:post) { build(:post_with_youtube, id: 123) } let(:post) { build(:post_with_youtube, id: 123) }
let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) } let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) }