Skip to content
This repository
Browse code

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

…ssions
  • Loading branch information...
commit a6b83cdd939cb4fbb39eed4971ed8952b7488478 1 parent 3fc212f
Mariano Iglesias authored April 13, 2010
15  cake/libs/view/helpers/text.php
@@ -117,17 +117,10 @@ function stripLinks($text) {
117 117
  * @access public
118 118
  * @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619
119 119
  */
120  
-	function autoLinkUrls($text, $options = array()) {
121  
-		$linkOptions = 'array(';
122  
-		foreach ($options as $option => $value) {
123  
-			$value = var_export($value, true);
124  
-			$linkOptions .= "'$option' => $value, ";
125  
-		}
126  
-		$linkOptions .= ')';
127  
-
128  
-		$text = preg_replace_callback('#(?<!href="|">)((?:http|https|ftp|nntp)://[^ <]+)#i', create_function('$matches',
129  
-			'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $linkOptions . ');'), $text);
130  
-
  120
+	function autoLinkUrls($text, $htmlOptions = array()) {
  121
+		$options = var_export($htmlOptions, true);
  122
+		$text = preg_replace_callback('#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches',
  123
+			'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
131 124
 		return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
132 125
 			create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . strtolower($matches[0]),' . $linkOptions . ');'), $text);
133 126
 	}
30  cake/tests/cases/libs/view/helpers/text.test.php
@@ -207,6 +207,36 @@ function testAutoLink() {
207 207
 		$result = $this->Text->autoLink($text);
208 208
 		$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';
209 209
 		$this->assertPattern('#^' . $expected . '$#', $result);
  210
+
  211
+		$text = 'This is a test text with URL http://www.cakephp.org';
  212
+		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  213
+		$result = $this->Text->autoLink($text);
  214
+		$this->assertEqual($result, $expected);
  215
+
  216
+		$text = 'This is a test text with URL http://www.cakephp.org and some more text';
  217
+		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
  218
+		$result = $this->Text->autoLink($text);
  219
+		$this->assertEqual($result, $expected);
  220
+
  221
+		$text = "This is a test text with URL http://www.cakephp.org\tand some more text";
  222
+		$expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
  223
+		$result = $this->Text->autoLink($text);
  224
+		$this->assertEqual($result, $expected);
  225
+
  226
+		$text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
  227
+		$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
  228
+		$result = $this->Text->autoLink($text);
  229
+		$this->assertEqual($result, $expected);
  230
+
  231
+		$text = 'This is a test text with URL http://www.cakephp.org';
  232
+		$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
  233
+		$result = $this->Text->autoLink($text, array('class'=>'link'));
  234
+		$this->assertEqual($result, $expected);
  235
+
  236
+		$text = 'This is a test text with URL http://www.cakephp.org';
  237
+		$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
  238
+		$result = $this->Text->autoLink($text, array('class'=>'link', 'id'=>'MyLink'));
  239
+		$this->assertEqual($result, $expected);
210 240
 	}
211 241
 
212 242
 /**

0 notes on commit a6b83cd

Please sign in to comment.
Something went wrong with that request. Please try again.