Skip to content

Commit

Permalink
Fix broken search results highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
flodolo authored and pascalchevrel committed Apr 4, 2016
1 parent 3bc1a0a commit b80dfaf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/classes/Transvision/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,21 @@ public static function markString($needle, $haystack)
*/
public static function highlightString($str)
{
/*
[^←→]: matches any character but the marker characters.
[^←→]|(?R): matches any character but the marker characters,
or recursively the entire pattern.
This is wrapped in a non capturing group (?:), since we're
only interested in the content of the more external markers.
For example, for “←←A→dd→” we're only interested in “←A→dd”,
capturing “←A→” is not useful.
Internal extra markers are later removed by str_replace.
See also http://stackoverflow.com/a/14952740
*/
$str = preg_replace(
'/←(.*)→/isU',
'/←((?:[^←→]|(?R))*)→/iu',
"<span class='highlight'>$1</span>",
$str
);
Expand Down
1 change: 1 addition & 0 deletions tests/units/Transvision/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public function highlightStringDP()
['←Cronologia→ di navigazione', '<span class=\'highlight\'>Cronologia</span> di navigazione'],
['←servi←ce→→', '<span class=\'highlight\'>service</span>'],
['Cronologia di navigazione', 'Cronologia di navigazione'],
['←←A→dd→ more ←se←a→rch→ ←engine→s…', '<span class=\'highlight\'>Add</span> more <span class=\'highlight\'>search</span> <span class=\'highlight\'>engine</span>s…'],
];
}

Expand Down

0 comments on commit b80dfaf

Please sign in to comment.