diff --git a/cake/libs/view/helpers/text.php b/cake/libs/view/helpers/text.php
index d6f88d4fc..477c6de4f 100644
--- a/cake/libs/view/helpers/text.php
+++ b/cake/libs/view/helpers/text.php
@@ -46,43 +46,51 @@ class TextHelper extends AppHelper {
* Highlights a given phrase in a text. You can specify any expression in highlighter that
* may include the \1 expression to include the $phrase found.
*
+ * Options:
+ *
+ * - '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
+ *
* @param string $text Text to search the phrase in
* @param string $phrase The phrase that will be searched
- * @param string $highlighter The piece of html with that the phrase will be highlighted
- * @param boolean $considerHtml If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
+ * @param array $options An array of html attributes and options.
* @return string The highlighted text
* @access public
*/
- function highlight($text, $phrase, $highlighter = '\1', $considerHtml = false) {
+ function highlight($text, $phrase, $options = array()) {
if (empty($phrase)) {
return $text;
}
- if (is_array($phrase)) {
+ $default = array(
+ 'format' => '\1',
+ 'html' => false
+ );
+ $options = array_merge($default, $options);
+ extract($options);
+ if (is_array($phrase)) {
$replace = array();
$with = array();
foreach ($phrase as $key => $segment) {
$segment = "($segment)";
-
- if ($considerHtml) {
+ if ($html) {
$segment = "(?![^<]+>)$segment(?![^<]+>)";
}
- $with[] = (is_array($highlighter)) ? $highlighter[$key] : $highlighter;
+ $with[] = (is_array($format)) ? $format[$key] : $format;
$replace[] = "|$segment|iu";
}
return preg_replace($replace, $with, $text);
-
} else {
$phrase = "($phrase)";
- if ($considerHtml) {
+ if ($html) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
- return preg_replace("|$phrase|iu", $highlighter, $text);
+ return preg_replace("|$phrase|iu", $format, $text);
}
}
@@ -160,24 +168,32 @@ class TextHelper extends AppHelper {
* Cuts a string to the length of $length and replaces the last characters
* with the ending if the text is longer than length.
*
+ * Options:
+ *
+ * - 'ending' Will be used as Ending and appended to the trimmed string
+ * - 'exact' If false, $text will not be cut mid-word
+ * - 'html' If true, HTML tags would be handled correctly
+ *
* @param string $text String to truncate.
* @param integer $length Length of returned string, including ellipsis.
- * @param mixed $ending If string, will be used as Ending and appended to the trimmed string. Can also be an associative array that can contain the last three params of this method.
- * @param boolean $exact If false, $text will not be cut mid-word
- * @param boolean $considerHtml If true, HTML tags would be handled correctly
+ * @param array $options An array of html attributes and options.
* @return string Trimmed string.
*/
- function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) {
- if (is_array($ending)) {
- extract($ending);
- }
- if ($considerHtml) {
+ function truncate($text, $length = 100, $options = array()) {
+ $default = array(
+ 'ending' => '...', 'exact' => true, 'html' => false
+ );
+ $options = array_merge($default, $options);
+ extract($options);
+
+ if ($html) {
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
return $text;
}
$totalLength = mb_strlen($ending);
$openTags = array();
$truncate = '';
+
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
foreach ($tags as $tag) {
if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
@@ -217,7 +233,6 @@ class TextHelper extends AppHelper {
break;
}
}
-
} else {
if (mb_strlen($text) <= $length) {
return $text;
@@ -228,7 +243,7 @@ class TextHelper extends AppHelper {
if (!$exact) {
$spacepos = mb_strrpos($truncate, ' ');
if (isset($spacepos)) {
- if ($considerHtml) {
+ if ($html) {
$bits = mb_substr($truncate, $spacepos);
preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
if (!empty($droppedTags)) {
@@ -242,10 +257,9 @@ class TextHelper extends AppHelper {
$truncate = mb_substr($truncate, 0, $spacepos);
}
}
-
$truncate .= $ending;
- if ($considerHtml) {
+ if ($html) {
foreach ($openTags as $tag) {
$truncate .= ''.$tag.'>';
}
@@ -276,9 +290,9 @@ class TextHelper extends AppHelper {
* @return string Modified string
* @access public
*/
- function excerpt($text, $phrase, $radius = 100, $ending = "...") {
+ function excerpt($text, $phrase, $radius = 100, $ending = '...') {
if (empty($text) or empty($phrase)) {
- return $this->truncate($text, $radius * 2, $ending);
+ return $this->truncate($text, $radius * 2, array('ending' => $ending));
}
$phraseLen = mb_strlen($phrase);
diff --git a/cake/tests/cases/libs/view/helpers/text.test.php b/cake/tests/cases/libs/view/helpers/text.test.php
index 748754073..294c6a17e 100644
--- a/cake/tests/cases/libs/view/helpers/text.test.php
+++ b/cake/tests/cases/libs/view/helpers/text.test.php
@@ -77,25 +77,24 @@ class TextHelperTest extends CakeTestCase {
$text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
$this->assertIdentical($this->Text->truncate($text1, 15), 'The quick br...');
- $this->assertIdentical($this->Text->truncate($text1, 15, '...', false), 'The quick...');
+ $this->assertIdentical($this->Text->truncate($text1, 15, array('exact' => false)), 'The quick...');
$this->assertIdentical($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
- $this->assertIdentical($this->Text->truncate($text2, 10, '...'), 'Heiz&ou...');
- $this->assertIdentical($this->Text->truncate($text2, 10, '...', false), '...');
+ $this->assertIdentical($this->Text->truncate($text2, 10), 'Heiz&ou...');
+ $this->assertIdentical($this->Text->truncate($text2, 10, array('exact' => false)), '...');
$this->assertIdentical($this->Text->truncate($text3, 20), '© 2005-20...');
$this->assertIdentical($this->Text->truncate($text4, 15), ' This image ...');
- $this->assertIdentical($this->Text->truncate($text4, 45, '...', true, true), '
This image tag is not XHTML conform!
But t...');
- $this->assertIdentical($this->Text->truncate($text4, 90, '...', true, true), ' This image tag is not XHTML conform!
But the following image tag should be conform
Grea...');
- $this->assertIdentical($this->Text->truncate($text5, 6, '', true, true), '012345');
- $this->assertIdentical($this->Text->truncate($text5, 20, '', true, true), $text5);
- $this->assertIdentical($this->Text->truncate($text6, 57, '...', false, true), "
Extra dates have been announced for this year's...
"); + $this->assertIdentical($this->Text->truncate($text5, 6, array('ending' => '')), '01<'); + $this->assertIdentical($this->Text->truncate($text1, 15, array('html' => true)), 'The quick br...'); + $this->assertIdentical($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick...'); + $this->assertIdentical($this->Text->truncate($text2, 10, array('html' => true)), 'Heizölr...'); + $this->assertIdentical($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), '...'); + $this->assertIdentical($this->Text->truncate($text3, 20, array('html' => true)), '© 2005-2007, Cake...'); + $this->assertIdentical($this->Text->truncate($text4, 15, array('html' => true)), 'Extra dates have been announced for this year's...
"); $this->assertIdentical($this->Text->truncate($text7, 255), $text7); $this->assertIdentical($this->Text->truncate($text7, 15), 'El moño está...'); $this->assertIdentical($this->Text->truncate($text8, 15), 'Vive la R'.chr(195).chr(169).'pu...'); @@ -111,46 +110,47 @@ class TextHelperTest extends CakeTestCase { function testHighlight() { $text = 'This is a test text'; $phrases = array('This', 'text'); - $result = $this->Text->highlight($text, $phrases, '\1'); + $result = $this->Text->highlight($text, $phrases, array('format' => '\1')); $expected = 'This is a test text'; $this->assertEqual($expected, $result); $text = 'This is a test text'; $phrases = null; - $result = $this->Text->highlight($text, $phrases, '\1'); + $result = $this->Text->highlight($text, $phrases, array('format' => '\1')); $this->assertEqual($result, $text); $text = 'Ich saß in einem Café am Übergang'; $expected = 'Ich saß in einem Café am Übergang'; $phrases = array('saß', 'café', 'übergang'); - $result = $this->Text->highlight($text, $phrases, '\1'); + $result = $this->Text->highlight($text, $phrases, array('format' => '\1')); $this->assertEqual($result, $expected); } /** - * testHighlightConsiderHtml method + * testHighlightHtml method * * @access public * @return void */ - function testHighlightConsiderHtml() { + function testHighlightHtml() { $text1 = 'strongbow isn’t real cider
'; $text2 = 'strongbow isn’t real cider
'; $text3 = 'strongbow isn’t real cider
'; - $this->assertEqual($this->Text->highlight($text1, 'strong', '\1', true), $expected); + $this->assertEqual($this->Text->highlight($text1, 'strong', $options), $expected); $expected = 'strongbow isn’t real cider
'; - $this->assertEqual($this->Text->highlight($text2, 'strong', '\1', true), $expected); + $this->assertEqual($this->Text->highlight($text2, 'strong', $options), $expected); - $this->assertEqual($this->Text->highlight($text3, 'strong', '\1', true), $text3); + $this->assertEqual($this->Text->highlight($text3, 'strong', $options), $text3); - $this->assertEqual($this->Text->highlight($text3, array('strong', 'what'), '\1', true), $text3); + $this->assertEqual($this->Text->highlight($text3, array('strong', 'what'), $options), $text3); $expected = 'What a strong mouse: