Skip to content

A platform for community discussion. Free, open, simple.

FIX: non-oneboxed links on separate lines should stay on separate lines

Régis Hanol3c8b43bb011d
diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb
index b8394f16901..dc5b6491572 100644
--- a/lib/oneboxer.rb
+++ b/lib/oneboxer.rb
@@ -77,6 +77,8 @@ module Oneboxer
doc
end
+ HTML5_BLOCK_ELEMENTS ||= %w{address article aside blockquote canvas center dd div dl dt fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr li main nav noscript ol output p pre section table tfoot ul video}
+
def self.apply(string_or_doc, args = nil)
doc = string_or_doc
doc = Nokogiri::HTML::fragment(doc) if doc.is_a?(String)
@@ -88,8 +90,9 @@ module Oneboxer
parsed_onebox = Nokogiri::HTML::fragment(onebox)
next unless parsed_onebox.children.count > 0
- # special logic to strip empty p elements
- if element&.parent&.node_name&.downcase == "p" && element&.parent&.children&.count == 1
+ if element&.parent&.node_name&.downcase == "p" &&
+ element.parent.children.count == 1 &&
+ HTML5_BLOCK_ELEMENTS.include?(parsed_onebox.children[0].node_name.downcase)
element = element.parent
end
@@ -98,6 +101,9 @@ module Oneboxer
end
end
+ # strip empty <p> elements
+ doc.css("p").each { |p| p.remove if p.children.empty? }
+
Result.new(doc, changed)
end
diff --git a/spec/components/oneboxer_spec.rb b/spec/components/oneboxer_spec.rb
index 601af265fc9..b5ebd305ce2 100644
--- a/spec/components/oneboxer_spec.rb
+++ b/spec/components/oneboxer_spec.rb
@@ -103,7 +103,7 @@ describe Oneboxer do
post = Fabricate(:post, raw: Discourse.base_url + "/new?'class=black")
cpp = CookedPostProcessor.new(post, invalidate_oneboxes: true)
cpp.post_process_oneboxes
- expect(cpp.html).to eq("<a href=\"#{Discourse.base_url}/new?%27class=black\">http://test.localhost/new?%27class=black</a>")
+ expect(cpp.html).to eq("<p><a href=\"#{Discourse.base_url}/new?%27class=black\">http://test.localhost/new?%27class=black</a></p>")
end
end