1<?php
2
3/*
4 * This file is part of the league/commonmark package.
5 *
6 * (c) Colin O'Dell <colinodell@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace League\CommonMark\Extension\DisallowedRawHtml;
13
14use League\CommonMark\ElementRendererInterface;
15use League\CommonMark\Inline\Element\AbstractInline;
16use League\CommonMark\Inline\Renderer\InlineRendererInterface;
17use League\CommonMark\Util\ConfigurationAwareInterface;
18use League\CommonMark\Util\ConfigurationInterface;
19
20final class DisallowedRawHtmlInlineRenderer implements InlineRendererInterface, ConfigurationAwareInterface
21{
22    /** @var InlineRendererInterface */
23    private $htmlInlineRenderer;
24
25    public function __construct(InlineRendererInterface $htmlBlockRenderer)
26    {
27        $this->htmlInlineRenderer = $htmlBlockRenderer;
28    }
29
30    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
31    {
32        $rendered = $this->htmlInlineRenderer->render($inline, $htmlRenderer);
33
34        if ($rendered === '') {
35            return '';
36        }
37
38        // Match these types of tags: <title> </title> <title x="sdf"> <title/> <title />
39        return preg_replace('/<(\/?(?:title|textarea|style|xmp|iframe|noembed|noframes|script|plaintext)[ \/>])/i', '&lt;$1', $rendered);
40    }
41
42    public function setConfiguration(ConfigurationInterface $configuration)
43    {
44        if ($this->htmlInlineRenderer instanceof ConfigurationAwareInterface) {
45            $this->htmlInlineRenderer->setConfiguration($configuration);
46        }
47    }
48}
49