Passed
Push — render-xml ( 61b7f8 )
by Colin
02:12
created

ImageRenderer::render()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Extension\CommonMark\Renderer\Inline;
18
19
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
20
use League\CommonMark\Node\Node;
21
use League\CommonMark\Renderer\ChildNodeRendererInterface;
22
use League\CommonMark\Renderer\NodeRendererInterface;
23
use League\CommonMark\Util\HtmlElement;
24
use League\CommonMark\Util\RegexHelper;
25
use League\CommonMark\Xml\XmlNodeRendererInterface;
26
use League\Config\ConfigurationAwareInterface;
27
use League\Config\ConfigurationInterface;
28
29
final class ImageRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
30
{
31
    /**
32
     * @var ConfigurationInterface
33
     *
34
     * @psalm-readonly-allow-private-mutation
35
     */
36
    private $config;
37
38
    /**
39
     * @param Image $node
40
     *
41
     * {@inheritDoc}
42
     *
43
     * @psalm-suppress MoreSpecificImplementedParamType
44
     */
45 93
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
46
    {
47 93
        Image::assertInstanceOf($node);
48
49 90
        $attrs = $node->data->get('attributes');
50
51 90
        $forbidUnsafeLinks = ! $this->config->get('allow_unsafe_links');
52 90
        if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($node->getUrl())) {
53 3
            $attrs['src'] = '';
54
        } else {
55 87
            $attrs['src'] = $node->getUrl();
56
        }
57
58 90
        $alt          = $childRenderer->renderNodes($node->children());
59 90
        $alt          = \preg_replace('/\<[^>]*alt="([^"]*)"[^>]*\>/', '$1', $alt);
60 90
        $attrs['alt'] = \preg_replace('/\<[^>]*\>/', '', $alt ?? '');
61
62 90
        if (($title = $node->getTitle()) !== null) {
63 39
            $attrs['title'] = $title;
64
        }
65
66 90
        return new HtmlElement('img', $attrs, '', true);
67
    }
68
69 3084
    public function setConfiguration(ConfigurationInterface $configuration): void
70
    {
71 3084
        $this->config = $configuration;
72 3084
    }
73
74 3
    public function getXmlTagName(Node $node): string
75
    {
76 3
        return 'image';
77
    }
78
79
    /**
80
     * @param Image $node
81
     *
82
     * @return array<string, scalar>
83
     *
84
     * @psalm-suppress MoreSpecificImplementedParamType
85
     */
86 3
    public function getXmlAttributes(Node $node): array
87
    {
88 3
        Image::assertInstanceOf($node);
89
90
        return [
91 3
            'destination' => $node->getUrl(),
92 3
            'title' => $node->getTitle() ?? '',
93
        ];
94
    }
95
}
96