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

This commit is contained in:
Régis Hanol 2018-04-11 21:33:45 +02:00
parent fcd20a70d7
commit 3c8b43bb01
2 changed files with 9 additions and 3 deletions

View File

@ -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

View File

@ -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