Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 <colinodell@gmail.com>
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\Inline\Newline;
21
use League\CommonMark\Node\Node;
22
use League\CommonMark\Node\NodeIterator;
23
use League\CommonMark\Node\StringContainerInterface;
24
use League\CommonMark\Renderer\ChildNodeRendererInterface;
25
use League\CommonMark\Renderer\NodeRendererInterface;
26
use League\CommonMark\Util\HtmlElement;
27
use League\CommonMark\Util\RegexHelper;
28
use League\CommonMark\Xml\XmlNodeRendererInterface;
29
use League\Config\ConfigurationAwareInterface;
30
use League\Config\ConfigurationInterface;
31
 
32
final class ImageRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
33
{
34
    /** @psalm-readonly-allow-private-mutation */
35
    private ConfigurationInterface $config;
36
 
37
    /**
38
     * @param Image $node
39
     *
40
     * {@inheritDoc}
41
     *
42
     * @psalm-suppress MoreSpecificImplementedParamType
43
     */
44
    public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
45
    {
46
        Image::assertInstanceOf($node);
47
 
48
        $attrs = $node->data->get('attributes');
49
 
50
        $forbidUnsafeLinks = ! $this->config->get('allow_unsafe_links');
51
        if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($node->getUrl())) {
52
            $attrs['src'] = '';
53
        } else {
54
            $attrs['src'] = $node->getUrl();
55
        }
56
 
57
        $attrs['alt'] = $this->getAltText($node);
58
 
59
        if (($title = $node->getTitle()) !== null) {
60
            $attrs['title'] = $title;
61
        }
62
 
63
        return new HtmlElement('img', $attrs, '', true);
64
    }
65
 
66
    public function setConfiguration(ConfigurationInterface $configuration): void
67
    {
68
        $this->config = $configuration;
69
    }
70
 
71
    public function getXmlTagName(Node $node): string
72
    {
73
        return 'image';
74
    }
75
 
76
    /**
77
     * @param Image $node
78
     *
79
     * @return array<string, scalar>
80
     *
81
     * @psalm-suppress MoreSpecificImplementedParamType
82
     */
83
    public function getXmlAttributes(Node $node): array
84
    {
85
        Image::assertInstanceOf($node);
86
 
87
        return [
88
            'destination' => $node->getUrl(),
89
            'title' => $node->getTitle() ?? '',
90
        ];
91
    }
92
 
93
    private function getAltText(Image $node): string
94
    {
95
        $altText = '';
96
 
97
        foreach ((new NodeIterator($node)) as $n) {
98
            if ($n instanceof StringContainerInterface) {
99
                $altText .= $n->getLiteral();
100
            } elseif ($n instanceof Newline) {
101
                $altText .= "\n";
102
            }
103
        }
104
 
105
        return $altText;
106
    }
107
}