mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +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:
parent
8237e0e001
commit
1e0c2235a3
|
@ -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,26 +372,9 @@ class CookedPostProcessor
|
||||||
.each { |r| yield r if r > 1 }
|
.each { |r| yield r if r > 1 }
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_lightbox!(img, original_width, original_height, upload, cropped: false)
|
def optimize_image!(img, 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
|
|
||||||
w, h = img["width"].to_i, img["height"].to_i
|
w, h = img["width"].to_i, img["height"].to_i
|
||||||
|
|
||||||
if upload
|
|
||||||
thumbnail = upload.thumbnail(w, h)
|
thumbnail = upload.thumbnail(w, h)
|
||||||
if thumbnail && thumbnail.filesize.to_i < upload.filesize
|
if thumbnail && thumbnail.filesize.to_i < upload.filesize
|
||||||
img["src"] = thumbnail.url
|
img["src"] = thumbnail.url
|
||||||
|
@ -422,6 +404,22 @@ class CookedPostProcessor
|
||||||
end
|
end
|
||||||
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
|
# then, some overlay informations
|
||||||
meta = create_node("div", "meta")
|
meta = create_node("div", "meta")
|
||||||
img.add_next_sibling(meta)
|
img.add_next_sibling(meta)
|
||||||
|
|
|
@ -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) }
|
||||||
|
|
Loading…
Reference in New Issue
Block a user