FIX: blank cooked markdown could raise an exception in logs

Previously if somehow a user created a blank markdown document using tag
tricks (eg `<p></p><p></p><p></p><p></p><p></p><p></p>`) and so on, we would
completely strip the document down to blank on post process due to onebox
hack.

Needs a followup cause I am still unclear about the reason for empty p stripping
and it can cause some unclear cases when we re-cook posts.
This commit is contained in:
Sam Saffron 2020-01-29 11:37:04 +11:00
parent ab3bda6cd0
commit 7f3a30d79f
3 changed files with 14 additions and 2 deletions

View File

@ -23,7 +23,8 @@ class CookedPostProcessor
@cooking_options[:topic_id] = post.topic_id @cooking_options[:topic_id] = post.topic_id
@cooking_options = @cooking_options.symbolize_keys @cooking_options = @cooking_options.symbolize_keys
@doc = Nokogiri::HTML::fragment(post.cook(post.raw, @cooking_options)) cooked = post.cook(post.raw, @cooking_options)
@doc = Nokogiri::HTML::fragment(cooked)
@has_oneboxes = post.post_analyzer.found_oneboxes? @has_oneboxes = post.post_analyzer.found_oneboxes?
@size_cache = {} @size_cache = {}

View File

@ -116,7 +116,11 @@ module Oneboxer
end end
# strip empty <p> elements # strip empty <p> elements
doc.css("p").each { |p| p.remove if p.children.empty? } doc.css("p").each do |p|
if p.children.empty? && doc.children.count > 1
p.remove
end
end
Result.new(doc, changed) Result.new(doc, changed)
end end

View File

@ -1783,6 +1783,13 @@ describe CookedPostProcessor do
expect(reply.raw).to eq(raw) expect(reply.raw).to eq(raw)
end end
it "does not generate a blank HTML document" do
post = Fabricate(:post, topic: topic, raw: "<sunday><monday>")
cp = CookedPostProcessor.new(post)
cp.post_process
expect(cp.html).to eq("<p></p>")
end
it "works only on new posts" do it "works only on new posts" do
Fabricate(:post, topic: topic, hidden: true, raw: "this is the second post after") Fabricate(:post, topic: topic, hidden: true, raw: "this is the second post after")
Fabricate(:post, topic: topic, post_type: Post.types[:small_action]) Fabricate(:post, topic: topic, post_type: Post.types[:small_action])