ImageRenderer   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 5

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 50
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C render() 0 28 7
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\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\Image;
21
use League\CommonMark\Util\Configuration;
22
use League\CommonMark\Util\ConfigurationAwareInterface;
23
use League\CommonMark\Util\RegexHelper;
24
25
class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterface
26
{
27
    /**
28
     * @var Configuration
29
     */
30
    protected $config;
31
32
    /**
33
     * @param Image                    $inline
34
     * @param ElementRendererInterface $htmlRenderer
35
     *
36
     * @return HtmlElement
37
     */
38 96
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
39
    {
40 96
        if (!($inline instanceof Image)) {
41 3
            throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
42
        }
43
44 93
        $attrs = [];
45 93
        foreach ($inline->getData('attributes', []) as $key => $value) {
46 3
            $attrs[$key] = $htmlRenderer->escape($value, true);
47 62
        }
48
49 93
        $forbidUnsafeLinks = $this->config->getConfig('safe') || !$this->config->getConfig('allow_unsafe_links');
50 93
        if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl())) {
51 6
            $attrs['src'] = '';
52 4
        } else {
53 87
            $attrs['src'] = $htmlRenderer->escape($inline->getUrl(), true);
54
        }
55
56 93
        $alt = $htmlRenderer->renderInlines($inline->children());
57 93
        $alt = preg_replace('/\<[^>]*alt="([^"]*)"[^>]*\>/', '$1', $alt);
58 93
        $attrs['alt'] = preg_replace('/\<[^>]*\>/', '', $alt);
59
60 93
        if (isset($inline->data['title'])) {
61 39
            $attrs['title'] = $htmlRenderer->escape($inline->data['title'], true);
62 26
        }
63
64 93
        return new HtmlElement('img', $attrs, '', true);
65
    }
66
67
    /**
68
     * @param Configuration $configuration
69
     */
70 1953
    public function setConfiguration(Configuration $configuration)
71
    {
72 1953
        $this->config = $configuration;
73 1953
    }
74
}
75