Skip to content

Commit

Permalink
Fix missing preg_quote around highlight searches.
Browse files Browse the repository at this point in the history
Highlight strings should be literal values not regexp fragments.
Fixes #2111
  • Loading branch information
markstory committed Oct 18, 2011
1 parent 5ee09eb commit 0ac46f3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cake/libs/view/helpers/text.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function highlight($text, $phrase, $options = array()) {
$with = array(); $with = array();


foreach ($phrase as $key => $segment) { foreach ($phrase as $key => $segment) {
$segment = "($segment)"; $segment = '(' . preg_quote($segment, '|') . ')';
if ($html) { if ($html) {
$segment = "(?![^<]+>)$segment(?![^<]+>)"; $segment = "(?![^<]+>)$segment(?![^<]+>)";
} }
Expand All @@ -86,7 +86,7 @@ function highlight($text, $phrase, $options = array()) {


return preg_replace($replace, $with, $text); return preg_replace($replace, $with, $text);
} else { } else {
$phrase = "($phrase)"; $phrase = '(' . preg_quote($phrase, '|') . ')';
if ($html) { if ($html) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)"; $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
} }
Expand Down
5 changes: 5 additions & 0 deletions cake/tests/cases/libs/view/helpers/text.test.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ function testHighlight() {
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>')); $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
$this->assertEqual($result, $text); $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'; $text = 'Ich saß in einem Café am Übergang';
$expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>'; $expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
$phrases = array('saß', 'café', 'übergang'); $phrases = array('saß', 'café', 'übergang');
Expand Down

0 comments on commit 0ac46f3

Please sign in to comment.