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\Block\Element\AbstractBlock;
15use League\CommonMark\Block\Renderer\BlockRendererInterface;
16use League\CommonMark\ElementRendererInterface;
17use League\CommonMark\Util\ConfigurationAwareInterface;
18use League\CommonMark\Util\ConfigurationInterface;
19
20final class DisallowedRawHtmlBlockRenderer implements BlockRendererInterface, ConfigurationAwareInterface
21{
22    /** @var BlockRendererInterface */
23    private $htmlBlockRenderer;
24
25    public function __construct(BlockRendererInterface $htmlBlockRenderer)
26    {
27        $this->htmlBlockRenderer = $htmlBlockRenderer;
28    }
29
30    public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
31    {
32        $rendered = $this->htmlBlockRenderer->render($block, $htmlRenderer, $inTightList);
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->htmlBlockRenderer instanceof ConfigurationAwareInterface) {
45            $this->htmlBlockRenderer->setConfiguration($configuration);
46        }
47    }
48}
49