Completed
Push — master ( 585e6a...d168ab )
by Colin
04:06 queued 22s
created

ImageRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 47
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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