FIX: blank popular posts in summary emails due to lightbox images

When looking for the first paragraph with content in a post,
it was matching the lightboxed image paragraph as "<p></p>".
Fix that and other potential empty paragraphs with the
p:not(:empty) selector.
Add a new selector to find the image links in lightboxed
images as valid content for emails.
This commit is contained in:
Neil Lalonde 2020-02-21 16:13:06 -05:00
parent 69a2ad626b
commit f73ed45de9
2 changed files with 35 additions and 2 deletions

@ -49,7 +49,7 @@ module UserNotificationsHelper
# If there is no first paragaph with text, return the first paragraph with
# something else (an image) or div (a onebox).
doc.css('body > p, body > div').first
doc.css('body > p:not(:empty), body > div:not(:empty), body > p > div.lightbox-wrapper img').first
end
def email_excerpt(html_arg, post = nil)

@ -28,6 +28,22 @@ describe UserNotificationsHelper do
HTML
end
let(:image_paragraph) do
'<p><img src="//localhost:3000/uploads/b9.png" width="300" height="300"></p>'
end
let(:lightbox_image) do
<<~HTML
<p><div class="lightbox-wrapper"><a class="lightbox" href="//localhost:3000/uploads/default/original/1X/123456.jpeg" data-download-href="//localhost:3000/uploads/default/123456" title="giant-meteor-2020"><img src="//localhost:3000/uploads/default/original/1X/123456.jpeg" alt="giant-meteor-2020" data-base62-sha1="3jcR88161od6Uthq1ixWKJh2ejp" width="517" height="152" data-small-upload="//localhost:3000/uploads/default/optimized/1X/123456_2_10x10.png"><div class="meta">
<svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use xlink:href="#far-image"></use></svg><span class="filename">giant-meteor-2020</span><span class="informations">851×251 44 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use xlink:href="#discourse-expand"></use></svg>
</div></a></div></p>
HTML
end
let(:expected_lightbox_image) do
'<div class="lightbox-wrapper"><a class="lightbox" href="//localhost:3000/uploads/default/original/1X/123456.jpeg" data-download-href="//localhost:3000/uploads/default/123456" title="giant-meteor-2020"><img src="//localhost:3000/uploads/default/original/1X/123456.jpeg" alt="giant-meteor-2020" data-base62-sha1="3jcR88161od6Uthq1ixWKJh2ejp" width="517" height="152" data-small-upload="//localhost:3000/uploads/default/optimized/1X/123456_2_10x10.png"></a></div>'
end
it "can return the first paragraph" do
SiteSetting.digest_min_excerpt_length = 50
expect(helper.email_excerpt(cooked)).to eq(paragraphs[0])
@ -71,7 +87,7 @@ describe UserNotificationsHelper do
end
it "defaults to content after post quote (image w/ no text)" do
image_paragraph = '<p><img src="//localhost:3000/uploads/b9.png" width="300" height="300"></p>'
cooked = <<~HTML
#{post_quote}
#{image_paragraph}
@ -87,6 +103,23 @@ describe UserNotificationsHelper do
HTML
expect(helper.email_excerpt(cooked)).to eq(aside_onebox)
end
it "defaults to content after post quote (lightbox image w/ no text)" do
cooked = <<~HTML
#{post_quote}
#{lightbox_image}
HTML
expect(helper.email_excerpt(cooked)).to eq(expected_lightbox_image)
end
it "handles when there's only an image" do
image_paragraph
expect(helper.email_excerpt("#{image_paragraph}")).to eq(image_paragraph)
end
it "handles when there's only a lightboxed image" do
expect(helper.email_excerpt("#{lightbox_image}")).to eq(expected_lightbox_image)
end
end
describe '#logo_url' do