1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
<?php
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* 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\HeadingPermalink;
use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment;
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkRenderer;
use PHPUnit\Framework\TestCase;
final class HeadingPermalinkExtensionTest extends TestCase
{
/**
* @param string $input
* @param string $expected
*
* @dataProvider dataProviderForTestHeadingPermalinksWithDefaultOptions
*/
public function testHeadingPermalinksWithDefaultOptions(string $input, string $expected)
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new HeadingPermalinkExtension());
$converter = new CommonMarkConverter([], $environment);
$this->assertEquals($expected, \trim($converter->convertToHtml($input)));
}
public function dataProviderForTestHeadingPermalinksWithDefaultOptions()
{
yield ['# Hello World!', sprintf('<h1><a id="user-content-hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">%s</a>Hello World!</h1>', HeadingPermalinkRenderer::DEFAULT_SYMBOL)];
yield ['# Hello *World*', sprintf('<h1><a id="user-content-hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">%s</a>Hello <em>World</em></h1>', HeadingPermalinkRenderer::DEFAULT_SYMBOL)];
yield ['# Hello `World`', sprintf('<h1><a id="user-content-hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">%s</a>Hello <code>World</code></h1>', HeadingPermalinkRenderer::DEFAULT_SYMBOL)];
yield ["Test\n----", sprintf('<h2><a id="user-content-test" href="#test" name="test" class="heading-permalink" aria-hidden="true" title="Permalink">%s</a>Test</h2>', HeadingPermalinkRenderer::DEFAULT_SYMBOL)];
yield ["# Hello World!\n\n# Hello World!", sprintf("<h1><a id=\"user-content-hello-world\" href=\"#hello-world\" name=\"hello-world\" class=\"heading-permalink\" aria-hidden=\"true\" title=\"Permalink\">%s</a>Hello World!</h1>\n<h1><a id=\"user-content-hello-world-1\" href=\"#hello-world-1\" name=\"hello-world-1\" class=\"heading-permalink\" aria-hidden=\"true\" title=\"Permalink\">%s</a>Hello World!</h1>", HeadingPermalinkRenderer::DEFAULT_SYMBOL, HeadingPermalinkRenderer::DEFAULT_SYMBOL)];
}
/**
* @param string $input
* @param string $expected
*
* @dataProvider dataProviderForTestHeadingPermalinksWithCustomOptions
*/
public function testHeadingPermalinksWithCustomOptions(string $input, string $expected)
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new HeadingPermalinkExtension());
$config = [
'heading_permalink' => [
'html_class' => 'custom-class',
'id_prefix' => 'custom-prefix',
// Ensure multiple characters are allowed (including multibyte) and special HTML characters are escaped.
'symbol' => '¶ 🦄️ <3 You',
'insert' => 'after',
'title' => 'Link',
],
];
$converter = new CommonMarkConverter($config, $environment);
$this->assertEquals($expected, \trim($converter->convertToHtml($input)));
}
public function dataProviderForTestHeadingPermalinksWithCustomOptions()
{
yield ['# Hello World!', '<h1>Hello World!<a id="custom-prefix-hello-world" href="#hello-world" name="hello-world" class="custom-class" aria-hidden="true" title="Link">¶ 🦄️ <3 You</a></h1>'];
yield ['# Hello *World*', '<h1>Hello <em>World</em><a id="custom-prefix-hello-world" href="#hello-world" name="hello-world" class="custom-class" aria-hidden="true" title="Link">¶ 🦄️ <3 You</a></h1>'];
yield ["Test\n----", '<h2>Test<a id="custom-prefix-test" href="#test" name="test" class="custom-class" aria-hidden="true" title="Link">¶ 🦄️ <3 You</a></h2>'];
}
/**
* @group legacy
*/
public function testHeadingPermalinksWithDeprecatedInnerContents()
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new HeadingPermalinkExtension());
$config = [
'heading_permalink' => [
'inner_contents' => HeadingPermalinkRenderer::DEFAULT_INNER_CONTENTS,
'symbol' => '#',
],
];
$converter = new CommonMarkConverter($config, $environment);
$input = '# Hello World!';
$expected = sprintf('<h1><a id="user-content-hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">%s</a>Hello World!</h1>', HeadingPermalinkRenderer::DEFAULT_INNER_CONTENTS);
$this->assertEquals($expected, \trim($converter->convertToHtml($input)));
}
public function testHeadingPermalinksWithEmptyIdPrefix()
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new HeadingPermalinkExtension());
$config = [
'heading_permalink' => [
'id_prefix' => '',
],
];
$converter = new CommonMarkConverter($config, $environment);
$input = '# Hello World!';
$expected = sprintf('<h1><a id="hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">%s</a>Hello World!</h1>', HeadingPermalinkRenderer::DEFAULT_SYMBOL);
$this->assertEquals($expected, \trim($converter->convertToHtml($input)));
}
public function testHeadingPermalinksWithEmptySymbol()
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new HeadingPermalinkExtension());
$config = [
'heading_permalink' => [
'symbol' => '',
],
];
$converter = new CommonMarkConverter($config, $environment);
$input = '# Hello World!';
$expected = '<h1><a id="user-content-hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink"></a>Hello World!</h1>';
$this->assertEquals($expected, \trim($converter->convertToHtml($input)));
}
public function testHeadingPermalinksWithInvalidInsertConfigurationValue()
{
$this->expectException(\RuntimeException::class);
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new HeadingPermalinkExtension());
$config = [
'heading_permalink' => [
'insert' => 'invalid value here',
],
];
$converter = new CommonMarkConverter($config, $environment);
$converter->convertToHtml('# This will fail');
}
}
|