Changeset 9030
- Timestamp:
- 03/15/08 19:59:34 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/text_helper.rb (modified) (4 diffs)
- trunk/actionpack/test/template/text_helper_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r9020 r9030 1 1 *SVN* 2 3 * Fixed that TextHelper#excerpt would include one character too many #11268 [Irfy] 2 4 3 5 * Fix more obscure nested parameter hash parsing bug. #10797 [thomas.lee] trunk/actionpack/lib/action_view/helpers/text_helper.rb
r8584 r9030 93 93 # The +radius+ expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters 94 94 # defined in +radius+ (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+, 95 # then the +excerpt_string+ will be prepended/appended accordingly. If the +phrase+96 # isn't found, nil is returned.95 # then the +excerpt_string+ will be prepended/appended accordingly. The resulting string will be stripped in any case. 96 # If the +phrase+ isn't found, nil is returned. 97 97 # 98 98 # ==== Examples 99 99 # excerpt('This is an example', 'an', 5) 100 # # => "...s is an exam p..."100 # # => "...s is an exam..." 101 101 # 102 102 # excerpt('This is an example', 'is', 5) 103 # # => "This is a n..."103 # # => "This is a..." 104 104 # 105 105 # excerpt('This is an example', 'is') … … 107 107 # 108 108 # excerpt('This next thing is an example', 'ex', 2) 109 # # => "...next t..."109 # # => "...next..." 110 110 # 111 111 # excerpt('This is also an example', 'an', 8, '<chop> ') … … 117 117 if found_pos = text.chars =~ /(#{phrase})/i 118 118 start_pos = [ found_pos - radius, 0 ].max 119 end_pos = [ found_pos + phrase.chars.length + radius, text.chars.length ].min119 end_pos = [ [ found_pos + phrase.chars.length + radius - 1, 0].max, text.chars.length ].min 120 120 121 121 prefix = start_pos > 0 ? excerpt_string : "" 122 postfix = end_pos < text.chars.length ? excerpt_string : ""122 postfix = end_pos < text.chars.length - 1 ? excerpt_string : "" 123 123 124 124 prefix + text.chars[start_pos..end_pos].strip + postfix … … 135 135 if found_pos = text =~ /(#{phrase})/i 136 136 start_pos = [ found_pos - radius, 0 ].max 137 end_pos = [ found_pos + phrase.length + radius, text.length ].min137 end_pos = [ [ found_pos + phrase.length + radius - 1, 0].max, text.length ].min 138 138 139 139 prefix = start_pos > 0 ? excerpt_string : "" 140 postfix = end_pos < text.length ? excerpt_string : ""140 postfix = end_pos < text.length - 1 ? excerpt_string : "" 141 141 142 142 prefix + text[start_pos..end_pos].strip + postfix trunk/actionpack/test/template/text_helper_test.rb
r8506 r9030 103 103 104 104 def test_excerpt 105 assert_equal("...is a beautiful morn i...", excerpt("This is a beautiful morning", "beautiful", 5))105 assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5)) 106 106 assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5)) 107 107 assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5)) … … 109 109 end 110 110 111 def test_excerpt_in_borderline_cases 112 assert_equal("", excerpt("", "", 0)) 113 assert_equal("a", excerpt("a", "a", 0)) 114 assert_equal("...b...", excerpt("abc", "b", 0)) 115 assert_equal("abc", excerpt("abc", "b", 1)) 116 assert_equal("abc...", excerpt("abcd", "b", 1)) 117 assert_equal("...abc", excerpt("zabc", "b", 1)) 118 assert_equal("...abc...", excerpt("zabcd", "b", 1)) 119 assert_equal("zabcd", excerpt("zabcd", "b", 2)) 120 121 # excerpt strips the resulting string before ap-/prepending excerpt_string. 122 # whether this behavior is meaningful when excerpt_string is not to be 123 # appended is questionable. 124 assert_equal("zabcd", excerpt(" zabcd ", "b", 4)) 125 assert_equal("...abc...", excerpt("z abc d", "b", 1)) 126 end 127 111 128 def test_excerpt_with_regex 112 assert_equal('...is a beautiful! mor n...', excerpt('This is a beautiful! morning', 'beautiful', 5))113 assert_equal('...is a beautiful? mor n...', excerpt('This is a beautiful? morning', 'beautiful', 5))129 assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', 5)) 130 assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', 5)) 114 131 end 115 132 … … 117 134 def test_excerpt_with_utf8 118 135 with_kcode('u') do 119 assert_equal("...ï¬ciency could not be h...", excerpt("That's why eï¬ciency could not be helped", 'could', 8))136 assert_equal("...ï¬ciency could not be...", excerpt("That's why eï¬ciency could not be helped", 'could', 8)) 120 137 end 121 138 with_kcode('none') do 122 assert_equal("...\203ciency could not be h...", excerpt("That's why eï¬ciency could not be helped", 'could', 8))139 assert_equal("...\203ciency could not be...", excerpt("That's why eï¬ciency could not be helped", 'could', 8)) 123 140 end 124 141 end 125 142 else 126 143 def test_excerpt_with_utf8 127 assert_equal("...ï¬ciency could not be h...".force_encoding('UTF-8'), excerpt("That's why eï¬ciency could not be helped".force_encoding('UTF-8'), 'could', 8))128 assert_equal("...\203ciency could not be h...", excerpt("That's why eï¬ciency could not be helped", 'could', 8))144 assert_equal("...ï¬ciency could not be...".force_encoding('UTF-8'), excerpt("That's why eï¬ciency could not be helped".force_encoding('UTF-8'), 'could', 8)) 145 assert_equal("...\203ciency could not be...", excerpt("That's why eï¬ciency could not be helped", 'could', 8)) 129 146 end 130 147 end