Permalink
Browse files

Fixing issue where TextHelper::autoLinkUrls was failing on some expre…

…ssions
  • Loading branch information...
mariano committed Apr 13, 2010
1 parent 3fc212f commit a6b83cdd939cb4fbb39eed4971ed8952b7488478
Showing with 34 additions and 11 deletions.
  1. +4 −11 cake/libs/view/helpers/text.php
  2. +30 −0 cake/tests/cases/libs/view/helpers/text.test.php
@@ -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);
}
@@ -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);
}
/**

0 comments on commit a6b83cd

Please sign in to comment.