| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744 | <?phpuse Codeception\Util\Fixtures;use Grav\Common\Grav;use Grav\Common\Uri;use Grav\Common\Config\Config;use Grav\Common\Page\Pages;use Grav\Common\Markdown\Parsedown;use Grav\Common\Language\Language;/** * Class ParsedownTest */class ParsedownTest extends \Codeception\TestCase\Test{    /** @var Parsedown $parsedown */    protected $parsedown;    /** @var Grav $grav */    protected $grav;    /** @var Pages $pages */    protected $pages;    /** @var Config $config */    protected $config;    /** @var  Uri $uri */    protected $uri;    /** @var  Language $language */    protected $language;    protected $old_home;    protected function _before()    {        $grav = Fixtures::get('grav');        $this->grav = $grav();        $this->pages = $this->grav['pages'];        $this->config = $this->grav['config'];        $this->uri = $this->grav['uri'];        $this->language = $this->grav['language'];        $this->old_home = $this->config->get('system.home.alias');        $this->config->set('system.home.alias', '/item1');        $this->config->set('system.absolute_urls', false);        $this->config->set('system.languages.supported', []);        unset($this->grav['language']);        $this->grav['language'] = new Language($this->grav);        /** @var UniformResourceLocator $locator */        $locator = $this->grav['locator'];        $locator->addPath('page', '', 'tests/fake/nested-site/user/pages', false);        $this->pages->init();        $defaults = [            'extra'            => false,            'auto_line_breaks' => false,            'auto_url_links'   => false,            'escape_markup'    => false,            'special_chars'    => ['>' => 'gt', '<' => 'lt'],        ];        $page = $this->pages->dispatch('/item2/item2-2');        $this->parsedown = new Parsedown($page, $defaults);    }    protected function _after()    {        $this->config->set('system.home.alias', $this->old_home);    }    public function testImages()    {        $this->config->set('system.languages.supported', ['fr','en']);        unset($this->grav['language']);        $this->grav['language'] = new Language($this->grav);        $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();        $this->assertSame('<p><img alt="" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="\/images\/.*-cache-image.jpe?g\?foo=1" \/><\/p>|',            $this->parsedown->text(''));        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><img alt="" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="\/images\/.*-cache-image.jpe?g\?foo=1" \/><\/p>|',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertSame('<p><img src="/item2/item2-2/missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img src="/home-missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img src="/home-missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img src="https://getgrav-grav.netdna-ssl.com/user/pages/media/grav-logo.svg" alt="" /></p>',            $this->parsedown->text(''));   }    public function testImagesSubDir()    {        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertRegexp('|<p><img alt="" src="\/subdir\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertSame('<p><img alt="" src="/subdir/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="\/subdir\/images\/.*-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertSame('<p><img src="/subdir/item2/item2-2/missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img src="/subdir/home-missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));    }    public function testImagesAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><img alt="" src="http://testing.dev/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="http:\/\/testing.dev\/images\/.*-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="http:\/\/testing.dev\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertSame('<p><img src="http://testing.dev/item2/item2-2/missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img src="http://testing.dev/home-missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));    }    public function testImagesSubDirAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><img alt="" src="http://testing.dev/subdir/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="http:\/\/testing.dev\/subdir\/images\/.*-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="http:\/\/testing.dev\/subdir\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertSame('<p><img src="http://testing.dev/subdir/item2/item2-2/missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img src="http://testing.dev/subdir/home-missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));    }    public function testImagesAttributes()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><img title="My Title" alt="" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img alt="" class="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img alt="" class="foo bar" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img alt="" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img alt="Alt Text" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img alt="Alt Text" class="bar" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img title="My Title" alt="Alt Text" class="bar" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));    }    public function testRootImages()    {        $this->uri->initializeWithURL('http://testing.dev/')->init();        $defaults = [            'extra'            => false,            'auto_line_breaks' => false,            'auto_url_links'   => false,            'escape_markup'    => false,            'special_chars'    => ['>' => 'gt', '<' => 'lt'],        ];        $page = $this->pages->dispatch('/');        $this->parsedown = new Parsedown($page, $defaults);        $this->assertSame('<p><img alt="" src="/tests/fake/nested-site/user/pages/01.item1/home-sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="\/images\/.*-home-cache-image.jpe?g\?foo=1" \/><\/p>|',            $this->parsedown->text(''));        $this->assertSame('<p><img src="/home-missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));        $this->config->set('system.languages.supported', ['fr','en']);        unset($this->grav['language']);        $this->grav['language'] = new Language($this->grav);        $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();        $this->assertSame('<p><img alt="" src="/tests/fake/nested-site/user/pages/01.item1/home-sample-image.jpg" /></p>',            $this->parsedown->text(''));    }    public function testRootImagesSubDirAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><img alt="" src="http://testing.dev/subdir/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="http:\/\/testing.dev\/subdir\/images\/.*-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertRegexp('|<p><img alt="" src="http:\/\/testing.dev\/subdir\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',            $this->parsedown->text(''));        $this->assertSame('<p><img src="http://testing.dev/subdir/item2/item2-2/missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));        $this->assertSame('<p><img src="http://testing.dev/subdir/home-missing-image.jpg" alt="" /></p>',            $this->parsedown->text(''));    }    public function testRootAbsoluteLinks()    {        $this->uri->initializeWithURL('http://testing.dev/')->init();        $defaults = [            'extra'            => false,            'auto_line_breaks' => false,            'auto_url_links'   => false,            'escape_markup'    => false,            'special_chars'    => ['>' => 'gt', '<' => 'lt'],        ];        $page = $this->pages->dispatch('/');        $this->parsedown = new Parsedown($page, $defaults);        $this->assertSame('<p><a href="/item1/item1-3">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](item1-3)'));        $this->assertSame('<p><a href="/item2">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](../item2)'));        $this->assertSame('<p><a href="/?foo=bar">With Query</a></p>',            $this->parsedown->text('[With Query](?foo=bar)'));        $this->assertSame('<p><a href="/foo:bar">With Param</a></p>',            $this->parsedown->text('[With Param](/foo:bar)'));        $this->assertSame('<p><a href="#foo">With Anchor</a></p>',            $this->parsedown->text('[With Anchor](#foo)'));        $this->config->set('system.languages.supported', ['fr','en']);        unset($this->grav['language']);        $this->grav['language'] = new Language($this->grav);        $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();        $this->assertSame('<p><a href="/fr/item2">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](../item2)'));        $this->assertSame('<p><a href="/fr/item1/item1-3">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](item1-3)'));        $this->assertSame('<p><a href="/fr/?foo=bar">With Query</a></p>',            $this->parsedown->text('[With Query](?foo=bar)'));        $this->assertSame('<p><a href="/fr/foo:bar">With Param</a></p>',            $this->parsedown->text('[With Param](/foo:bar)'));        $this->assertSame('<p><a href="#foo">With Anchor</a></p>',            $this->parsedown->text('[With Anchor](#foo)'));    }    public function testAnchorLinksLangRelativeUrls()    {        $this->config->set('system.languages.supported', ['fr','en']);        unset($this->grav['language']);        $this->grav['language'] = new Language($this->grav);        $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();        $this->assertSame('<p><a href="#foo">Current Anchor</a></p>',            $this->parsedown->text('[Current Anchor](#foo)'));        $this->assertSame('<p><a href="/fr/#foo">Root Anchor</a></p>',            $this->parsedown->text('[Root Anchor](/#foo)'));        $this->assertSame('<p><a href="/fr/item2/item2-1#foo">Peer Anchor</a></p>',            $this->parsedown->text('[Peer Anchor](../item2-1#foo)'));        $this->assertSame('<p><a href="/fr/item2/item2-1#foo">Peer Anchor 2</a></p>',            $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));    }    public function testAnchorLinksLangAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->config->set('system.languages.supported', ['fr','en']);        unset($this->grav['language']);        $this->grav['language'] = new Language($this->grav);        $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();        $this->assertSame('<p><a href="#foo">Current Anchor</a></p>',            $this->parsedown->text('[Current Anchor](#foo)'));        $this->assertSame('<p><a href="http://testing.dev/fr/item2/item2-1#foo">Peer Anchor</a></p>',            $this->parsedown->text('[Peer Anchor](../item2-1#foo)'));        $this->assertSame('<p><a href="http://testing.dev/fr/item2/item2-1#foo">Peer Anchor 2</a></p>',            $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));        $this->assertSame('<p><a href="http://testing.dev/fr/#foo">Root Anchor</a></p>',            $this->parsedown->text('[Root Anchor](/#foo)'));    }    public function testExternalLinks()    {        $this->assertSame('<p><a href="http://www.cnn.com">cnn.com</a></p>',            $this->parsedown->text('[cnn.com](http://www.cnn.com)'));        $this->assertSame('<p><a href="https://www.google.com">google.com</a></p>',            $this->parsedown->text('[google.com](https://www.google.com)'));        $this->assertSame('<p><a href="https://github.com/getgrav/grav/issues/new?title=%5Badd-resource%5D%20New%20Plugin%2FTheme&body=Hello%20%2A%2AThere%2A%2A">complex url</a></p>',            $this->parsedown->text('[complex url](https://github.com/getgrav/grav/issues/new?title=[add-resource]%20New%20Plugin/Theme&body=Hello%20**There**)'));    }    public function testExternalLinksSubDir()    {        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="http://www.cnn.com">cnn.com</a></p>',            $this->parsedown->text('[cnn.com](http://www.cnn.com)'));        $this->assertSame('<p><a href="https://www.google.com">google.com</a></p>',            $this->parsedown->text('[google.com](https://www.google.com)'));    }    public function testExternalLinksSubDirAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="http://www.cnn.com">cnn.com</a></p>',            $this->parsedown->text('[cnn.com](http://www.cnn.com)'));        $this->assertSame('<p><a href="https://www.google.com">google.com</a></p>',            $this->parsedown->text('[google.com](https://www.google.com)'));    }    public function testAnchorLinksRelativeUrls()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="#foo">Current Anchor</a></p>',            $this->parsedown->text('[Current Anchor](#foo)'));        $this->assertSame('<p><a href="/#foo">Root Anchor</a></p>',            $this->parsedown->text('[Root Anchor](/#foo)'));        $this->assertSame('<p><a href="/item2/item2-1#foo">Peer Anchor</a></p>',            $this->parsedown->text('[Peer Anchor](../item2-1#foo)'));        $this->assertSame('<p><a href="/item2/item2-1#foo">Peer Anchor 2</a></p>',            $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));    }    public function testAnchorLinksAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="#foo">Current Anchor</a></p>',            $this->parsedown->text('[Current Anchor](#foo)'));        $this->assertSame('<p><a href="http://testing.dev/item2/item2-1#foo">Peer Anchor</a></p>',            $this->parsedown->text('[Peer Anchor](../item2-1#foo)'));        $this->assertSame('<p><a href="http://testing.dev/item2/item2-1#foo">Peer Anchor 2</a></p>',            $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));        $this->assertSame('<p><a href="http://testing.dev/#foo">Root Anchor</a></p>',            $this->parsedown->text('[Root Anchor](/#foo)'));    }    public function testAnchorLinksWithPortAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithURL('http://testing.dev:8080/item2/item2-2')->init();        $this->assertSame('<p><a href="http://testing.dev:8080/item2/item2-1#foo">Peer Anchor</a></p>',            $this->parsedown->text('[Peer Anchor](../item2-1#foo)'));        $this->assertSame('<p><a href="http://testing.dev:8080/item2/item2-1#foo">Peer Anchor 2</a></p>',            $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));        $this->assertSame('<p><a href="#foo">Current Anchor</a></p>',            $this->parsedown->text('[Current Anchor](#foo)'));        $this->assertSame('<p><a href="http://testing.dev:8080/#foo">Root Anchor</a></p>',            $this->parsedown->text('[Root Anchor](/#foo)'));    }    public function testAnchorLinksSubDirRelativeUrls()    {        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="/subdir/item2/item2-1#foo">Peer Anchor</a></p>',            $this->parsedown->text('[Peer Anchor](../item2-1#foo)'));        $this->assertSame('<p><a href="/subdir/item2/item2-1#foo">Peer Anchor 2</a></p>',            $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));        $this->assertSame('<p><a href="#foo">Current Anchor</a></p>',            $this->parsedown->text('[Current Anchor](#foo)'));        $this->assertSame('<p><a href="/subdir/#foo">Root Anchor</a></p>',            $this->parsedown->text('[Root Anchor](/#foo)'));    }    public function testAnchorLinksSubDirAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-1#foo">Peer Anchor</a></p>',            $this->parsedown->text('[Peer Anchor](../item2-1#foo)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-1#foo">Peer Anchor 2</a></p>',            $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));        $this->assertSame('<p><a href="#foo">Current Anchor</a></p>',            $this->parsedown->text('[Current Anchor](#foo)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/#foo">Root Anchor</a></p>',            $this->parsedown->text('[Root Anchor](/#foo)'));    }    public function testSlugRelativeLinks()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="/">Up to Root Level</a></p>',            $this->parsedown->text('[Up to Root Level](../..)'));        $this->assertSame('<p><a href="/item2/item2-1">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](../item2-1)'));        $this->assertSame('<p><a href="/item2/item2-2/item2-2-1">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](item2-2-1)'));        $this->assertSame('<p><a href="/item2">Up a Level</a></p>',            $this->parsedown->text('[Up a Level](..)'));        $this->assertSame('<p><a href="/item3/item3-3">Up and Down</a></p>',            $this->parsedown->text('[Up and Down](../../item3/item3-3)'));        $this->assertSame('<p><a href="/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',            $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)'));        $this->assertSame('<p><a href="/item2?foo=bar">Up a Level with Query</a></p>',            $this->parsedown->text('[Up a Level with Query](../?foo=bar)'));        $this->assertSame('<p><a href="/item3/item3-3?foo=bar">Up and Down with Query</a></p>',            $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)'));        $this->assertSame('<p><a href="/item3/item3-3/foo:bar">Up and Down with Param</a></p>',            $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)'));        $this->assertSame('<p><a href="/item3/item3-3#foo">Up and Down with Anchor</a></p>',            $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)'));    }    public function testSlugRelativeLinksAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="http://testing.dev/item2/item2-1">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](../item2-1)'));        $this->assertSame('<p><a href="http://testing.dev/item2/item2-2/item2-2-1">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](item2-2-1)'));        $this->assertSame('<p><a href="http://testing.dev/item2">Up a Level</a></p>',            $this->parsedown->text('[Up a Level](..)'));        $this->assertSame('<p><a href="http://testing.dev/">Up to Root Level</a></p>',            $this->parsedown->text('[Up to Root Level](../..)'));        $this->assertSame('<p><a href="http://testing.dev/item3/item3-3">Up and Down</a></p>',            $this->parsedown->text('[Up and Down](../../item3/item3-3)'));        $this->assertSame('<p><a href="http://testing.dev/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',            $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)'));        $this->assertSame('<p><a href="http://testing.dev/item2?foo=bar">Up a Level with Query</a></p>',            $this->parsedown->text('[Up a Level with Query](../?foo=bar)'));        $this->assertSame('<p><a href="http://testing.dev/item3/item3-3?foo=bar">Up and Down with Query</a></p>',            $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)'));        $this->assertSame('<p><a href="http://testing.dev/item3/item3-3/foo:bar">Up and Down with Param</a></p>',            $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)'));        $this->assertSame('<p><a href="http://testing.dev/item3/item3-3#foo">Up and Down with Anchor</a></p>',            $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)'));    }    public function testSlugRelativeLinksSubDir()    {        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="/subdir/item2/item2-1">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](../item2-1)'));        $this->assertSame('<p><a href="/subdir/item2/item2-2/item2-2-1">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](item2-2-1)'));        $this->assertSame('<p><a href="/subdir/item2">Up a Level</a></p>',            $this->parsedown->text('[Up a Level](..)'));        $this->assertSame('<p><a href="/subdir">Up to Root Level</a></p>',            $this->parsedown->text('[Up to Root Level](../..)'));        $this->assertSame('<p><a href="/subdir/item3/item3-3">Up and Down</a></p>',            $this->parsedown->text('[Up and Down](../../item3/item3-3)'));        $this->assertSame('<p><a href="/subdir/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',            $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)'));        $this->assertSame('<p><a href="/subdir/item2?foo=bar">Up a Level with Query</a></p>',            $this->parsedown->text('[Up a Level with Query](../?foo=bar)'));        $this->assertSame('<p><a href="/subdir/item3/item3-3?foo=bar">Up and Down with Query</a></p>',            $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)'));        $this->assertSame('<p><a href="/subdir/item3/item3-3/foo:bar">Up and Down with Param</a></p>',            $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)'));        $this->assertSame('<p><a href="/subdir/item3/item3-3#foo">Up and Down with Anchor</a></p>',            $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)'));    }    public function testSlugRelativeLinksSubDirAbsoluteUrls()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-1">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](../item2-1)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-2/item2-2-1">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](item2-2-1)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2">Up a Level</a></p>',            $this->parsedown->text('[Up a Level](..)'));        $this->assertSame('<p><a href="http://testing.dev/subdir">Up to Root Level</a></p>',            $this->parsedown->text('[Up to Root Level](../..)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item3/item3-3">Up and Down</a></p>',            $this->parsedown->text('[Up and Down](../../item3/item3-3)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',            $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2?foo=bar">Up a Level with Query</a></p>',            $this->parsedown->text('[Up a Level with Query](../?foo=bar)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item3/item3-3?foo=bar">Up and Down with Query</a></p>',            $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item3/item3-3/foo:bar">Up and Down with Param</a></p>',            $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item3/item3-3#foo">Up and Down with Anchor</a></p>',            $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)'));    }    public function testDirectoryRelativeLinks()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="/item3/item3-3/foo:bar">Up and Down with Param</a></p>',            $this->parsedown->text('[Up and Down with Param](../../03.item3/03.item3-3/foo:bar)'));        $this->assertSame('<p><a href="/item2/item2-1">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](../01.item2-1)'));        $this->assertSame('<p><a href="/item2/item2-2/item2-2-1">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](01.item2-2-1)'));        $this->assertSame('<p><a href="/item3/item3-3">Up and Down</a></p>',            $this->parsedown->text('[Up and Down](../../03.item3/03.item3-3)'));        $this->assertSame('<p><a href="/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',            $this->parsedown->text('[Down a Level with Query](01.item2-2-1?foo=bar)'));        $this->assertSame('<p><a href="/item3/item3-3?foo=bar">Up and Down with Query</a></p>',            $this->parsedown->text('[Up and Down with Query](../../03.item3/03.item3-3?foo=bar)'));        $this->assertSame('<p><a href="/item3/item3-3#foo">Up and Down with Anchor</a></p>',            $this->parsedown->text('[Up and Down with Anchor](../../03.item3/03.item3-3#foo)'));    }    public function testAbsoluteLinks()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="/">Root</a></p>',            $this->parsedown->text('[Root](/)'));        $this->assertSame('<p><a href="/item2/item2-1">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](/item2/item2-1)'));        $this->assertSame('<p><a href="/item2/item2-2/item2-2-1">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)'));        $this->assertSame('<p><a href="/item2">Up a Level</a></p>',            $this->parsedown->text('[Up a Level](/item2)'));        $this->assertSame('<p><a href="/item2?foo=bar">With Query</a></p>',            $this->parsedown->text('[With Query](/item2?foo=bar)'));        $this->assertSame('<p><a href="/item2/foo:bar">With Param</a></p>',            $this->parsedown->text('[With Param](/item2/foo:bar)'));        $this->assertSame('<p><a href="/item2#foo">With Anchor</a></p>',            $this->parsedown->text('[With Anchor](/item2#foo)'));    }    public function testDirectoryAbsoluteLinksSubDir()    {        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="/subdir/">Root</a></p>',            $this->parsedown->text('[Root](/)'));        $this->assertSame('<p><a href="/subdir/item2/item2-1">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](/item2/item2-1)'));        $this->assertSame('<p><a href="/subdir/item2/item2-2/item2-2-1">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)'));        $this->assertSame('<p><a href="/subdir/item2">Up a Level</a></p>',            $this->parsedown->text('[Up a Level](/item2)'));        $this->assertSame('<p><a href="/subdir/item2?foo=bar">With Query</a></p>',            $this->parsedown->text('[With Query](/item2?foo=bar)'));        $this->assertSame('<p><a href="/subdir/item2/foo:bar">With Param</a></p>',            $this->parsedown->text('[With Param](/item2/foo:bar)'));        $this->assertSame('<p><a href="/subdir/item2#foo">With Anchor</a></p>',            $this->parsedown->text('[With Anchor](/item2#foo)'));    }    public function testDirectoryAbsoluteLinksSubDirAbsoluteUrl()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="http://testing.dev/subdir/">Root</a></p>',            $this->parsedown->text('[Root](/)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-1">Peer Page</a></p>',            $this->parsedown->text('[Peer Page](/item2/item2-1)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-2/item2-2-1">Down a Level</a></p>',            $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2">Up a Level</a></p>',            $this->parsedown->text('[Up a Level](/item2)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2?foo=bar">With Query</a></p>',            $this->parsedown->text('[With Query](/item2?foo=bar)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/foo:bar">With Param</a></p>',            $this->parsedown->text('[With Param](/item2/foo:bar)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2#foo">With Anchor</a></p>',            $this->parsedown->text('[With Anchor](/item2#foo)'));    }    public function testSpecialProtocols()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="mailto:user@domain.com">mailto</a></p>',            $this->parsedown->text('[mailto](mailto:user@domain.com)'));        $this->assertSame('<p><a href="xmpp:xyx@domain.com">xmpp</a></p>',            $this->parsedown->text('[xmpp](xmpp:xyx@domain.com)'));        $this->assertSame('<p><a href="tel:123-555-12345">tel</a></p>',            $this->parsedown->text('[tel](tel:123-555-12345)'));        $this->assertSame('<p><a href="sms:123-555-12345">sms</a></p>',            $this->parsedown->text('[sms](sms:123-555-12345)'));        $this->assertSame('<p><a href="rdp://ts.example.com">ts.example.com</a></p>',            $this->parsedown->text('[ts.example.com](rdp://ts.example.com)'));    }    public function testSpecialProtocolsSubDir()    {        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="mailto:user@domain.com">mailto</a></p>',            $this->parsedown->text('[mailto](mailto:user@domain.com)'));        $this->assertSame('<p><a href="xmpp:xyx@domain.com">xmpp</a></p>',            $this->parsedown->text('[xmpp](xmpp:xyx@domain.com)'));        $this->assertSame('<p><a href="tel:123-555-12345">tel</a></p>',            $this->parsedown->text('[tel](tel:123-555-12345)'));        $this->assertSame('<p><a href="sms:123-555-12345">sms</a></p>',            $this->parsedown->text('[sms](sms:123-555-12345)'));        $this->assertSame('<p><a href="rdp://ts.example.com">ts.example.com</a></p>',            $this->parsedown->text('[ts.example.com](rdp://ts.example.com)'));    }    public function testSpecialProtocolsSubDirAbsoluteUrl()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="mailto:user@domain.com">mailto</a></p>',            $this->parsedown->text('[mailto](mailto:user@domain.com)'));        $this->assertSame('<p><a href="xmpp:xyx@domain.com">xmpp</a></p>',            $this->parsedown->text('[xmpp](xmpp:xyx@domain.com)'));        $this->assertSame('<p><a href="tel:123-555-12345">tel</a></p>',            $this->parsedown->text('[tel](tel:123-555-12345)'));        $this->assertSame('<p><a href="sms:123-555-12345">sms</a></p>',            $this->parsedown->text('[sms](sms:123-555-12345)'));        $this->assertSame('<p><a href="rdp://ts.example.com">ts.example.com</a></p>',            $this->parsedown->text('[ts.example.com](rdp://ts.example.com)'));    }    public function testReferenceLinks()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $sample = '[relative link][r_relative]                   [r_relative]: ../item2-3#blah';        $this->assertSame('<p><a href="/item2/item2-3#blah">relative link</a></p>',            $this->parsedown->text($sample));        $sample = '[absolute link][r_absolute]                   [r_absolute]: /item3#blah';        $this->assertSame('<p><a href="/item3#blah">absolute link</a></p>',            $this->parsedown->text($sample));        $sample = '[external link][r_external]                   [r_external]: http://www.cnn.com';        $this->assertSame('<p><a href="http://www.cnn.com">external link</a></p>',            $this->parsedown->text($sample));    }    public function testAttributeLinks()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="#something" class="button">Anchor Class</a></p>',            $this->parsedown->text('[Anchor Class](?classes=button#something)'));        $this->assertSame('<p><a href="/item2/item2-3" class="button">Relative Class</a></p>',            $this->parsedown->text('[Relative Class](../item2-3?classes=button)'));        $this->assertSame('<p><a href="/item2/item2-3" id="unique">Relative ID</a></p>',            $this->parsedown->text('[Relative ID](../item2-3?id=unique)'));        $this->assertSame('<p><a href="https://github.com/getgrav/grav" class="button big">External</a></p>',            $this->parsedown->text('[External](https://github.com/getgrav/grav?classes=button,big)'));        $this->assertSame('<p><a href="/item2/item2-3?id=unique">Relative Noprocess</a></p>',            $this->parsedown->text('[Relative Noprocess](../item2-3?id=unique&noprocess)'));        $this->assertSame('<p><a href="/item2/item2-3" target="_blank">Relative Target</a></p>',            $this->parsedown->text('[Relative Target](../item2-3?target=_blank)'));        $this->assertSame('<p><a href="/item2/item2-3" rel="nofollow">Relative Rel</a></p>',            $this->parsedown->text('[Relative Rel](../item2-3?rel=nofollow)'));        $this->assertSame('<p><a href="/item2/item2-3?foo=bar&baz=qux" rel="nofollow" class="button">Relative Mixed</a></p>',            $this->parsedown->text('[Relative Mixed](../item2-3?foo=bar&baz=qux&rel=nofollow&class=button)'));    }    public function testInvalidLinks()    {        $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();        $this->assertSame('<p><a href="/item2/item2-2/no-page">Non Existent Page</a></p>',            $this->parsedown->text('[Non Existent Page](no-page)'));        $this->assertSame('<p><a href="/item2/item2-2/existing-file.zip">Existent File</a></p>',            $this->parsedown->text('[Existent File](existing-file.zip)'));        $this->assertSame('<p><a href="/item2/item2-2/missing-file.zip">Non Existent File</a></p>',            $this->parsedown->text('[Non Existent File](missing-file.zip)'));    }    public function testInvalidLinksSubDir()    {        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="/subdir/item2/item2-2/no-page">Non Existent Page</a></p>',            $this->parsedown->text('[Non Existent Page](no-page)'));        $this->assertSame('<p><a href="/subdir/item2/item2-2/existing-file.zip">Existent File</a></p>',            $this->parsedown->text('[Existent File](existing-file.zip)'));        $this->assertSame('<p><a href="/subdir/item2/item2-2/missing-file.zip">Non Existent File</a></p>',            $this->parsedown->text('[Non Existent File](missing-file.zip)'));    }    public function testInvalidLinksSubDirAbsoluteUrl()    {        $this->config->set('system.absolute_urls', true);        $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-2/no-page">Non Existent Page</a></p>',            $this->parsedown->text('[Non Existent Page](no-page)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-2/existing-file.zip">Existent File</a></p>',            $this->parsedown->text('[Existent File](existing-file.zip)'));        $this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-2/missing-file.zip">Non Existent File</a></p>',            $this->parsedown->text('[Non Existent File](missing-file.zip)'));    }    /**     * @param $string     *     * @return mixed     */    private function stripLeadingWhitespace($string)    {        return preg_replace('/^\s*(.*)/', '', $string);    }}
 |