Skip to content

Commit

Permalink
adding test and fix for hightlighting tags, old bug 2111 for 1.3 but …
Browse files Browse the repository at this point in the history
…its the same issue. This should make more options possible
  • Loading branch information
dogmatic69 committed Mar 16, 2012
1 parent 47a2c22 commit d3a4481
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/Cake/Test/Case/Utility/StringTest.php
Expand Up @@ -454,6 +454,11 @@ public function testHighlight() {
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>')); $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
$expected = '<b>This</b> is a test <b>text</b>'; $expected = '<b>This</b> is a test <b>text</b>';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);

$phrases = array('is', 'text');
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>', 'regex' => "|\b%s\b|iu"));
$expected = 'This <b>is</b> a test <b>text</b>';
$this->assertEquals($expected, $result);


$text = 'This is a test text'; $text = 'This is a test text';
$phrases = null; $phrases = null;
Expand Down
8 changes: 5 additions & 3 deletions lib/Cake/Utility/String.php
Expand Up @@ -361,6 +361,7 @@ public static function wrap($text, $options = array()) {
* *
* - `format` The piece of html with that the phrase will be highlighted * - `format` The piece of html with that the phrase will be highlighted
* - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted * - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
* - `regex` a custom regex rule that is ued to match words, default is '|$tag|iu'
* *
* @param string $text Text to search the phrase in * @param string $text Text to search the phrase in
* @param string $phrase The phrase that will be searched * @param string $phrase The phrase that will be searched
Expand All @@ -375,7 +376,8 @@ public static function highlight($text, $phrase, $options = array()) {


$default = array( $default = array(
'format' => '<span class="highlight">\1</span>', 'format' => '<span class="highlight">\1</span>',
'html' => false 'html' => false,
'regex' => "|%s|iu"
); );
$options = array_merge($default, $options); $options = array_merge($default, $options);
extract($options); extract($options);
Expand All @@ -391,7 +393,7 @@ public static function highlight($text, $phrase, $options = array()) {
} }


$with[] = (is_array($format)) ? $format[$key] : $format; $with[] = (is_array($format)) ? $format[$key] : $format;
$replace[] = "|$segment|iu"; $replace[] = sprintf($options['regex'], $segment);
} }


return preg_replace($replace, $with, $text); return preg_replace($replace, $with, $text);
Expand All @@ -401,7 +403,7 @@ public static function highlight($text, $phrase, $options = array()) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)"; $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
} }


return preg_replace("|$phrase|iu", $format, $text); return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
} }
} }


Expand Down

0 comments on commit d3a4481

Please sign in to comment.