diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 938c8fbeb19..a73f65f41f0 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -23,7 +23,8 @@ class CookedPostProcessor @cooking_options[:topic_id] = post.topic_id @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? @size_cache = {} diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index 1c2e9998944..9c323f0c391 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -116,7 +116,11 @@ module Oneboxer end # strip empty

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) end diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 4a9165d5b6e..9e23a18a92b 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -1783,6 +1783,13 @@ describe CookedPostProcessor do expect(reply.raw).to eq(raw) end + it "does not generate a blank HTML document" do + post = Fabricate(:post, topic: topic, raw: "") + cp = CookedPostProcessor.new(post) + cp.post_process + expect(cp.html).to eq("

") + end + it "works only on new posts" do Fabricate(:post, topic: topic, hidden: true, raw: "this is the second post after") Fabricate(:post, topic: topic, post_type: Post.types[:small_action])