Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
markstory committed Oct 18, 2011
1 parent 01992dd commit 7f0f224
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/Cake/Test/Case/View/Helper/TextHelperTest.php
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/View/Helper/TextHelper.php
Expand Up @@ -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(?![^<]+>)";
}
Expand All @@ -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(?![^<]+>)";
}
Expand Down

0 comments on commit 7f0f224

Please sign in to comment.