EmailAutolinkParserTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Functional\Extension\Autolink;
  12. use League\CommonMark\Environment\Environment;
  13. use League\CommonMark\Extension\Autolink\AutolinkExtension;
  14. use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
  15. use League\CommonMark\MarkdownConverter;
  16. use PHPUnit\Framework\TestCase;
  17. final class EmailAutolinkParserTest extends TestCase
  18. {
  19. /**
  20. * @dataProvider dataProviderForEmailAutolinks
  21. */
  22. public function testEmailAutolinks(string $input, string $expected): void
  23. {
  24. $environment = new Environment();
  25. $environment->addExtension(new CommonMarkCoreExtension());
  26. $environment->addExtension(new AutolinkExtension());
  27. $converter = new MarkdownConverter($environment);
  28. $this->assertEquals($expected, \trim((string) $converter->convert($input)));
  29. }
  30. /**
  31. * @return iterable<array<mixed>>
  32. */
  33. public function dataProviderForEmailAutolinks(): iterable
  34. {
  35. yield ['You can try emailing foo@example.com but that inbox doesn\'t actually exist.', '<p>You can try emailing <a href="mailto:foo@example.com">foo@example.com</a> but that inbox doesn\'t actually exist.</p>'];
  36. yield ['> This processor can even handle email addresses like foo@example.com inside of blockquotes!', "<blockquote>\n<p>This processor can even handle email addresses like <a href=\"mailto:foo@example.com\">foo@example.com</a> inside of blockquotes!</p>\n</blockquote>"];
  37. yield ['@invalid', '<p>@invalid</p>'];
  38. // GFM spec tests
  39. yield ['foo@bar.baz', '<p><a href="mailto:foo@bar.baz">foo@bar.baz</a></p>'];
  40. yield ['hello@mail+xyz.example isn\'t valid, but hello+xyz@mail.example is.', '<p>hello@mail+xyz.example isn\'t valid, but <a href="mailto:hello+xyz@mail.example">hello+xyz@mail.example</a> is.</p>'];
  41. yield ['a.b-c_d@a.b', '<p><a href="mailto:a.b-c_d@a.b">a.b-c_d@a.b</a></p>'];
  42. yield ['a.b-c_d@a.b.', '<p><a href="mailto:a.b-c_d@a.b">a.b-c_d@a.b</a>.</p>'];
  43. yield ['a.b-c_d@a.b-', '<p>a.b-c_d@a.b-</p>'];
  44. yield ['a.b-c_d@a.b_', '<p>a.b-c_d@a.b_</p>'];
  45. // Regression: CommonMark autolinks should not be double-linked
  46. yield ['<foo@example.com>', '<p><a href="mailto:foo@example.com">foo@example.com</a></p>'];
  47. yield ['[me@mydomain.com](mailto:me@mydomain.com)', '<p><a href="mailto:me@mydomain.com">me@mydomain.com</a></p>'];
  48. yield ['[email **me@mydomain.com**](mailto:me@mydomain.com)', '<p><a href="mailto:me@mydomain.com">email <strong>me@mydomain.com</strong></a></p>'];
  49. }
  50. }