LinkRenderer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 58
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getXmlTagName() 0 3 1
A setConfiguration() 0 3 1
A getXmlAttributes() 0 7 1
B render() 0 20 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Extension\CommonMark\Renderer\Inline;
18
19
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
20
use League\CommonMark\Node\Node;
21
use League\CommonMark\Renderer\ChildNodeRendererInterface;
22
use League\CommonMark\Renderer\NodeRendererInterface;
23
use League\CommonMark\Util\HtmlElement;
24
use League\CommonMark\Util\RegexHelper;
25
use League\CommonMark\Xml\XmlNodeRendererInterface;
26
use League\Config\ConfigurationAwareInterface;
27
use League\Config\ConfigurationInterface;
28
29
final class LinkRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
30
{
31
    /** @psalm-readonly-allow-private-mutation */
32
    private ConfigurationInterface $config;
33
34
    /**
35
     * @param Link $node
36
     *
37
     * {@inheritDoc}
38
     *
39
     * @psalm-suppress MoreSpecificImplementedParamType
40
     */
41 564
    public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
42
    {
43 564
        Link::assertInstanceOf($node);
44
45 562
        $attrs = $node->data->get('attributes');
46
47 562
        $forbidUnsafeLinks = ! $this->config->get('allow_unsafe_links');
48 562
        if (! ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($node->getUrl()))) {
49 560
            $attrs['href'] = $node->getUrl();
50
        }
51
52 562
        if (($title = $node->getTitle()) !== null) {
53 64
            $attrs['title'] = $title;
54
        }
55
56 562
        if (isset($attrs['target']) && $attrs['target'] === '_blank' && ! isset($attrs['rel'])) {
57 6
            $attrs['rel'] = 'noopener noreferrer';
58
        }
59
60 562
        return new HtmlElement('a', $attrs, $childRenderer->renderNodes($node->children()));
61
    }
62
63 2242
    public function setConfiguration(ConfigurationInterface $configuration): void
64
    {
65 2242
        $this->config = $configuration;
66
    }
67
68 40
    public function getXmlTagName(Node $node): string
69
    {
70 40
        return 'link';
71
    }
72
73
    /**
74
     * @param Link $node
75
     *
76
     * @return array<string, scalar>
77
     *
78
     * @psalm-suppress MoreSpecificImplementedParamType
79
     */
80 40
    public function getXmlAttributes(Node $node): array
81
    {
82 40
        Link::assertInstanceOf($node);
83
84 40
        return [
85 40
            'destination' => $node->getUrl(),
86 40
            'title' => $node->getTitle() ?? '',
87 40
        ];
88
    }
89
}
90