FIX: Do not add lightbox to onebox images (#6479)

This commit is contained in:
Vinoth Kannan 2018-10-11 03:27:21 +05:30 committed by Sam
parent a566ed42ae
commit 59be289084
2 changed files with 37 additions and 8 deletions

View File

@ -60,12 +60,7 @@ class CookedPostProcessor
def post_process_images
extract_images.each do |img|
src = img["src"].sub(/^https?:/i, "")
if large_images.include?(src)
add_large_image_placeholder!(img)
elsif broken_images.include?(src)
add_broken_image_placeholder!(img)
else
unless add_image_placeholder!(img)
limit_size!(img)
convert_to_link!(img)
end
@ -90,6 +85,18 @@ class CookedPostProcessor
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)
url = img["src"]
@ -127,6 +134,7 @@ class CookedPostProcessor
end
img.remove
true
end
def add_broken_image_placeholder!(img)
@ -136,6 +144,7 @@ class CookedPostProcessor
img.remove_attribute("src")
img.remove_attribute("width")
img.remove_attribute("height")
true
end
def large_images
@ -167,6 +176,8 @@ class CookedPostProcessor
@doc.css("img[src^='data']") -
# minus emojis
@doc.css("img.emoji") -
# minus oneboxed images
oneboxed_images -
# minus images inside quotes
@doc.css(".quote img")
end
@ -442,8 +453,10 @@ class CookedPostProcessor
img_classes = (img["class"] || "").split(" ")
link_classes = ((parent&.name == "a" && parent["class"]) || "").split(" ")
if large_images.include?(src) || broken_images.include?(src)
img.remove unless img_classes.include?("onebox") || link_classes.include?("onebox")
if 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
end

View File

@ -178,6 +178,22 @@ describe CookedPostProcessor do
expect(cpp).to be_dirty
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
let(:post) do
Fabricate(:post, raw: '<img src="/uploads/default/original/1X/1234567890123456.svg">')