ImageRenderer   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 6

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