| 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 |
* 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\Node\Node;
|
|
|
17 |
use League\CommonMark\Renderer\ChildNodeRendererInterface;
|
|
|
18 |
use League\CommonMark\Renderer\NodeRendererInterface;
|
|
|
19 |
use League\Config\ConfigurationAwareInterface;
|
|
|
20 |
use League\Config\ConfigurationInterface;
|
|
|
21 |
|
|
|
22 |
final class DisallowedRawHtmlRenderer implements NodeRendererInterface, ConfigurationAwareInterface
|
|
|
23 |
{
|
|
|
24 |
/** @psalm-readonly */
|
|
|
25 |
private NodeRendererInterface $innerRenderer;
|
|
|
26 |
|
|
|
27 |
/** @psalm-readonly-allow-private-mutation */
|
|
|
28 |
private ConfigurationInterface $config;
|
|
|
29 |
|
|
|
30 |
public function __construct(NodeRendererInterface $innerRenderer)
|
|
|
31 |
{
|
|
|
32 |
$this->innerRenderer = $innerRenderer;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function render(Node $node, ChildNodeRendererInterface $childRenderer): ?string
|
|
|
36 |
{
|
|
|
37 |
$rendered = (string) $this->innerRenderer->render($node, $childRenderer);
|
|
|
38 |
|
|
|
39 |
if ($rendered === '') {
|
|
|
40 |
return '';
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
$tags = (array) $this->config->get('disallowed_raw_html/disallowed_tags');
|
|
|
44 |
if (\count($tags) === 0) {
|
|
|
45 |
return $rendered;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$regex = \sprintf('/<(\/?(?:%s)[ \/>])/i', \implode('|', \array_map('preg_quote', $tags)));
|
|
|
49 |
|
|
|
50 |
// Match these types of tags: <title> </title> <title x="sdf"> <title/> <title />
|
|
|
51 |
return \preg_replace($regex, '<$1', $rendered);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public function setConfiguration(ConfigurationInterface $configuration): void
|
|
|
55 |
{
|
|
|
56 |
$this->config = $configuration;
|
|
|
57 |
|
|
|
58 |
if ($this->innerRenderer instanceof ConfigurationAwareInterface) {
|
|
|
59 |
$this->innerRenderer->setConfiguration($configuration);
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
}
|