Skip to content

Commit

Permalink
FIX: non-oneboxed links on separate lines should stay on separate lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ZogStriP committed Apr 11, 2018
1 parent fcd20a7 commit 3c8b43b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/oneboxer.rb
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -98,6 +101,9 @@ def self.apply(string_or_doc, args = nil)
end
end

# strip empty <p> elements
doc.css("p").each { |p| p.remove if p.children.empty? }

Result.new(doc, changed)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/components/oneboxer_spec.rb
Expand Up @@ -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("<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

Expand Down

1 comment on commit 3c8b43b

@discoursebot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/links-on-separate-lines-end-up-on-the-same-line/83911/7

Please sign in to comment.