* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace League\CommonMark\Tests\Functional\Extension\Autolink; use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\Autolink\AutolinkExtension; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\Extension\Strikethrough\StrikethroughExtension; use League\CommonMark\MarkdownConverter; use PHPUnit\Framework\TestCase; final class UrlAutolinkParserTest extends TestCase { /** * @dataProvider dataProviderForAutolinkTests */ public function testUrlAutolinks(string $input, string $expected): void { $environment = new Environment(); $environment->addExtension(new CommonMarkCoreExtension()); $environment->addExtension(new AutolinkExtension()); $converter = new MarkdownConverter($environment); $this->assertEquals($expected, \trim((string) $converter->convert($input))); } /** * @return iterable> */ public function dataProviderForAutolinkTests(): iterable { // Basic examples yield ['You can search on http://google.com for stuff.', '

You can search on http://google.com for stuff.

']; yield ['https://google.com', '

https://google.com

']; yield ['https://sub_domain.example.com', '

https://sub_domain.example.com

']; yield ['ftp://example.com', '

ftp://example.com

']; yield ['www.google.com', '

www.google.com

']; yield [' http://leadingwhitespace.example.com', '

http://leadingwhitespace.example.com

']; yield ['http://trailingwhitespace.example.com ', '

http://trailingwhitespace.example.com

']; yield ['- https://example.com/list-item', ""]; // Tests of "incomplete" URLs yield ['google.com is missing www and/or a protocol', '

google.com is missing www and/or a protocol

']; yield ['http:/google.com is missing a slash', '

http:/google.com is missing a slash

']; yield ['javascript:alert(0); doesn\'t match the supported protocols', '

javascript:alert(0); doesn\'t match the supported protocols

']; // Tests involving trailing characters yield ['Maybe you\'re interested in https://www.google.com/search?q=php+commonmark!', '

Maybe you\'re interested in https://www.google.com/search?q=php+commonmark!

']; yield ['Or perhaps you\'re looking for my personal website https://www.colinodell.com...?', '

Or perhaps you\'re looking for my personal website https://www.colinodell.com...?

']; yield ['Check https://www.stackoverflow.com: they have all the answers', '

Check https://www.stackoverflow.com: they have all the answers

']; yield ['- https://example.com/list-item-with-trailing-colon:', ""]; yield ['Visit www.commonmark.org.', '

Visit www.commonmark.org.

']; yield ['Visit www.commonmark.org/a.b.', '

Visit www.commonmark.org/a.b.

']; // Tests involving parentheses yield ['www.google.com/search?q=Markup+(business)', '

www.google.com/search?q=Markup+(business)

']; yield ['www.google.com/search?q=Markup+(business)))', '

www.google.com/search?q=Markup+(business)))

']; yield ['(www.google.com/search?q=Markup+(business))', '

(www.google.com/search?q=Markup+(business))

']; yield ['(www.google.com/search?q=Markup+(business)', '

(www.google.com/search?q=Markup+(business)

']; yield ['www.google.com/search?q=(business))+ok', '

www.google.com/search?q=(business))+ok

']; yield ['(https://www.example.com/test).', '

(https://www.example.com/test).

']; // Tests involving semi-colon endings yield ['www.google.com/search?q=commonmark&hl=en', '

www.google.com/search?q=commonmark&hl=en

']; yield ['www.google.com/search?q=commonmark&hl;', '

www.google.com/search?q=commonmark&hl;

']; // Test that < immediately terminates an autolink yield ['www.commonmark.org/hewww.commonmark.org/he<lp

']; // Regression: two links with one underscore each yield ["https://eventum.example.net/history.php?iss_id=107092\nhttps://gitlab.example.net/group/project/merge_requests/39#note_150630", "

https://eventum.example.net/history.php?iss_id=107092\nhttps://gitlab.example.net/group/project/merge_requests/39#note_150630

"]; // Regression: CommonMark autolinks should not be double-linked yield ['', '

https://www.google.com

']; yield ['[www.google.com](https://www.google.com)', '

www.google.com

']; yield ['[search on **www.google.com**](https://www.google.com)', '

search on www.google.com

']; // Issue 492: underscores in URLs (see https://github.com/thephpleague/commonmark/issues/492) yield ['http://wiki/Puncutation_in_links:_why_its_bad_(and_should_be_avoided)', '

http://wiki/Puncutation_in_links:_why_its_bad_(and_should_be_avoided)

']; } public function testUrlAutolinksWithStrikethrough(): void { $markdown = '~~Prefix i link: https://aws.amazon.com/emr/features/hadoop/~~'; $environment = new Environment(); $environment->addExtension(new CommonMarkCoreExtension()); $environment->addExtension(new AutolinkExtension()); $environment->addExtension(new StrikethroughExtension()); $converter = new MarkdownConverter($environment); $html = $converter->convert($markdown)->getContent(); $this->assertSame( '

Prefix i link: https://aws.amazon.com/emr/features/hadoop/

' . "\n", $html ); } }