From 297680c28d5903212ad6c7eb8c7b7e5d8fce0748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 10 May 2013 12:28:17 +0200 Subject: [PATCH] FIX: pinned topic excerpt is not properly truncated --- lib/pretty_text.rb | 10 +++------- spec/components/pretty_text_spec.rb | 2 ++ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 3edbf23a2..623adec9c 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -247,13 +247,9 @@ module PrettyText end def self.get_excerpt(html, length, options) - me = self.new(length,options) parser = Nokogiri::HTML::SAX::Parser.new(me) begin - copy = "
" - copy << html unless html.nil? - copy << "
" parser.parse(html) unless html.nil? rescue DoneException # we are done @@ -302,8 +298,9 @@ module PrettyText def characters(string, truncate = true, count_it = true, encode = true) return if @in_quote encode = encode ? lambda{|s| ERB::Util.html_escape(s)} : lambda {|s| s} - if @current_length + string.length > @length && count_it - @excerpt << encode.call(string[0..(@length-@current_length)-1]) if truncate + if count_it && @current_length + string.length > @length + length = [0, @length - @current_length - 1].max + @excerpt << encode.call(string[0..length]) if truncate @excerpt << "…" @excerpt << "" if @in_a raise DoneException.new @@ -318,4 +315,3 @@ module PrettyText end end - diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 37c4430ab..61662d3e9 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -131,6 +131,7 @@ test it "should truncate stuff properly" do PrettyText.excerpt("hello world",5).should == "hello…" + PrettyText.excerpt("

hello

world

",6).should == "hello w…" end it "should insert a space between to Ps" do @@ -168,6 +169,7 @@ test it "should handle nil" do PrettyText.excerpt(nil,100).should == '' end + end