FIX: Do not add lightbox to onebox images (#6479)
This commit is contained in:
parent
a566ed42ae
commit
59be289084
|
@ -60,12 +60,7 @@ class CookedPostProcessor
|
||||||
|
|
||||||
def post_process_images
|
def post_process_images
|
||||||
extract_images.each do |img|
|
extract_images.each do |img|
|
||||||
src = img["src"].sub(/^https?:/i, "")
|
unless add_image_placeholder!(img)
|
||||||
if large_images.include?(src)
|
|
||||||
add_large_image_placeholder!(img)
|
|
||||||
elsif broken_images.include?(src)
|
|
||||||
add_broken_image_placeholder!(img)
|
|
||||||
else
|
|
||||||
limit_size!(img)
|
limit_size!(img)
|
||||||
convert_to_link!(img)
|
convert_to_link!(img)
|
||||||
end
|
end
|
||||||
|
@ -90,6 +85,18 @@ class CookedPostProcessor
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_image_placeholder!(img)
|
||||||
|
src = img["src"].sub(/^https?:/i, "")
|
||||||
|
|
||||||
|
if large_images.include?(src)
|
||||||
|
return add_large_image_placeholder!(img)
|
||||||
|
elsif broken_images.include?(src)
|
||||||
|
return add_broken_image_placeholder!(img)
|
||||||
|
end
|
||||||
|
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
def add_large_image_placeholder!(img)
|
def add_large_image_placeholder!(img)
|
||||||
url = img["src"]
|
url = img["src"]
|
||||||
|
|
||||||
|
@ -127,6 +134,7 @@ class CookedPostProcessor
|
||||||
end
|
end
|
||||||
|
|
||||||
img.remove
|
img.remove
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_broken_image_placeholder!(img)
|
def add_broken_image_placeholder!(img)
|
||||||
|
@ -136,6 +144,7 @@ class CookedPostProcessor
|
||||||
img.remove_attribute("src")
|
img.remove_attribute("src")
|
||||||
img.remove_attribute("width")
|
img.remove_attribute("width")
|
||||||
img.remove_attribute("height")
|
img.remove_attribute("height")
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def large_images
|
def large_images
|
||||||
|
@ -167,6 +176,8 @@ class CookedPostProcessor
|
||||||
@doc.css("img[src^='data']") -
|
@doc.css("img[src^='data']") -
|
||||||
# minus emojis
|
# minus emojis
|
||||||
@doc.css("img.emoji") -
|
@doc.css("img.emoji") -
|
||||||
|
# minus oneboxed images
|
||||||
|
oneboxed_images -
|
||||||
# minus images inside quotes
|
# minus images inside quotes
|
||||||
@doc.css(".quote img")
|
@doc.css(".quote img")
|
||||||
end
|
end
|
||||||
|
@ -442,8 +453,10 @@ class CookedPostProcessor
|
||||||
img_classes = (img["class"] || "").split(" ")
|
img_classes = (img["class"] || "").split(" ")
|
||||||
link_classes = ((parent&.name == "a" && parent["class"]) || "").split(" ")
|
link_classes = ((parent&.name == "a" && parent["class"]) || "").split(" ")
|
||||||
|
|
||||||
if large_images.include?(src) || broken_images.include?(src)
|
if img_classes.include?("onebox") || link_classes.include?("onebox")
|
||||||
img.remove unless img_classes.include?("onebox") || link_classes.include?("onebox")
|
next if add_image_placeholder!(img)
|
||||||
|
elsif large_images.include?(src) || broken_images.include?(src)
|
||||||
|
img.remove
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -178,6 +178,22 @@ describe CookedPostProcessor do
|
||||||
expect(cpp).to be_dirty
|
expect(cpp).to be_dirty
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'when image is inside onebox' do
|
||||||
|
let(:url) { 'https://image.com/my-avatar' }
|
||||||
|
let(:post) { Fabricate(:post, raw: url) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
Oneboxer.stubs(:onebox).with(url, anything).returns("<img class='onebox' src='/uploads/default/original/1X/1234567890123456.jpg' />")
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should not add lightbox' do
|
||||||
|
cpp.post_process_oneboxes
|
||||||
|
cpp.post_process_images
|
||||||
|
|
||||||
|
expect(cpp.html).to match_html("<p><img class=\"onebox\" src=\"/uploads/default/original/1X/1234567890123456.jpg\" width=\"690\"\ height=\"788\"></p>")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'when image is an svg' do
|
describe 'when image is an svg' do
|
||||||
let(:post) do
|
let(:post) do
|
||||||
Fabricate(:post, raw: '<img src="/uploads/default/original/1X/1234567890123456.svg">')
|
Fabricate(:post, raw: '<img src="/uploads/default/original/1X/1234567890123456.svg">')
|
||||||
|
|
Loading…
Reference in New Issue