Completed
Push — master ( 1c51ea...167531 )
by Colin
8s
created

LinkRenderer::render()   C

Complexity

Conditions 7
Paths 17

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 6.9811
nc 17
cc 7
eloc 12
nop 2
crap 7
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 336
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
39
    {
40 336
        if (!($inline instanceof Link)) {
41 3
            throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
42
        }
43
44 333
        $attrs = [];
45 333
        foreach ($inline->getData('attributes', []) as $key => $value) {
46 3
            $attrs[$key] = $htmlRenderer->escape($value, true);
47 333
        }
48
49 333
        $forbidUnsafeLinks = $this->config->getConfig('safe') || !$this->config->getConfig('allow_unsafe_links');
50 333
        if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl()))) {
51 321
            $attrs['href'] = $htmlRenderer->escape($inline->getUrl(), true);
52 321
        }
53
54 333
        if (isset($inline->data['title'])) {
55 90
            $attrs['title'] = $htmlRenderer->escape($inline->data['title'], true);
56 90
        }
57
58 333
        return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children()));
59
    }
60
61
    /**
62
     * @param Configuration $configuration
63
     */
64 1899
    public function setConfiguration(Configuration $configuration)
65
    {
66 1899
        $this->config = $configuration;
67 1899
    }
68
}
69