Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created May 14, 2011 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/971597 to your computer and use it in GitHub Desktop.
Save AquaGeek/971597 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #1597
From d09ccf1cc0f1426532efdf537a3b8ed73fb5271b Mon Sep 17 00:00:00 2001
From: Peter Wagenet <peter.wagenet@gmail.com>
Date: Thu, 18 Dec 2008 14:41:15 -0500
Subject: [PATCH] Truncate counts HTML special characters in omission as a single character
---
actionpack/lib/action_view/helpers/text_helper.rb | 3 ++-
actionpack/test/template/text_helper_test.rb | 1 +
2 files changed, 3 insertions(+), 1 deletions(-)
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 1d9e4fe..06028fb 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -69,7 +69,8 @@ module ActionView
options.reverse_merge!(:length => 30, :omission => "...")
if text
- l = options[:length] - options[:omission].mb_chars.length
+ # The gsub here allows special HTML chars to only count as one for substitution
+ l = options[:length] - options[:omission].gsub(/&((#\d+)|(\w+));/,'*').mb_chars.length
chars = text.mb_chars
(chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s
end
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb
index a6200fb..1cf38e1 100644
--- a/actionpack/test/template/text_helper_test.rb
+++ b/actionpack/test/template/text_helper_test.rb
@@ -48,6 +48,7 @@ class TextHelperTest < ActionView::TestCase
assert_equal "This is a string that wil[...]", truncate("This is a string that will go longer then the default truncate length of 30", :omission => "[...]")
assert_equal "Hello W...", truncate("Hello World!", :length => 10)
assert_equal "Hello[...]", truncate("Hello World!", :omission => "[...]", :length => 10)
+ assert_equal "Hello Wor&#8230;", truncate("Hello World!", :omission => "&#8230;", :length => 10)
end
if RUBY_VERSION < '1.9.0'
--
1.5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment