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\Link;
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 LinkRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
30
{
31
    /** @psalm-readonly-allow-private-mutation */
32
    private ConfigurationInterface $config;
33
 
34
    /**
35
     * @param Link $node
36
     *
37
     * {@inheritDoc}
38
     *
39
     * @psalm-suppress MoreSpecificImplementedParamType
40
     */
41
    public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
42
    {
43
        Link::assertInstanceOf($node);
44
 
45
        $attrs = $node->data->get('attributes');
46
 
47
        $forbidUnsafeLinks = ! $this->config->get('allow_unsafe_links');
48
        if (! ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($node->getUrl()))) {
49
            $attrs['href'] = $node->getUrl();
50
        }
51
 
52
        if (($title = $node->getTitle()) !== null) {
53
            $attrs['title'] = $title;
54
        }
55
 
56
        if (isset($attrs['target']) && $attrs['target'] === '_blank' && ! isset($attrs['rel'])) {
57
            $attrs['rel'] = 'noopener noreferrer';
58
        }
59
 
60
        return new HtmlElement('a', $attrs, $childRenderer->renderNodes($node->children()));
61
    }
62
 
63
    public function setConfiguration(ConfigurationInterface $configuration): void
64
    {
65
        $this->config = $configuration;
66
    }
67
 
68
    public function getXmlTagName(Node $node): string
69
    {
70
        return 'link';
71
    }
72
 
73
    /**
74
     * @param Link $node
75
     *
76
     * @return array<string, scalar>
77
     *
78
     * @psalm-suppress MoreSpecificImplementedParamType
79
     */
80
    public function getXmlAttributes(Node $node): array
81
    {
82
        Link::assertInstanceOf($node);
83
 
84
        return [
85
            'destination' => $node->getUrl(),
86
            'title' => $node->getTitle() ?? '',
87
        ];
88
    }
89
}