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 5ee09eb commit 0ac46f359f328736d0f5a4176024d190b05bfb1e @markstory markstory committed Oct 18, 2011
Showing with 7 additions and 2 deletions.
  1. +2 −2 cake/libs/view/helpers/text.php
  2. +5 −0 cake/tests/cases/libs/view/helpers/text.test.php
@@ -75,7 +75,7 @@ function highlight($text, $phrase, $options = array()) {
$with = array();
foreach ($phrase as $key => $segment) {
- $segment = "($segment)";
+ $segment = '(' . preg_quote($segment, '|') . ')';
if ($html) {
$segment = "(?![^<]+>)$segment(?![^<]+>)";
}
@@ -86,7 +86,7 @@ function highlight($text, $phrase, $options = array()) {
return preg_replace($replace, $with, $text);
} else {
- $phrase = "($phrase)";
+ $phrase = '(' . preg_quote($phrase, '|') . ')';
if ($html) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
@@ -114,6 +114,11 @@ function testHighlight() {
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
$this->assertEqual($result, $text);
+ $text = 'This is a (test) text';
+ $phrases = '(test';
+ $result = $this->Text->highlight($text, $phrases, array('format' => '<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 0ac46f3

Please sign in to comment.