Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9030

Show
Ignore:
Timestamp:
03/15/08 19:59:34 (1 year ago)
Author:
david
Message:

Fixed that TextHelper#excerpt would include one character too many (closes #11268) [Irfy]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r9020 r9030  
    11*SVN* 
     2 
     3* Fixed that TextHelper#excerpt would include one character too many #11268 [Irfy] 
    24 
    35* Fix more obscure nested parameter hash parsing bug.  #10797 [thomas.lee] 
  • trunk/actionpack/lib/action_view/helpers/text_helper.rb

    r8584 r9030  
    9393        # The +radius+ expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters 
    9494        # 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. 
    9797        # 
    9898        # ==== Examples 
    9999        #   excerpt('This is an example', 'an', 5)  
    100         #   # => "...s is an examp..." 
     100        #   # => "...s is an exam..." 
    101101        # 
    102102        #   excerpt('This is an example', 'is', 5)  
    103         #   # => "This is an..." 
     103        #   # => "This is a..." 
    104104        # 
    105105        #   excerpt('This is an example', 'is')  
     
    107107        # 
    108108        #   excerpt('This next thing is an example', 'ex', 2)  
    109         #   # => "...next t..." 
     109        #   # => "...next..." 
    110110        # 
    111111        #   excerpt('This is also an example', 'an', 8, '<chop> ') 
     
    117117            if found_pos = text.chars =~ /(#{phrase})/i 
    118118              start_pos = [ found_pos - radius, 0 ].max 
    119               end_pos   = [ found_pos + phrase.chars.length + radius, text.chars.length ].min 
     119              end_pos   = [ [ found_pos + phrase.chars.length + radius - 1, 0].max, text.chars.length ].min 
    120120 
    121121              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 : "" 
    123123 
    124124              prefix + text.chars[start_pos..end_pos].strip + postfix 
     
    135135            if found_pos = text =~ /(#{phrase})/i 
    136136              start_pos = [ found_pos - radius, 0 ].max 
    137               end_pos   = [ found_pos + phrase.length + radius, text.length ].min 
     137              end_pos   = [ [ found_pos + phrase.length + radius - 1, 0].max, text.length ].min 
    138138 
    139139              prefix  = start_pos > 0 ? excerpt_string : "" 
    140               postfix = end_pos < text.length ? excerpt_string : "" 
     140              postfix = end_pos < text.length - 1 ? excerpt_string : "" 
    141141 
    142142              prefix + text[start_pos..end_pos].strip + postfix 
  • trunk/actionpack/test/template/text_helper_test.rb

    r8506 r9030  
    103103 
    104104  def test_excerpt 
    105     assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5)) 
     105    assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5)) 
    106106    assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5)) 
    107107    assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5)) 
     
    109109  end 
    110110 
     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 
    111128  def test_excerpt_with_regex 
    112     assert_equal('...is a beautiful! morn...', excerpt('This is a beautiful! morning', 'beautiful', 5)) 
    113     assert_equal('...is a beautiful? morn...', 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)) 
    114131  end 
    115132 
     
    117134    def test_excerpt_with_utf8 
    118135      with_kcode('u') do 
    119         assert_equal("...fficiency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8)) 
     136        assert_equal("...fficiency could not be...", excerpt("That's why efficiency could not be helped", 'could', 8)) 
    120137      end 
    121138      with_kcode('none') do 
    122         assert_equal("...\203ciency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8)) 
     139        assert_equal("...\203ciency could not be...", excerpt("That's why efficiency could not be helped", 'could', 8)) 
    123140      end 
    124141    end 
    125142  else 
    126143    def test_excerpt_with_utf8 
    127       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)) 
    128       assert_equal("...\203ciency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8)) 
     144      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)) 
     145      assert_equal("...\203ciency could not be...", excerpt("That's why efficiency could not be helped", 'could', 8)) 
    129146    end 
    130147  end