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 50 50 "This is a <strong class=\"highlight\">beautiful</strong> morning", 51 51 highlight("This is a beautiful morning", "beautiful") 52 52 ) 53 54 assert_equal( 55 "This is a <strong class=\"highlight\">beautiful</strong> morning", 56 highlight("This is a beautiful morning", "BEAUTIFUL") 57 ) 53 58 54 59 assert_equal( 55 60 "This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day", … … 67 72 ) 68 73 69 74 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 70 88 end 71 89 72 90 def test_highlighter_with_regexp -
lib/action_view/helpers/text_helper.rb
old new 51 51 if text.blank? || phrases.blank? 52 52 text 53 53 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('|') 55 65 text.gsub(/(#{match})/i, highlighter) 56 66 end 57 67 end