LinkRenderer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 48
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfiguration() 0 4 1
B render() 0 26 10
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\Inline\Renderer;
16
17
use League\CommonMark\ElementRendererInterface;
18
use League\CommonMark\HtmlElement;
19
use League\CommonMark\Inline\Element\AbstractInline;
20
use League\CommonMark\Inline\Element\Link;
21
use League\CommonMark\Util\Configuration;
22
use League\CommonMark\Util\ConfigurationAwareInterface;
23
use League\CommonMark\Util\RegexHelper;
24
use League\CommonMark\Util\Xml;
25
26
class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterface
27
{
28
    /**
29
     * @var Configuration
30
     */
31
    protected $config;
32
33
    /**
34
     * @param Link                     $inline
35
     * @param ElementRendererInterface $htmlRenderer
36
     *
37
     * @return HtmlElement
38
     */
39 351
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
40
    {
41 351
        if (!($inline instanceof Link)) {
42 3
            throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
43
        }
44
45 348
        $attrs = [];
46 348
        foreach ($inline->getData('attributes', []) as $key => $value) {
47 6
            $attrs[$key] = Xml::escape($value, true);
48 116
        }
49
50 348
        $forbidUnsafeLinks = $this->config->getConfig('safe') || !$this->config->getConfig('allow_unsafe_links');
51 348
        if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl()))) {
52 336
            $attrs['href'] = Xml::escape($inline->getUrl(), true);
53 112
        }
54
55 348
        if (isset($inline->data['title'])) {
56 93
            $attrs['title'] = Xml::escape($inline->data['title'], true);
57 31
        }
58
59 348
        if (isset($attrs['target']) && $attrs['target'] === '_blank' && !isset($attrs['rel'])) {
60 3
            $attrs['rel'] = 'noopener noreferrer';
61 1
        }
62
63 348
        return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children()));
64
    }
65
66
    /**
67
     * @param Configuration $configuration
68
     */
69 1962
    public function setConfiguration(Configuration $configuration)
70
    {
71 1962
        $this->config = $configuration;
72 1962
    }
73
}
74