From e7074c1e5ce9bc9cea0ef8427a245cb91ad578f3 Mon Sep 17 00:00:00 2001 From: "renan.saddam" Date: Thu, 29 Oct 2009 01:23:44 -0200 Subject: [PATCH 1/2] Moving parameters to $options on Text::truncate() and Text::highlight(). --- cake/libs/view/helpers/text.php | 62 ++++++++++++------- .../cases/libs/view/helpers/text.test.php | 58 ++++++++--------- 2 files changed, 67 insertions(+), 53 deletions(-) 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 .= ''; } @@ -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 Me, myself and I
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)), ' This image ...'); + $this->assertIdentical($this->Text->truncate($text4, 45, array('html' => true)), ' This image tag is not XHTML conform!

But t...'); + $this->assertIdentical($this->Text->truncate($text4, 90, array('html' => true)), ' This image tag is not XHTML conform!

But the following image tag should be conform Me, myself and I
Grea...'); + $this->assertIdentical($this->Text->truncate($text5, 6, array('ending' => '', 'html' => true)), '012345'); + $this->assertIdentical($this->Text->truncate($text5, 20, array('ending' => '', 'html' => true)), $text5); + $this->assertIdentical($this->Text->truncate($text6, 57, array('exact' => false, '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 = 'What a strong mouse!'; $text4 = 'What a strong mouse: What a strong mouse!'; + $options = array('format' => '\1', 'html' => true); $expected = '

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: What a strong mouse!'; - $this->assertEqual($this->Text->highlight($text4, array('strong', 'what'), '\1', true), $expected); + $this->assertEqual($this->Text->highlight($text4, array('strong', 'what'), $options), $expected); } /** @@ -162,7 +162,7 @@ class TextHelperTest extends CakeTestCase { function testHighlightMulti() { $text = 'This is a test text'; $phrases = array('This', 'text'); - $result = $this->Text->highlight($text, $phrases, array('\1', '\1')); + $result = $this->Text->highlight($text, $phrases, array('format' => array('\1', '\1'))); $expected = 'This is a test text'; $this->assertEqual($expected, $result); @@ -286,10 +286,10 @@ class TextHelperTest extends CakeTestCase { $text = 'This is a Test text'; $expected = 'This is a Test text'; - $result = $this->Text->highlight($text, 'test', '\1'); + $result = $this->Text->highlight($text, 'test', array('format' => '\1')); $this->assertEqual($expected, $result); - $result = $this->Text->highlight($text, array('test'), '\1'); + $result = $this->Text->highlight($text, array('test'), array('format' => '\1')); $this->assertEqual($expected, $result); } From 61845090ed288af7842ed6ead75e754c76d6183f Mon Sep 17 00:00:00 2001 From: "renan.saddam" Date: Thu, 29 Oct 2009 01:31:55 -0200 Subject: [PATCH 2/2] Changing variable name to make consistency between others methods. --- cake/libs/view/helpers/text.php | 37 ++++++++++++++++----------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/cake/libs/view/helpers/text.php b/cake/libs/view/helpers/text.php index 477c6de4f..6159c4962 100644 --- a/cake/libs/view/helpers/text.php +++ b/cake/libs/view/helpers/text.php @@ -110,56 +110,55 @@ class TextHelper extends AppHelper { * strings like http:// and ftp://. * * @param string $text Text to add links to - * @param array $htmlOptions Array of HTML options. + * @param array $options Array of HTML options. * @return string The text with links * @access public */ - function autoLinkUrls($text, $htmlOptions = array()) { - $options = 'array('; - foreach ($htmlOptions as $option => $value) { + function autoLinkUrls($text, $options = array()) { + $linkOptions = 'array('; + foreach ($options as $option => $value) { $value = var_export($value, true); - $options .= "'$option' => $value, "; + $linkOptions .= "'$option' => $value, "; } - $options .= ')'; + $linkOptions .= ')'; $text = preg_replace_callback('#(?)((?:http|https|ftp|nntp)://[^ <]+)#i', create_function('$matches', - '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text); + '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $linkOptions . ');'), $text); return preg_replace_callback('#(?)(?tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . strtolower($matches[0]),' . $options . ');'), $text); + create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . strtolower($matches[0]),' . $linkOptions . ');'), $text); } /** * Adds email links ( $value, "; + $linkOptions .= "'$option' => $value, "; } - $options .= ')'; + $linkOptions .= ')'; return preg_replace_callback('#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#', - create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $options . ');'), $text); + create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $linkOptions . ');'), $text); } /** * Convert all links and email adresses to HTML links. * * @param string $text Text - * @param array $htmlOptions Array of HTML options. + * @param array $options Array of HTML options. * @return string The text with links * @access public */ - function autoLink($text, $htmlOptions = array()) { - return $this->autoLinkEmails($this->autoLinkUrls($text, $htmlOptions), $htmlOptions); + function autoLink($text, $options = array()) { + return $this->autoLinkEmails($this->autoLinkUrls($text, $options), $options); } /**