Fixing TextHelper::autoLinkEmails() not linking email addresses that contain '.

Test added.
Fixes #1457
This commit is contained in:
mark_story 2011-01-17 11:11:30 -05:00
parent ca299a097c
commit b0d4951184
2 changed files with 9 additions and 2 deletions

View file

@ -142,9 +142,11 @@ class TextHelper extends AppHelper {
$linkOptions .= "'$option' => $value, "; $linkOptions .= "'$option' => $value, ";
} }
$linkOptions .= ')'; $linkOptions .= ')';
$atom = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]';
return preg_replace_callback('#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#', return preg_replace_callback(
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $linkOptions . ');'), $text); '/(' . $atom . '+(?:\.' . $atom . '+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)*)/i',
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $linkOptions . ');'), $text);
} }
/** /**

View file

@ -299,6 +299,11 @@ class TextHelperTest extends CakeTestCase {
$result = $this->Text->autoLinkEmails($text); $result = $this->Text->autoLinkEmails($text);
$this->assertPattern('#^' . $expected . '$#', $result); $this->assertPattern('#^' . $expected . '$#', $result);
$text = "Text with o'hare._-bob@example.com address";
$expected = 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address';
$result = $this->Text->autoLinkEmails($text);
$this->assertEqual($expected, $result);
$text = 'Text with email@example.com address'; $text = 'Text with email@example.com address';
$expected = 'Text with <a href="mailto:email@example.com" \s*class="link">email@example.com</a> address'; $expected = 'Text with <a href="mailto:email@example.com" \s*class="link">email@example.com</a> address';
$result = $this->Text->autoLinkEmails($text, array('class' => 'link')); $result = $this->Text->autoLinkEmails($text, array('class' => 'link'));