diff --git a/cake/libs/view/helpers/text.php b/cake/libs/view/helpers/text.php index f8d79b1bad2..7720ab43099 100644 --- a/cake/libs/view/helpers/text.php +++ b/cake/libs/view/helpers/text.php @@ -42,6 +42,13 @@ */ class TextHelper extends AppHelper { +/** + * helpers + * + * @var array + */ + public $helpers = array('Html'); + /** * 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. @@ -118,12 +125,50 @@ public function stripLinks($text) { * @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619 */ public function autoLinkUrls($text, $htmlOptions = array()) { - $options = var_export($htmlOptions, true); - $text = preg_replace_callback('#(?)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches', - '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text); + $this->_linkOptions = $htmlOptions; + $text = preg_replace_callback( + '#(?)((?:https?|ftp|nntp)://[^\s<>()]+)#i', + array(&$this, '_linkBareUrl'), + $text + ); + return preg_replace_callback( + '#(?)(?)(?tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'), $text); +/** + * Links urls that include http:// + * + * @param array $matches + * @return string + * @see TextHelper::autoLinkUrls() + */ + private function _linkBareUrl($matches) { + return $this->Html->link($matches[0], $matches[0], $this->_linkOptions); + } + +/** + * Links urls missing http:// + * + * @param array $matches + * @return string + * @see TextHelper::autoLinkUrls() + */ + private function _linkUrls($matches) { + return $this->Html->link($matches[0], 'http://' . $matches[0], $this->_linkOptions); + } + +/** + * Links email addresses + * + * @param array $matches + * @return string + * @see TextHelper::autoLinkUrls() + */ + private function _linkEmails($matches) { + return $this->Html->link($matches[0], 'mailto:' . $matches[0], $this->_linkOptions); } /** @@ -136,15 +181,12 @@ public function autoLinkUrls($text, $htmlOptions = array()) { * @link http://book.cakephp.org/view/1469/Text#autoLinkEmails-1618 */ public function autoLinkEmails($text, $options = array()) { - $linkOptions = 'array('; - foreach ($options as $option => $value) { - $value = var_export($value, true); - $linkOptions .= "'$option' => $value, "; - } - $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],' . $linkOptions . ');'), $text); + $this->_linkOptions = $options; + return preg_replace_callback( + '#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#', + array(&$this, '_linkEmails'), + $text + ); } /** diff --git a/cake/tests/cases/libs/view/helpers/text.test.php b/cake/tests/cases/libs/view/helpers/text.test.php index 62dd33d1480..139b5f46f2a 100644 --- a/cake/tests/cases/libs/view/helpers/text.test.php +++ b/cake/tests/cases/libs/view/helpers/text.test.php @@ -17,6 +17,7 @@ * @since CakePHP(tm) v 1.2.0.4206 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ +App::import('Core', 'View'); App::import('Helper', 'Text'); /**