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
  • Loading branch information
markstory committed Oct 18, 2011
1 parent 8947e92 commit b0f9894
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 2 additions & 4 deletions cake/libs/view/helpers/text.php
Expand Up @@ -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);
}
}
Expand Down
5 changes: 5 additions & 0 deletions cake/tests/cases/libs/view/helpers/text.test.php
Expand Up @@ -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');
Expand Down

0 comments on commit b0f9894

Please sign in to comment.