diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2008-03-15 19:59:34 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2008-03-15 19:59:34 +0000 |
commit | 5f5822af37b439caabf1993453e93ac6182a4c9e (patch) | |
tree | 697633b255b694e671dab91828e1d86d1e6d945f /actionpack/lib/action_view/helpers | |
parent | d0bc724786906644d7ce00e28ca62f5c308f4309 (diff) | |
download | rails-5f5822af37b439caabf1993453e93ac6182a4c9e.tar.gz rails-5f5822af37b439caabf1993453e93ac6182a4c9e.tar.bz2 rails-5f5822af37b439caabf1993453e93ac6182a4c9e.zip |
Fixed that TextHelper#excerpt would include one character too many (closes #11268) [Irfy]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9030 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index d6dbcfbbea..599cd43721 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -92,21 +92,21 @@ module ActionView # Extracts an excerpt from +text+ that matches the first instance of +phrase+. # The +radius+ expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters # defined in +radius+ (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+, - # then the +excerpt_string+ will be prepended/appended accordingly. If the +phrase+ - # isn't found, nil is returned. + # then the +excerpt_string+ will be prepended/appended accordingly. The resulting string will be stripped in any case. + # If the +phrase+ isn't found, nil is returned. # # ==== Examples # excerpt('This is an example', 'an', 5) - # # => "...s is an examp..." + # # => "...s is an exam..." # # excerpt('This is an example', 'is', 5) - # # => "This is an..." + # # => "This is a..." # # excerpt('This is an example', 'is') # # => "This is an example" # # excerpt('This next thing is an example', 'ex', 2) - # # => "...next t..." + # # => "...next..." # # excerpt('This is also an example', 'an', 8, '<chop> ') # # => "<chop> is also an example" @@ -116,10 +116,10 @@ module ActionView if found_pos = text.chars =~ /(#{phrase})/i start_pos = [ found_pos - radius, 0 ].max - end_pos = [ found_pos + phrase.chars.length + radius, text.chars.length ].min + end_pos = [ [ found_pos + phrase.chars.length + radius - 1, 0].max, text.chars.length ].min prefix = start_pos > 0 ? excerpt_string : "" - postfix = end_pos < text.chars.length ? excerpt_string : "" + postfix = end_pos < text.chars.length - 1 ? excerpt_string : "" prefix + text.chars[start_pos..end_pos].strip + postfix else @@ -134,10 +134,10 @@ module ActionView if found_pos = text =~ /(#{phrase})/i start_pos = [ found_pos - radius, 0 ].max - end_pos = [ found_pos + phrase.length + radius, text.length ].min + end_pos = [ [ found_pos + phrase.length + radius - 1, 0].max, text.length ].min prefix = start_pos > 0 ? excerpt_string : "" - postfix = end_pos < text.length ? excerpt_string : "" + postfix = end_pos < text.length - 1 ? excerpt_string : "" prefix + text[start_pos..end_pos].strip + postfix else |