mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Fixing issue where TextHelper::autoLinkUrls was failing on some expressions
This commit is contained in:
parent
3fc212fe28
commit
a6b83cdd93
2 changed files with 34 additions and 11 deletions
|
@ -117,17 +117,10 @@ class TextHelper extends AppHelper {
|
||||||
* @access public
|
* @access public
|
||||||
* @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619
|
* @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619
|
||||||
*/
|
*/
|
||||||
function autoLinkUrls($text, $options = array()) {
|
function autoLinkUrls($text, $htmlOptions = array()) {
|
||||||
$linkOptions = 'array(';
|
$options = var_export($htmlOptions, true);
|
||||||
foreach ($options as $option => $value) {
|
$text = preg_replace_callback('#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches',
|
||||||
$value = var_export($value, true);
|
'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
|
||||||
$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);
|
|
||||||
|
|
||||||
return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
|
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);
|
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 @@ class TextHelperTest extends CakeTestCase {
|
||||||
$result = $this->Text->autoLink($text);
|
$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';
|
$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);
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue