From 3c8b43bb011da73507c022b0dceaa807fd718c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 11 Apr 2018 21:33:45 +0200 Subject: [PATCH] FIX: non-oneboxed links on separate lines should stay on separate lines --- lib/oneboxer.rb | 10 ++++++++-- spec/components/oneboxer_spec.rb | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index b8394f169019f..dc5b64915723d 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -77,6 +77,8 @@ def self.each_onebox_link(string_or_doc) 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 @@ def self.apply(string_or_doc, args = nil) 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 @@ def self.apply(string_or_doc, args = nil) end end + # strip empty

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 601af265fc918..b5ebd305ce2d1 100644 --- a/spec/components/oneboxer_spec.rb +++ b/spec/components/oneboxer_spec.rb @@ -103,7 +103,7 @@ def preview(url, user = nil, category = nil, topic = nil) 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("http://test.localhost/new?%27class=black") + expect(cpp.html).to eq("

http://test.localhost/new?%27class=black

") end end