Browse Source

Merge pull request #5555 from cakephp/text-helper-port

Forward port #5544 to 3.0.
Mark Story 11 years ago
parent
commit
40ccd5472c
2 changed files with 29 additions and 1 deletions
  1. 4 1
      src/View/Helper/TextHelper.php
  2. 25 0
      tests/TestCase/View/Helper/TextHelperTest.php

+ 4 - 1
src/View/Helper/TextHelper.php

@@ -117,7 +117,10 @@ class TextHelper extends Helper
         $this->_placeholders = [];
         $options += ['escape' => true];
 
-        $pattern = '#(?<!href="|src="|">)((?:https?|ftp|nntp)://[\p{L}0-9.\-_:]+(?:[/?][^\s<]*)?)#ui';
+
+        $pattern = '#(?<!href="|src="|">)((?:https?|ftp|nntp)://[\p{L}0-9.\-_:]+' .
+            '(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+' .
+            '(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#i';
         $text = preg_replace_callback(
             $pattern,
             [&$this, '_insertPlaceHolder'],

+ 25 - 0
tests/TestCase/View/Helper/TextHelperTest.php

@@ -174,6 +174,31 @@ class TextHelperTest extends TestCase
         $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->assertEquals($expected, $result);
+
+        $text = 'This is a test text with URL (http://www.cakephp.org/page/4) in brackets';
+        $expected = 'This is a test text with URL (<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>) in brackets';
+        $result = $this->Text->autoLink($text);
+        $this->assertEquals($expected, $result);
+
+        $text = 'This is a test text with URL [http://www.cakephp.org/page/4] in square brackets';
+        $expected = 'This is a test text with URL [<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>] in square brackets';
+        $result = $this->Text->autoLink($text);
+        $this->assertEquals($expected, $result);
+
+        $text = 'This is a test text with URL [http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3] in square brackets';
+        $expected = 'This is a test text with URL [<a href="http://www.example.com?aParam[]=value1&amp;aParam[]=value2&amp;aParam[]=value3">http://www.example.com?aParam[]=value1&amp;aParam[]=value2&amp;aParam[]=value3</a>] in square brackets';
+        $result = $this->Text->autoLink($text);
+        $this->assertEquals($expected, $result);
+
+        $text = 'This is a test text with URL ;http://www.cakephp.org/page/4; semi-colon';
+        $expected = 'This is a test text with URL ;<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>; semi-colon';
+        $result = $this->Text->autoLink($text);
+        $this->assertEquals($expected, $result);
+
+        $text = 'This is a test text with URL (http://www.cakephp.org/page/4/other(thing)) brackets';
+        $expected = 'This is a test text with URL (<a href="http://www.cakephp.org/page/4/other(thing)">http://www.cakephp.org/page/4/other(thing)</a>) brackets';
+        $result = $this->Text->autoLink($text);
+        $this->assertEquals($expected, $result);
     }
 
     /**