mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
d0bc724786
commit
5f5822af37
3 changed files with 35 additions and 16 deletions
|
@ -1,5 +1,7 @@
|
||||||
*SVN*
|
*SVN*
|
||||||
|
|
||||||
|
* Fixed that TextHelper#excerpt would include one character too many #11268 [Irfy]
|
||||||
|
|
||||||
* Fix more obscure nested parameter hash parsing bug. #10797 [thomas.lee]
|
* Fix more obscure nested parameter hash parsing bug. #10797 [thomas.lee]
|
||||||
|
|
||||||
* Added ActionView::Helpers::register_javascript/stylesheet_expansion to make it easier for plugin developers to inject multiple assets #10350 [lotswholetime]
|
* Added ActionView::Helpers::register_javascript/stylesheet_expansion to make it easier for plugin developers to inject multiple assets #10350 [lotswholetime]
|
||||||
|
|
|
@ -92,21 +92,21 @@ module ActionView
|
||||||
# Extracts an excerpt from +text+ that matches the first instance of +phrase+.
|
# 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
|
# 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+,
|
# 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+
|
# then the +excerpt_string+ will be prepended/appended accordingly. The resulting string will be stripped in any case.
|
||||||
# isn't found, nil is returned.
|
# If the +phrase+ isn't found, nil is returned.
|
||||||
#
|
#
|
||||||
# ==== Examples
|
# ==== Examples
|
||||||
# excerpt('This is an example', 'an', 5)
|
# excerpt('This is an example', 'an', 5)
|
||||||
# # => "...s is an examp..."
|
# # => "...s is an exam..."
|
||||||
#
|
#
|
||||||
# excerpt('This is an example', 'is', 5)
|
# excerpt('This is an example', 'is', 5)
|
||||||
# # => "This is an..."
|
# # => "This is a..."
|
||||||
#
|
#
|
||||||
# excerpt('This is an example', 'is')
|
# excerpt('This is an example', 'is')
|
||||||
# # => "This is an example"
|
# # => "This is an example"
|
||||||
#
|
#
|
||||||
# excerpt('This next thing is an example', 'ex', 2)
|
# excerpt('This next thing is an example', 'ex', 2)
|
||||||
# # => "...next t..."
|
# # => "...next..."
|
||||||
#
|
#
|
||||||
# excerpt('This is also an example', 'an', 8, '<chop> ')
|
# excerpt('This is also an example', 'an', 8, '<chop> ')
|
||||||
# # => "<chop> is also an example"
|
# # => "<chop> is also an example"
|
||||||
|
@ -116,10 +116,10 @@ module ActionView
|
||||||
|
|
||||||
if found_pos = text.chars =~ /(#{phrase})/i
|
if found_pos = text.chars =~ /(#{phrase})/i
|
||||||
start_pos = [ found_pos - radius, 0 ].max
|
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 : ""
|
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
|
prefix + text.chars[start_pos..end_pos].strip + postfix
|
||||||
else
|
else
|
||||||
|
@ -134,10 +134,10 @@ module ActionView
|
||||||
|
|
||||||
if found_pos = text =~ /(#{phrase})/i
|
if found_pos = text =~ /(#{phrase})/i
|
||||||
start_pos = [ found_pos - radius, 0 ].max
|
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 : ""
|
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
|
prefix + text[start_pos..end_pos].strip + postfix
|
||||||
else
|
else
|
||||||
|
|
|
@ -102,30 +102,47 @@ class TextHelperTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_excerpt
|
def test_excerpt
|
||||||
assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5))
|
assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5))
|
||||||
assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))
|
assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))
|
||||||
assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
|
assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
|
||||||
assert_nil excerpt("This is a beautiful morning", "day")
|
assert_nil excerpt("This is a beautiful morning", "day")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_excerpt_in_borderline_cases
|
||||||
|
assert_equal("", excerpt("", "", 0))
|
||||||
|
assert_equal("a", excerpt("a", "a", 0))
|
||||||
|
assert_equal("...b...", excerpt("abc", "b", 0))
|
||||||
|
assert_equal("abc", excerpt("abc", "b", 1))
|
||||||
|
assert_equal("abc...", excerpt("abcd", "b", 1))
|
||||||
|
assert_equal("...abc", excerpt("zabc", "b", 1))
|
||||||
|
assert_equal("...abc...", excerpt("zabcd", "b", 1))
|
||||||
|
assert_equal("zabcd", excerpt("zabcd", "b", 2))
|
||||||
|
|
||||||
|
# excerpt strips the resulting string before ap-/prepending excerpt_string.
|
||||||
|
# whether this behavior is meaningful when excerpt_string is not to be
|
||||||
|
# appended is questionable.
|
||||||
|
assert_equal("zabcd", excerpt(" zabcd ", "b", 4))
|
||||||
|
assert_equal("...abc...", excerpt("z abc d", "b", 1))
|
||||||
|
end
|
||||||
|
|
||||||
def test_excerpt_with_regex
|
def test_excerpt_with_regex
|
||||||
assert_equal('...is a beautiful! morn...', excerpt('This is a beautiful! morning', 'beautiful', 5))
|
assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', 5))
|
||||||
assert_equal('...is a beautiful? morn...', excerpt('This is a beautiful? morning', 'beautiful', 5))
|
assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', 5))
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_VERSION < '1.9'
|
if RUBY_VERSION < '1.9'
|
||||||
def test_excerpt_with_utf8
|
def test_excerpt_with_utf8
|
||||||
with_kcode('u') do
|
with_kcode('u') do
|
||||||
assert_equal("...fficiency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8))
|
assert_equal("...fficiency could not be...", excerpt("That's why efficiency could not be helped", 'could', 8))
|
||||||
end
|
end
|
||||||
with_kcode('none') do
|
with_kcode('none') do
|
||||||
assert_equal("...\203ciency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8))
|
assert_equal("...\203ciency could not be...", excerpt("That's why efficiency could not be helped", 'could', 8))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
def test_excerpt_with_utf8
|
def test_excerpt_with_utf8
|
||||||
assert_equal("...fficiency could not be h...".force_encoding('UTF-8'), excerpt("That's why efficiency could not be helped".force_encoding('UTF-8'), 'could', 8))
|
assert_equal("...fficiency could not be...".force_encoding('UTF-8'), excerpt("That's why efficiency could not be helped".force_encoding('UTF-8'), 'could', 8))
|
||||||
assert_equal("...\203ciency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8))
|
assert_equal("...\203ciency could not be...", excerpt("That's why efficiency could not be helped", 'could', 8))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue