FIX: embedding topics would fail with some HTML

FIX: embedding topics would fail with some HTML

When truncating content we try to search for first paragraph, if HTML had no P it would fallback to first div which may have nested elements.

diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index c1948d8..973b489 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -227,7 +227,7 @@ class TopicEmbed < ActiveRecord::Base
     return result unless result.blank?
 
     # If there is no first paragaph, return the first div (onebox)
-    doc.css('div').first
+    doc.css('div').first.to_s
   end
 
   def self.expanded_for(post)
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index 18d4dd9..1ff6904 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -105,6 +105,16 @@ describe TopicEmbed do
 
         expect(post.raw).to include(long_content)
       end
+
+      it 'looks at first div when there is no paragraph' do
+
+        no_para = "<div><h>testing it</h></div>"
+
+        SiteSetting.embed_truncate = true
+        post = TopicEmbed.import(user, url, title, no_para)
+
+        expect(post.raw).to include("testing it")
+      end
     end
   end

GitHub sha: 2408d555

1 Like

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