Completed
Push — master ( 7a0a03...73b89e )
by Colin
02:28
created

ImageRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
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 44
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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