Skip to content

Commit

Permalink
FIX: Display large/broken image placeholders for image oneboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Oct 7, 2018
1 parent 83bf641 commit c499872
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/cooked_post_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ def extract_images
@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
Expand Down Expand Up @@ -410,9 +408,12 @@ def post_process_oneboxes
next if img["src"].blank?

src = img["src"].sub(/^https?:/i, "")
parent = img.parent
img_classes = (img["class"] || "").split(" ")
link_classes = ((parent&.name == "a" && parent["class"]) || "").split(" ")

if large_images.include?(src) || broken_images.include?(src)
img.remove
img.remove unless img_classes.include?("onebox") || link_classes.include?("onebox")
next
end

Expand All @@ -426,7 +427,7 @@ def post_process_oneboxes

next if img["class"]&.include?('onebox-avatar')

parent_class = img.parent && img.parent["class"]
parent_class = parent && parent["class"]
width = img["width"].to_i
height = img["height"].to_i

Expand Down
18 changes: 18 additions & 0 deletions spec/components/cooked_post_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,24 @@

expect(cpp.doc.to_s).to eq("<p><img class=\"onebox\" src=\"#{upload.url}\" width=\"\" height=\"\"></p>")
end

it "replaces large image placeholder" do
url = 'https://image.com/my-avatar'
image_url = 'https://image.com/avatar.png'

Oneboxer.stubs(:onebox).with(url, anything).returns("<img class='onebox' src='#{image_url}' />")

post = Fabricate(:post, raw: url)

post.custom_fields[Post::LARGE_IMAGES] = "[\"//image.com/avatar.png\"]"
post.save_custom_fields

cpp = CookedPostProcessor.new(post, invalidate_oneboxes: true)
cpp.post_process_oneboxes
cpp.post_process_images

expect(cpp.doc.to_s).to match(/<div class="large-image-placeholder">/)
end
end

context ".post_process_oneboxes removes nofollow if add_rel_nofollow_to_user_content is disabled" do
Expand Down

1 comment on commit c499872

@discoursebot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/giphy-onebox-for-animated-gifs/41478/13

Please sign in to comment.