Hi everybody,
I just stumbled over a user note in the PHP manual, and checked CMSimple_XH; apparently, that's a bug.
Search highlighting breaks for empty search string
Search highlighting breaks for empty search string
Christoph M. Becker – Plugins for CMSimple_XH
Re: Search highlighting breaks for empty search string
While fixing this bug, I noticed another issue: searching for spaces can generate tremendous amounts of HTML (have a look at the generated HTML source, or at the Content-Length header).
And there's yet another issue: searching for the same word twice will add two <span>s. Not really a problem, but useless.
This patch should solve all these issues:
And there's yet another issue: searching for the same word twice will add two <span>s. Not really a problem, but useless.
This patch should solve all these issues:
Code: Select all
Index: cmsimple/functions.php
===================================================================
--- cmsimple/functions.php (revision 1610)
+++ cmsimple/functions.php (working copy)
@@ -2884,10 +2884,14 @@
*/
function XH_highlightSearchWords($words, $text)
{
+ $words = array_unique($words);
usort($words, create_function('$a, $b', 'return strlen($b) - strlen($a);'));
$patterns = array();
foreach ($words as $word) {
- $patterns[] = '/' . preg_quote($word, '/') . '(?![^<]*>)/isuU';
+ $word = trim($word);
+ if ($word != '') {
+ $patterns[] = '/' . preg_quote($word, '/') . '(?![^<]*>)/isuU';
+ }
}
return preg_replace($patterns, '<span class="xh_find">$0</span>', $text);
}
Index: tests/unit/FunctionsTest.php
===================================================================
--- tests/unit/FunctionsTest.php (revision 1610)
+++ tests/unit/FunctionsTest.php (working copy)
@@ -735,6 +735,15 @@
. '</span>'
. "\xE0\xA4\xAE\xE0\xA4\xB5\xE0\xA4\xB8\xE0\xA4\xBE\xE0\xA4\xA6"
. "\xE0\xA4\xAF\xE0\xA5\x87\xE0\xA4\xA4\xE0\xA5\x8D"
+ ],
+ // empty search string (see http://cmsimpleforum.com/viewtopic.php?f=10&t=8789)
+ [[''], 'foo bar baz', 'foo bar baz'],
+ // searching for a space doesn't "highlight" spaces
+ [[' '], 'foo bar baz', 'foo bar baz'],
+ // search for same word twice
+ [
+ ['word', 'word'], 'foo word bar',
+ 'foo <span class="xh_find">word</span> bar'
]
];
}
Christoph M. Becker – Plugins for CMSimple_XH
Re: Search highlighting breaks for empty search string
Done (r1650, r1651).
Christoph M. Becker – Plugins for CMSimple_XH