UrlAutolinkParserTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Extension\Strikethrough\StrikethroughExtension;
  16. use League\CommonMark\MarkdownConverter;
  17. use PHPUnit\Framework\TestCase;
  18. final class UrlAutolinkParserTest extends TestCase
  19. {
  20. /**
  21. * @dataProvider dataProviderForAutolinkTests
  22. */
  23. public function testUrlAutolinks(string $input, string $expected): void
  24. {
  25. $environment = new Environment();
  26. $environment->addExtension(new CommonMarkCoreExtension());
  27. $environment->addExtension(new AutolinkExtension());
  28. $converter = new MarkdownConverter($environment);
  29. $this->assertEquals($expected, \trim((string) $converter->convert($input)));
  30. }
  31. /**
  32. * @return iterable<array<mixed>>
  33. */
  34. public function dataProviderForAutolinkTests(): iterable
  35. {
  36. // Basic examples
  37. yield ['You can search on http://google.com for stuff.', '<p>You can search on <a href="http://google.com">http://google.com</a> for stuff.</p>'];
  38. yield ['https://google.com', '<p><a href="https://google.com">https://google.com</a></p>'];
  39. yield ['https://sub_domain.example.com', '<p><a href="https://sub_domain.example.com">https://sub_domain.example.com</a></p>'];
  40. yield ['ftp://example.com', '<p><a href="ftp://example.com">ftp://example.com</a></p>'];
  41. yield ['www.google.com', '<p><a href="http://www.google.com">www.google.com</a></p>'];
  42. yield [' http://leadingwhitespace.example.com', '<p><a href="http://leadingwhitespace.example.com">http://leadingwhitespace.example.com</a></p>'];
  43. yield ['http://trailingwhitespace.example.com ', '<p><a href="http://trailingwhitespace.example.com">http://trailingwhitespace.example.com</a></p>'];
  44. yield ['- https://example.com/list-item', "<ul>\n<li>\n<a href=\"https://example.com/list-item\">https://example.com/list-item</a>\n</li>\n</ul>"];
  45. // Tests of "incomplete" URLs
  46. yield ['google.com is missing www and/or a protocol', '<p>google.com is missing www and/or a protocol</p>'];
  47. yield ['http:/google.com is missing a slash', '<p>http:/google.com is missing a slash</p>'];
  48. yield ['javascript:alert(0); doesn\'t match the supported protocols', '<p>javascript:alert(0); doesn\'t match the supported protocols</p>'];
  49. // Tests involving trailing characters
  50. yield ['Maybe you\'re interested in https://www.google.com/search?q=php+commonmark!', '<p>Maybe you\'re interested in <a href="https://www.google.com/search?q=php+commonmark">https://www.google.com/search?q=php+commonmark</a>!</p>'];
  51. yield ['Or perhaps you\'re looking for my personal website https://www.colinodell.com...?', '<p>Or perhaps you\'re looking for my personal website <a href="https://www.colinodell.com">https://www.colinodell.com</a>...?</p>'];
  52. yield ['Check https://www.stackoverflow.com: they have all the answers', '<p>Check <a href="https://www.stackoverflow.com">https://www.stackoverflow.com</a>: they have all the answers</p>'];
  53. yield ['- https://example.com/list-item-with-trailing-colon:', "<ul>\n<li>\n<a href=\"https://example.com/list-item-with-trailing-colon\">https://example.com/list-item-with-trailing-colon</a>:</li>\n</ul>"];
  54. yield ['Visit www.commonmark.org.', '<p>Visit <a href="http://www.commonmark.org">www.commonmark.org</a>.</p>'];
  55. yield ['Visit www.commonmark.org/a.b.', '<p>Visit <a href="http://www.commonmark.org/a.b">www.commonmark.org/a.b</a>.</p>'];
  56. // Tests involving parentheses
  57. yield ['www.google.com/search?q=Markup+(business)', '<p><a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a></p>'];
  58. yield ['www.google.com/search?q=Markup+(business)))', '<p><a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a>))</p>'];
  59. yield ['(www.google.com/search?q=Markup+(business))', '<p>(<a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a>)</p>'];
  60. yield ['(www.google.com/search?q=Markup+(business)', '<p>(<a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a></p>'];
  61. yield ['www.google.com/search?q=(business))+ok', '<p><a href="http://www.google.com/search?q=(business))+ok">www.google.com/search?q=(business))+ok</a></p>'];
  62. yield ['(https://www.example.com/test).', '<p>(<a href="https://www.example.com/test">https://www.example.com/test</a>).</p>'];
  63. // Tests involving semi-colon endings
  64. yield ['www.google.com/search?q=commonmark&hl=en', '<p><a href="http://www.google.com/search?q=commonmark&amp;hl=en">www.google.com/search?q=commonmark&amp;hl=en</a></p>'];
  65. yield ['www.google.com/search?q=commonmark&hl;', '<p><a href="http://www.google.com/search?q=commonmark">www.google.com/search?q=commonmark</a>&amp;hl;</p>'];
  66. // Test that < immediately terminates an autolink
  67. yield ['www.commonmark.org/he<lp', '<p><a href="http://www.commonmark.org/he">www.commonmark.org/he</a>&lt;lp</p>'];
  68. // Regression: two links with one underscore each
  69. yield ["https://eventum.example.net/history.php?iss_id=107092\nhttps://gitlab.example.net/group/project/merge_requests/39#note_150630", "<p><a href=\"https://eventum.example.net/history.php?iss_id=107092\">https://eventum.example.net/history.php?iss_id=107092</a>\n<a href=\"https://gitlab.example.net/group/project/merge_requests/39#note_150630\">https://gitlab.example.net/group/project/merge_requests/39#note_150630</a></p>"];
  70. // Regression: CommonMark autolinks should not be double-linked
  71. yield ['<https://www.google.com>', '<p><a href="https://www.google.com">https://www.google.com</a></p>'];
  72. yield ['[www.google.com](https://www.google.com)', '<p><a href="https://www.google.com">www.google.com</a></p>'];
  73. yield ['[search on **www.google.com**](https://www.google.com)', '<p><a href="https://www.google.com">search on <strong>www.google.com</strong></a></p>'];
  74. // Issue 492: underscores in URLs (see https://github.com/thephpleague/commonmark/issues/492)
  75. yield ['http://wiki/Puncutation_in_links:_why_its_bad_(and_should_be_avoided)', '<p><a href="http://wiki/Puncutation_in_links:_why_its_bad_(and_should_be_avoided)">http://wiki/Puncutation_in_links:_why_its_bad_(and_should_be_avoided)</a></p>'];
  76. }
  77. public function testUrlAutolinksWithStrikethrough(): void
  78. {
  79. $markdown = '~~Prefix i link: https://aws.amazon.com/emr/features/hadoop/~~';
  80. $environment = new Environment();
  81. $environment->addExtension(new CommonMarkCoreExtension());
  82. $environment->addExtension(new AutolinkExtension());
  83. $environment->addExtension(new StrikethroughExtension());
  84. $converter = new MarkdownConverter($environment);
  85. $html = $converter->convert($markdown)->getContent();
  86. $this->assertSame(
  87. '<p><del>Prefix i link: <a href="https://aws.amazon.com/emr/features/hadoop/">https://aws.amazon.com/emr/features/hadoop/</a></del></p>' . "\n",
  88. $html
  89. );
  90. }
  91. }