Passed
Push — latest ( d59ff4...6fb795 )
by Colin
08:12
created

DisallowedRawHtmlRenderer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 5
eloc 9
dl 0
loc 33
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A render() 0 10 2
A setConfiguration() 0 4 2
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
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\DisallowedRawHtml;
15
16
use League\CommonMark\Configuration\ConfigurationAwareInterface;
17
use League\CommonMark\Configuration\ConfigurationInterface;
18
use League\CommonMark\Node\Node;
19
use League\CommonMark\Renderer\ChildNodeRendererInterface;
20
use League\CommonMark\Renderer\NodeRendererInterface;
21
22
final class DisallowedRawHtmlRenderer implements NodeRendererInterface, ConfigurationAwareInterface
23
{
24
    /**
25
     * @var NodeRendererInterface
26
     *
27
     * @psalm-readonly
28
     */
29
    private $innerRenderer;
30
31 117
    public function __construct(NodeRendererInterface $innerRenderer)
32
    {
33 117
        $this->innerRenderer = $innerRenderer;
34 117
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 9
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
40
    {
41 9
        $rendered = (string) $this->innerRenderer->render($node, $childRenderer);
42
43 9
        if ($rendered === '') {
44
            return '';
45
        }
46
47
        // Match these types of tags: <title> </title> <title x="sdf"> <title/> <title />
48 9
        return \preg_replace('/<(\/?(?:title|textarea|style|xmp|iframe|noembed|noframes|script|plaintext)[ \/>])/i', '&lt;$1', $rendered);
49
    }
50
51 117
    public function setConfiguration(ConfigurationInterface $configuration): void
52
    {
53 117
        if ($this->innerRenderer instanceof ConfigurationAwareInterface) {
54 117
            $this->innerRenderer->setConfiguration($configuration);
55
        }
56 117
    }
57
}
58