Permalink
Browse files

adding test and fix for hightlighting tags, old bug 2111 for 1.3 but …

…its the same issue. This should make more options possible
  • Loading branch information...
dogmatic69 committed Mar 16, 2012
1 parent 47a2c22 commit d3a44811ce70e6d9777d193e6ae5e001c7146237
Showing with 10 additions and 3 deletions.
  1. +5 −0 lib/Cake/Test/Case/Utility/StringTest.php
  2. +5 −3 lib/Cake/Utility/String.php
@@ -454,6 +454,11 @@ public function testHighlight() {
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
$expected = '<b>This</b> is a test <b>text</b>';
$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';
$phrases = null;
@@ -361,6 +361,7 @@ public static function wrap($text, $options = array()) {
*
* - `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
+ * - `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 $phrase The phrase that will be searched
@@ -375,7 +376,8 @@ public static function highlight($text, $phrase, $options = array()) {
$default = array(
'format' => '<span class="highlight">\1</span>',
- 'html' => false
+ 'html' => false,
+ 'regex' => "|%s|iu"
);
$options = array_merge($default, $options);
extract($options);
@@ -391,7 +393,7 @@ public static function highlight($text, $phrase, $options = array()) {
}
$with[] = (is_array($format)) ? $format[$key] : $format;
- $replace[] = "|$segment|iu";
+ $replace[] = sprintf($options['regex'], $segment);
}
return preg_replace($replace, $with, $text);
@@ -401,7 +403,7 @@ public static function highlight($text, $phrase, $options = array()) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
- return preg_replace("|$phrase|iu", $format, $text);
+ return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
}
}

0 comments on commit d3a4481

Please sign in to comment.