A platform for community discussion. Free, open, simple.
FIX: Don't disable details when below truncate limit
Robin Ward21e1b05c7ede
diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rbindex 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 = ""enddef self.get_excerpt(html, length, options)@@ -110,8 +111,10 @@ class ExcerptParser < Nokogiri::XML::SAX::Document@in_spoiler = trueendwhen "details"+ @detail_contents = ""@in_details = truewhen "summary"+ @summary_contents = ""@in_summary = trueendend@@ -133,12 +136,15 @@ class ExcerptParser < Nokogiri::XML::SAX::Document@in_quote = falsewhen "details"@in_details = false- when "summary"- @in_summary = false- if @summary_contents.present?+ full = "<details><summary>#{clean(@summary_contents)}</summary>#{clean(@detail_contents)}</details>"+ if @current_length + full.length > @length@excerpt << "<details class='disabled'><summary>#{@summary_contents[0..@length]}</summary></details>"+ else+ @excerpt << fullend- @summary_contents = ""++ when "summary"+ @in_summary = falsewhen "div", "span"throw :done if @start_excerptcharacters("</span>", false, false, false) if @in_spoiler@@ -146,6 +152,10 @@ class ExcerptParser < Nokogiri::XML::SAX::Documentendend+ 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::Documentif @in_detailsif @in_summary@summary_contents << string+ else+ @detail_contents << stringendreturnenddiff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rbindex e0aba3b7dc9..2317dd9556d 100644--- a/spec/components/pretty_text_spec.rb+++ b/spec/components/pretty_text_spec.rb@@ -385,10 +385,14 @@ describe PrettyText doexpect(PrettyText.excerpt("<span class='spoiler'>spoiler</div>", 100)).to match_html "<span class='spoiler'>spoiler</span>"end- it "should keep details" do+ it "should keep details if too long" doexpect(PrettyText.excerpt("<details><summary>expand</summary><p>hello</p></details>", 30)).to match_html "<details class='disabled'><summary>expand</summary></details>"end+ it "doesn't disable details if short enough" do+ expect(PrettyText.excerpt("<details><summary>expand</summary><p>hello</p></details>", 60)).to match_html "<details><summary>expand</summary>hello</details>"+ end+it "should remove meta informations" doexpect(PrettyText.excerpt(wrapped_image, 100)).to match_html "<a href='//localhost:3000/uploads/default/4399/33691397e78b4d75.png' class='lightbox' title='Screen Shot 2014-04-14 at 9.47.10 PM.png'>[image]</a>"end