From 3cd73cdf184a9660070551a65dd7bfe75f1b110c Mon Sep 17 00:00:00 2001
From: Gerhard Schlager <mail@gerhard-schlager.at>
Date: Tue, 17 Oct 2017 22:37:13 +0200
Subject: [PATCH] FIX: fancy topic title must fit into column

---
 app/models/topic.rb       | 7 ++++++-
 spec/models/topic_spec.rb | 8 ++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/app/models/topic.rb b/app/models/topic.rb
index 86b882203ea..fe15c98f00b 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -37,6 +37,10 @@ class Topic < ActiveRecord::Base
     @max_sort_order ||= (2**31) - 1
   end
 
+  def self.max_fancy_title_length
+    400
+  end
+
   def featured_users
     @featured_users ||= TopicFeaturedUsers.new(self)
   end
@@ -304,7 +308,8 @@ class Topic < ActiveRecord::Base
   def self.fancy_title(title)
     escaped = ERB::Util.html_escape(title)
     return unless escaped
-    Emoji.unicode_unescape(HtmlPrettify.render(escaped))
+    fancy_title = Emoji.unicode_unescape(HtmlPrettify.render(escaped))
+    fancy_title.length > Topic.max_fancy_title_length ? title : fancy_title
   end
 
   def fancy_title
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 10560a25de2..1cea61ad4b0 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -329,6 +329,14 @@ describe Topic do
         expect(topic.fancy_title).to eq("this is another edge case")
       end
 
+      it "works with long title that results in lots of entities" do
+        long_title = "NEW STOCK PICK: PRCT - LAST PICK UP 233%, NNCO.................................................................................................................................................................. ofoum"
+        topic.title = long_title
+
+        expect { topic.save! }.to_not raise_error
+        expect(topic.fancy_title).to eq(long_title)
+      end
+
       context 'readonly mode' do
         before do
           Discourse.enable_readonly_mode