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

Ticket #7400: make_highlight_helper_multibyte_safe.3.diff

File make_highlight_helper_multibyte_safe.3.diff, 2.2 kB (added by manfred, 1 year ago)
  • test/template/text_helper_test.rb

    old new  
    5050      "This is a <strong class=\"highlight\">beautiful</strong> morning", 
    5151      highlight("This is a beautiful morning", "beautiful") 
    5252    ) 
     53     
     54    assert_equal( 
     55      "This is a <strong class=\"highlight\">beautiful</strong> morning", 
     56      highlight("This is a beautiful morning", "BEAUTIFUL") 
     57    )   
    5358 
    5459    assert_equal( 
    5560      "This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day", 
     
    6772    ) 
    6873 
    6974    assert_equal '   ', highlight('   ', 'blank text is returned verbatim') 
     75     
     76     
     77    with_kcode 'u' do 
     78      assert_equal( 
     79        "Saisissez directement votre <strong class=\"highlight\">région</strong> une autre <strong class=\"highlight\">région</strong>
", 
     80        highlight("Saisissez directement votre région une autre région
", "région") 
     81      ) 
     82       
     83      assert_equal( 
     84        "Saisissez directement votre <strong class=\"highlight\">région</strong> une autre <strong class=\"highlight\">région</strong>
", 
     85        highlight("Saisissez directement votre région une autre région
", "RÉGION") 
     86      ) 
     87    end 
    7088  end 
    7189 
    7290  def test_highlighter_with_regexp 
  • lib/action_view/helpers/text_helper.rb

    old new  
    5151        if text.blank? || phrases.blank? 
    5252          text 
    5353        else 
    54           match = Array(phrases).map { |p| Regexp.escape(p) }.join('|') 
     54          match = Array(phrases).map do |p| 
     55            p.split(//u).map do |x| 
     56              if x.length > 1 
     57                uc = x.chars.upcase 
     58                dc = x.chars.downcase 
     59                uc == dc ? Regexp.escape(x) : "[#{uc}#{dc}]" 
     60              else 
     61                Regexp.escape(x) 
     62              end 
     63            end.join 
     64          end.join('|') 
    5565          text.gsub(/(#{match})/i, highlighter) 
    5666        end 
    5767      end