Completed
Push — master ( 9eb60a...7a0a03 )
by Colin
36:39 queued 35:14
created

ImageRenderer::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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
     * @param Image                 $inline
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '*', expecting T_FUNCTION or T_CONST
Loading history...
34
     * @param NodeRendererInterface $htmlRenderer
35
     *
36
     * @return HtmlElement
37
     */
38
    public function render(AbstractInline $inline, NodeRendererInterface $htmlRenderer)
39
    {
40
        if (!($inline instanceof Image)) {
41
            throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
42
        }
43
44
        $attrs = $inline->getData('attributes', []);
45
46
        $forbidUnsafeLinks = !$this->config->get('allow_unsafe_links');
47
        if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl())) {
48
            $attrs['src'] = '';
49
        } else {
50
            $attrs['src'] = $inline->getUrl();
51
        }
52
53
        $alt = $htmlRenderer->renderInlines($inline->children());
54
        $alt = \preg_replace('/\<[^>]*alt="([^"]*)"[^>]*\>/', '$1', $alt);
55
        $attrs['alt'] = \preg_replace('/\<[^>]*\>/', '', $alt);
56
57
        if (isset($inline->data['title'])) {
58
            $attrs['title'] = $inline->data['title'];
59
        }
60
61
        return new HtmlElement('img', $attrs, '', true);
62
    }
63
64
    public function setConfiguration(ConfigurationInterface $configuration): void
65
    {
66
        $this->config = $configuration;
67
    }
68
}
69