FIX: Font tag resulted in wrong email trimming

FIX: Font tag resulted in wrong email trimming
diff --git a/lib/html_to_markdown.rb b/lib/html_to_markdown.rb
index 86ab03d..b16f647 100644
--- a/lib/html_to_markdown.rb
+++ b/lib/html_to_markdown.rb
@@ -16,8 +16,10 @@ class HtmlToMarkdown
   end
 
   # If a `<div>` is within a `<span>` that's invalid, so let's hoist the `<div>` up
+  INLINE_ELEMENTS ||= %w{span font}
+  BLOCK_ELEMENTS ||= %w{div p}
   def fix_span_elements(node)
-    if node.name == 'span' && node.at('div')
+    if (INLINE_ELEMENTS.include?(node.name) && BLOCK_ELEMENTS.any? { |e| node.at(e) })
       node.swap(node.children)
     end
 
diff --git a/spec/components/html_to_markdown_spec.rb b/spec/components/html_to_markdown_spec.rb
index 1aef741..1070cf0 100644
--- a/spec/components/html_to_markdown_spec.rb
+++ b/spec/components/html_to_markdown_spec.rb
@@ -236,9 +236,14 @@ describe HtmlToMarkdown do
     expect(html_to_markdown("<style>* { margin: 0 }</style>")).to eq("")
   end
 
-  it "handles divs within spans" do
-    html = "<div>1st paragraph<span><div>2nd paragraph</div></span></div>"
-    expect(html_to_markdown(html)).to eq("1st paragraph\n2nd paragraph")
+  it "handles <p> and <div> within <span>" do
+    html = "<div>1st paragraph<span><div>2nd paragraph</div><p>3rd paragraph</p></span></div>"
+    expect(html_to_markdown(html)).to eq("1st paragraph\n2nd paragraph\n\n3rd paragraph")
+  end
+
+  it "handles <p> and <div> within <font>" do
+    html = "<font>1st paragraph<br><span>2nd paragraph</span><div>3rd paragraph</div><p>4th paragraph</p></font>"
+    expect(html_to_markdown(html)).to eq("1st paragraph\n2nd paragraph\n3rd paragraph\n\n4th paragraph")
   end
 
   context "with an oddly placed <br>" do

GitHub
sha: 577af81e

2 Likes

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

https://meta.discourse.org/t/incorrect-handling-of-reply-by-e-mail-in-topics/102059/4