Fixing issue where TextHelper::autoLinkUrls was failing on some expressions

This commit is contained in:
Mariano Iglesias 2010-04-13 15:33:24 -03:00
parent 3fc212fe28
commit a6b83cdd93
2 changed files with 34 additions and 11 deletions

View file

@ -117,17 +117,10 @@ class TextHelper extends AppHelper {
* @access public
* @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619
*/
function autoLinkUrls($text, $options = array()) {
$linkOptions = 'array(';
foreach ($options as $option => $value) {
$value = var_export($value, true);
$linkOptions .= "'$option' => $value, ";
}
$linkOptions .= ')';
$text = preg_replace_callback('#(?<!href="|">)((?:http|https|ftp|nntp)://[^ <]+)#i', create_function('$matches',
'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $linkOptions . ');'), $text);
function autoLinkUrls($text, $htmlOptions = array()) {
$options = var_export($htmlOptions, true);
$text = preg_replace_callback('#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches',
'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . strtolower($matches[0]),' . $linkOptions . ');'), $text);
}

View file

@ -207,6 +207,36 @@ class TextHelperTest extends CakeTestCase {
$result = $this->Text->autoLink($text);
$expected = 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL and <a href="mailto:test@cakephp\.org">test@cakephp\.org</a> email address';
$this->assertPattern('#^' . $expected . '$#', $result);
$text = 'This is a test text with URL http://www.cakephp.org';
$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
$result = $this->Text->autoLink($text);
$this->assertEqual($result, $expected);
$text = 'This is a test text with URL http://www.cakephp.org and some more text';
$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
$result = $this->Text->autoLink($text);
$this->assertEqual($result, $expected);
$text = "This is a test text with URL http://www.cakephp.org\tand some more text";
$expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
$result = $this->Text->autoLink($text);
$this->assertEqual($result, $expected);
$text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
$result = $this->Text->autoLink($text);
$this->assertEqual($result, $expected);
$text = 'This is a test text with URL http://www.cakephp.org';
$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
$result = $this->Text->autoLink($text, array('class'=>'link'));
$this->assertEqual($result, $expected);
$text = 'This is a test text with URL http://www.cakephp.org';
$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
$result = $this->Text->autoLink($text, array('class'=>'link', 'id'=>'MyLink'));
$this->assertEqual($result, $expected);
}
/**