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

ImageRenderer::render()   C

Complexity

Conditions 7
Paths 17

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 28
Code Lines 17

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 28
ccs 19
cts 19
cp 1
rs 6.7272
nc 17
cc 7
eloc 17
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\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 93
        }
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 6
        } 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 39
        }
63
64 93
        return new HtmlElement('img', $attrs, '', true);
65
    }
66
67
    /**
68
     * @param Configuration $configuration
69
     */
70 1899
    public function setConfiguration(Configuration $configuration)
71
    {
72 1899
        $this->config = $configuration;
73 1899
    }
74
}
75