Permalink
Browse files

Fix missing preg_quote around highlight searches.

Highlight strings should be literal values not regexp fragments.
Fixes #2111

Conflicts:

	cake/libs/view/helpers/text.php
	cake/tests/cases/libs/view/helpers/text.test.php
  • Loading branch information...
1 parent 01992dd commit 7f0f224c56fc1d3d1daba24a8fe5d15f10d52785 @markstory markstory committed Oct 18, 2011
Showing with 7 additions and 2 deletions.
  1. +5 −0 lib/Cake/Test/Case/View/Helper/TextHelperTest.php
  2. +2 −2 lib/Cake/View/Helper/TextHelper.php
@@ -104,6 +104,11 @@ public 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');
@@ -77,7 +77,7 @@ public function highlight($text, $phrase, $options = array()) {
$with = array();
foreach ($phrase as $key => $segment) {
- $segment = "($segment)";
+ $segment = '(' . preg_quote($segment, '|') . ')';
if ($html) {
$segment = "(?![^<]+>)$segment(?![^<]+>)";
}
@@ -88,7 +88,7 @@ public function highlight($text, $phrase, $options = array()) {
return preg_replace($replace, $with, $text);
} else {
- $phrase = "($phrase)";
+ $phrase = '(' . preg_quote($phrase, '|') . ')';
if ($html) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}

0 comments on commit 7f0f224

Please sign in to comment.