Completed
Push — master ( 0b0b24...54ff37 )
by Colin
02:37
created

LinkRenderer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 42
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 23 8
A setConfiguration() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Extension\CommonMark\Renderer\Inline;
16
17
use League\CommonMark\Configuration\ConfigurationAwareInterface;
18
use League\CommonMark\Configuration\ConfigurationInterface;
19
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
20
use League\CommonMark\Node\Inline\AbstractInline;
21
use League\CommonMark\Renderer\ElementRendererInterface;
22
use League\CommonMark\Renderer\Inline\InlineRendererInterface;
23
use League\CommonMark\Util\HtmlElement;
24
use League\CommonMark\Util\RegexHelper;
25
26
final class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterface
27
{
28
    /**
29
     * @var ConfigurationInterface
30
     */
31
    protected $config;
32
33
    /**
34
     * @param Link                     $inline
35
     * @param ElementRendererInterface $htmlRenderer
36
     *
37
     * @return HtmlElement
38
     */
39 537
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
40
    {
41 537
        if (!($inline instanceof Link)) {
42 3
            throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
43
        }
44
45 534
        $attrs = $inline->getData('attributes', []);
46
47 534
        $forbidUnsafeLinks = !$this->config->get('allow_unsafe_links');
48 534
        if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl()))) {
49 531
            $attrs['href'] = $inline->getUrl();
50
        }
51
52 534
        if (isset($inline->data['title'])) {
53 93
            $attrs['title'] = $inline->data['title'];
54
        }
55
56 534
        if (isset($attrs['target']) && $attrs['target'] === '_blank' && !isset($attrs['rel'])) {
57 3
            $attrs['rel'] = 'noopener noreferrer';
58
        }
59
60 534
        return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children()));
61
    }
62
63 2508
    public function setConfiguration(ConfigurationInterface $configuration): void
64
    {
65 2508
        $this->config = $configuration;
66 2508
    }
67
}
68