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 8103581 commit 864ea3d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
10 changes: 2 additions & 8 deletions cake/libs/view/helpers/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,8 @@ function stripLinks($text) {
* @access public
*/
function autoLinkUrls($text, $htmlOptions = array()) {
$options = 'array(';
foreach ($htmlOptions as $option => $value) {
$value = var_export($value, true);
$options .= "'$option' => $value, ";
}
$options .= ')';

$text = preg_replace_callback('#(?<!href="|">)((?:http|https|ftp|nntp)://[^ <]+)#i', create_function('$matches',
$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',
Expand Down
40 changes: 35 additions & 5 deletions cake/tests/cases/libs/view/helpers/text.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function testTruncate() {
$this->assertIdentical($this->Text->{$m}($text8, 15), 'Vive la R'.chr(195).chr(169).'pu...');
$this->assertIdentical($this->Text->{$m}($text1, 25, 'Read more'), 'The quick brown Read more');
$this->assertIdentical($this->Text->{$m}($text1, 25, '<a href="http://www.google.com/">Read more</a>', true, true), 'The quick brown <a href="http://www.google.com/">Read more</a>');

if ($this->method == 'truncate') {
$this->method = 'trim';
$this->testTruncate();
Expand Down Expand Up @@ -199,6 +199,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);
}
/**
* testAutoLinkUrls method
Expand Down Expand Up @@ -288,19 +318,19 @@ function testExcerpt() {
$expected = '...with test text...';
$result = $this->Text->excerpt($text, 'test', 9, '...');
$this->assertEqual($expected, $result);

$expected = 'This is a...';
$result = $this->Text->excerpt($text, 'not_found', 9, '...');
$this->assertEqual($expected, $result);

$expected = 'This is a phras...';
$result = $this->Text->excerpt($text, null, 9, '...');
$this->assertEqual($expected, $result);

$expected = $text;
$result = $this->Text->excerpt($text, null, 200, '...');
$this->assertEqual($expected, $result);

$expected = '...phrase...';
$result = $this->Text->excerpt($text, 'phrase', 2, '...');
$this->assertEqual($expected, $result);
Expand Down

0 comments on commit 864ea3d

Please sign in to comment.