Completed
Push — master ( 7a6aee...e5c914 )
by Colin
52:42 queued 27:44
created

src/Inline/Renderer/LinkRenderer.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Link;
21
use League\CommonMark\Util\Configuration;
22
use League\CommonMark\Util\ConfigurationAwareInterface;
23
use League\CommonMark\Util\RegexHelper;
24
25
class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterface
26
{
27
    /**
28
     * @var Configuration
29
     */
30
    protected $config;
31
32
    /**
33
     * @param Link                     $inline
34
     * @param ElementRendererInterface $htmlRenderer
35
     *
36
     * @return HtmlElement
37
     */
38 348
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
39
    {
40 348
        if (!($inline instanceof Link)) {
41 3
            throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
42
        }
43
44 345
        $attrs = [];
45 345
        foreach ($inline->getData('attributes', []) as $key => $value) {
46 3
            $attrs[$key] = $htmlRenderer->escape($value, true);
0 ignored issues
show
Deprecated Code introduced by
The method League\CommonMark\Elemen...ererInterface::escape() has been deprecated.

This method has been deprecated.

Loading history...
47 230
        }
48
49 345
        $forbidUnsafeLinks = $this->config->getConfig('safe') || !$this->config->getConfig('allow_unsafe_links');
50 345
        if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl()))) {
51 333
            $attrs['href'] = $htmlRenderer->escape($inline->getUrl(), true);
0 ignored issues
show
Deprecated Code introduced by
The method League\CommonMark\Elemen...ererInterface::escape() has been deprecated.

This method has been deprecated.

Loading history...
52 222
        }
53
54 345
        if (isset($inline->data['title'])) {
55 90
            $attrs['title'] = $htmlRenderer->escape($inline->data['title'], true);
0 ignored issues
show
Deprecated Code introduced by
The method League\CommonMark\Elemen...ererInterface::escape() has been deprecated.

This method has been deprecated.

Loading history...
56 60
        }
57
58 345
        return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children()));
59
    }
60
61
    /**
62
     * @param Configuration $configuration
63
     */
64 1953
    public function setConfiguration(Configuration $configuration)
65
    {
66 1953
        $this->config = $configuration;
67 1953
    }
68
}
69