Permalink
Browse files

Fix missing preg_quote around highlight searches.

Highlight strings should be literal values not regexp fragments.
Fixes #2111
  • Loading branch information...
1 parent 8947e92 commit b0f98948ce45b7b1b19a5822796e9701761d1350 @markstory markstory committed Oct 18, 2011
Showing with 7 additions and 4 deletions.
  1. +2 −4 cake/libs/view/helpers/text.php
  2. +5 −0 cake/tests/cases/libs/view/helpers/text.test.php
@@ -65,21 +65,19 @@ function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</s
foreach ($phrase as $key => $value) {
$key = $value;
$value = $highlighter;
- $key = '(' . $key . ')';
+ $key = '(' . preg_quote($key, '|') . ')';
if ($considerHtml) {
$key = '(?![^<]+>)' . $key . '(?![^<]+>)';
}
$replace[] = '|' . $key . '|iu';
$with[] = empty($value) ? $highlighter : $value;
}
-
return preg_replace($replace, $with, $text);
} else {
- $phrase = '(' . $phrase . ')';
+ $phrase = '(' . preg_quote($phrase, '|') . ')';
if ($considerHtml) {
$phrase = '(?![^<]+>)' . $phrase . '(?![^<]+>)';
}
-
return preg_replace('|'.$phrase.'|iu', $highlighter, $text);
}
}
@@ -125,6 +125,11 @@ function testHighlight() {
$result = $this->Text->highlight($text, $phrases, '<b>\1</b>');
$this->assertEqual($result, $text);
+ $text = 'This is a (test) text';
+ $phrases = '(test';
+ $result = $this->Text->highlight($text, $phrases, '<b>\1</b>');
+ $this->assertEqual('This is a <b>(test</b>) text', $result);
+
$text = 'Ich saß in einem Café am Übergang';
$expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
$phrases = array('saß', 'café', 'übergang');

0 comments on commit b0f9894

Please sign in to comment.