@@ -0,0 +1,45 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
14 | # You should have received a copy of the GNU General Public License | |||
|
15 | # along with this program; if not, write to the Free Software | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | require File.dirname(__FILE__) + '/../../test_helper' | |||
|
19 | ||||
|
20 | class SearchHelperTest < HelperTestCase | |||
|
21 | include SearchHelper | |||
|
22 | ||||
|
23 | def test_highlight_single_token | |||
|
24 | assert_equal 'This is a <span class="highlight token-0">token</span>.', | |||
|
25 | highlight_tokens('This is a token.', %w(token)) | |||
|
26 | end | |||
|
27 | ||||
|
28 | def test_highlight_multiple_tokens | |||
|
29 | assert_equal 'This is a <span class="highlight token-0">token</span> and <span class="highlight token-1">another</span> <span class="highlight token-0">token</span>.', | |||
|
30 | highlight_tokens('This is a token and another token.', %w(token another)) | |||
|
31 | end | |||
|
32 | ||||
|
33 | def test_highlight_should_not_exceed_maximum_length | |||
|
34 | s = (('1234567890' * 100) + ' token ') * 100 | |||
|
35 | r = highlight_tokens(s, %w(token)) | |||
|
36 | assert r.include?('<span class="highlight token-0">token</span>') | |||
|
37 | assert r.length <= 1300 | |||
|
38 | end | |||
|
39 | ||||
|
40 | def test_highlight_multibyte | |||
|
41 | s = ('й' * 200) + ' token ' + ('й' * 200) | |||
|
42 | r = highlight_tokens(s, %w(token)) | |||
|
43 | assert_equal ('й' * 45) + ' ... ' + ('й' * 44) + ' <span class="highlight token-0">token</span> ' + ('й' * 44) + ' ... ' + ('й' * 45), r | |||
|
44 | end | |||
|
45 | end |
@@ -27,8 +27,9 module SearchHelper | |||||
27 | result << '...' |
|
27 | result << '...' | |
28 | break |
|
28 | break | |
29 | end |
|
29 | end | |
|
30 | words = words.mb_chars | |||
30 | if i.even? |
|
31 | if i.even? | |
31 |
result << h(words.length > 100 ? "#{words |
|
32 | result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words) | |
32 | else |
|
33 | else | |
33 | t = (tokens.index(words.downcase) || 0) % 4 |
|
34 | t = (tokens.index(words.downcase) || 0) % 4 | |
34 | result << content_tag('span', h(words), :class => "highlight token-#{t}") |
|
35 | result << content_tag('span', h(words), :class => "highlight token-#{t}") |
General Comments 0
You need to be logged in to leave comments.
Login now