LinkRenderer::setConfiguration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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\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
25
class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterface
26
{
27
    /**
28
     * @var Configuration
29
     */
30
    protected $config;
31
32
    /**
33
     * @param Link                     $inline
34
     * @param ElementRendererInterface $htmlRenderer
35
     *
36
     * @return HtmlElement
37
     */
38 369
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
39
    {
40 369
        if (!($inline instanceof Link)) {
41 3
            throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
42
        }
43
44 366
        $attrs = $inline->getData('attributes', []);
45
46 366
        $forbidUnsafeLinks = $this->config->getConfig('safe') || !$this->config->getConfig('allow_unsafe_links');
47 366
        if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl()))) {
48 357
            $attrs['href'] = $inline->getUrl();
49
        }
50
51 366
        if (isset($inline->data['title'])) {
52 93
            $attrs['title'] = $inline->data['title'];
53
        }
54
55 366
        if (isset($attrs['target']) && $attrs['target'] === '_blank' && !isset($attrs['rel'])) {
56 3
            $attrs['rel'] = 'noopener noreferrer';
57
        }
58
59 366
        return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children()));
60
    }
61
62
    /**
63
     * @param Configuration $configuration
64
     */
65 2049
    public function setConfiguration(Configuration $configuration)
66
    {
67 2049
        $this->config = $configuration;
68 2049
    }
69
}
70