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

Changeset 2265

Show
Ignore:
Timestamp:
09/19/05 21:36:36 (3 years ago)
Author:
minam
Message:

Make the truncate() helper multi-byte safe (assuming $KCODE has been set to something other than "NONE") #2103

Files:

Legend:

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

    r2257 r2265  
    11*SVN* 
     2 
     3* Make the truncate() helper multi-byte safe (assuming $KCODE has been set to something other than "NONE") #2103 
    24 
    35* Add routing tests from #1945 [ben@groovie.org] 
  • trunk/actionpack/lib/action_view/helpers/text_helper.rb

    r2241 r2265  
    2121      def truncate(text, length = 30, truncate_string = "...") 
    2222        if text.nil? then return end 
    23         if text.length > length then text[0..(length - 3)] + truncate_string else text end 
     23 
     24        if $KCODE == "NONE" 
     25          text.length > length ? text[0..(length - 3)] + truncate_string : text 
     26        else 
     27          chars = text.split(//) 
     28          chars.length > length ? chars[0..(length-3)].join + truncate_string : text 
     29        end 
    2430      end 
    2531 
  • trunk/actionpack/test/template/text_helper_test.rb

    r2241 r2265  
    11require 'test/unit' 
     2require "#{File.dirname(__FILE__)}/../testing_sandbox" 
    23require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper' 
    34require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/numeric'  # for human_size 
     
    89  include ActionView::Helpers::TextHelper 
    910  include ActionView::Helpers::TagHelper 
     11  include TestingSandbox 
    1012   
    1113  def setup 
     
    2426    assert_equal "Hello World!", truncate("Hello World!", 12) 
    2527    assert_equal "Hello Worl...", truncate("Hello World!!", 12) 
     28  end 
     29 
     30  def test_truncate_multibyte_without_kcode 
     31    result = execute_in_sandbox(<<-'CODE') 
     32      require "#{File.dirname(__FILE__)}/../lib/action_view/helpers/text_helper" 
     33      include ActionView::Helpers::TextHelper 
     34      truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", 10) 
     35    CODE 
     36 
     37    assert_equal "\354\225\210\353\205\225\355\225...", result 
     38  end 
     39 
     40  def test_truncate_multibyte_with_kcode 
     41    result = execute_in_sandbox(<<-'CODE') 
     42      $KCODE = "u" 
     43      require 'jcode' 
     44 
     45      require "#{File.dirname(__FILE__)}/../lib/action_view/helpers/text_helper" 
     46      include ActionView::Helpers::TextHelper 
     47      truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254\353\236 \354\225\204\353\235\274\353\246\254\354\230\244", 10) 
     48    CODE 
     49 
     50    assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254\353\236 \354\225\204...", result 
    2651  end 
    2752