Skip to content

Commit

Permalink
Fixing issue where TextHelper::autoLinkUrls was failing on some expre…
Browse files Browse the repository at this point in the history
…ssions
  • Loading branch information
mariano committed Apr 13, 2010
1 parent 3fc212f commit a6b83cd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
15 changes: 4 additions & 11 deletions cake/libs/view/helpers/text.php
Expand Up @@ -117,17 +117,10 @@ function stripLinks($text) {
* @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);
}
Expand Down
30 changes: 30 additions & 0 deletions cake/tests/cases/libs/view/helpers/text.test.php
Expand Up @@ -207,6 +207,36 @@ function testAutoLink() {
$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);
}

/**
Expand Down

0 comments on commit a6b83cd

Please sign in to comment.