From 21e1b05c7ede82f6566622d2c1503757f8478cda Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 20 Dec 2017 15:44:36 -0500 Subject: [PATCH] FIX: Don't disable details when below truncate limit --- lib/excerpt_parser.rb | 20 ++++++++++++++++---- spec/components/pretty_text_spec.rb | 6 +++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rb index 5420ef59e04..0e3f824679b 100644 --- a/lib/excerpt_parser.rb +++ b/lib/excerpt_parser.rb @@ -19,6 +19,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document @remap_emoji = options[:remap_emoji] == true @start_excerpt = false @summary_contents = "" + @detail_contents = "" end def self.get_excerpt(html, length, options) @@ -110,8 +111,10 @@ class ExcerptParser < Nokogiri::XML::SAX::Document @in_spoiler = true end when "details" + @detail_contents = "" @in_details = true when "summary" + @summary_contents = "" @in_summary = true end end @@ -133,12 +136,15 @@ class ExcerptParser < Nokogiri::XML::SAX::Document @in_quote = false when "details" @in_details = false + full = "
#{clean(@summary_contents)}#{clean(@detail_contents)}
" + if @current_length + full.length > @length + @excerpt << "
#{@summary_contents[0..@length]}
" + else + @excerpt << full + end + when "summary" @in_summary = false - if @summary_contents.present? - @excerpt << "
#{@summary_contents[0..@length]}
" - end - @summary_contents = "" when "div", "span" throw :done if @start_excerpt characters("", false, false, false) if @in_spoiler @@ -146,6 +152,10 @@ class ExcerptParser < Nokogiri::XML::SAX::Document end end + def clean(str) + ERB::Util.html_escape(str.strip) + end + def characters(string, truncate = true, count_it = true, encode = true) return if @in_quote @@ -154,6 +164,8 @@ class ExcerptParser < Nokogiri::XML::SAX::Document if @in_details if @in_summary @summary_contents << string + else + @detail_contents << string end return end diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index e0aba3b7dc9..2317dd9556d 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -385,10 +385,14 @@ describe PrettyText do expect(PrettyText.excerpt("spoiler", 100)).to match_html "spoiler" end - it "should keep details" do + it "should keep details if too long" do expect(PrettyText.excerpt("
expand

hello

", 30)).to match_html "
expand
" end + it "doesn't disable details if short enough" do + expect(PrettyText.excerpt("
expand

hello

", 60)).to match_html "
expandhello
" + end + it "should remove meta informations" do expect(PrettyText.excerpt(wrapped_image, 100)).to match_html "[image]" end