Skip to content

Commit b80dfaf

Browse files
flodolopascalchevrel
authored andcommitted
Fix broken search results highlighting
1 parent 3bc1a0a commit b80dfaf

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

app/classes/Transvision/Utils.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,21 @@ public static function markString($needle, $haystack)
119119
*/
120120
public static function highlightString($str)
121121
{
122+
/*
123+
[^←→]: matches any character but the marker characters.
124+
[^←→]|(?R): matches any character but the marker characters,
125+
or recursively the entire pattern.
126+
127+
This is wrapped in a non capturing group (?:), since we're
128+
only interested in the content of the more external markers.
129+
For example, for “←←A→dd→” we're only interested in “←A→dd”,
130+
capturing “←A→” is not useful.
131+
Internal extra markers are later removed by str_replace.
132+
133+
See also http://stackoverflow.com/a/14952740
134+
*/
122135
$str = preg_replace(
123-
'/←(.*)→/isU',
136+
'/←((?:[^←→]|(?R))*)→/iu',
124137
"<span class='highlight'>$1</span>",
125138
$str
126139
);

tests/units/Transvision/Utils.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public function highlightStringDP()
176176
['←Cronologia→ di navigazione', '<span class=\'highlight\'>Cronologia</span> di navigazione'],
177177
['←servi←ce→→', '<span class=\'highlight\'>service</span>'],
178178
['Cronologia di navigazione', 'Cronologia di navigazione'],
179+
['←←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…'],
179180
];
180181
}
181182

0 commit comments

Comments
 (0)