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\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
|
154 |
|
public function __construct(NodeRendererInterface $innerRenderer) |
31
|
|
|
{ |
32
|
154 |
|
$this->innerRenderer = $innerRenderer; |
33
|
|
|
} |
34
|
|
|
|
35
|
64 |
|
public function render(Node $node, ChildNodeRendererInterface $childRenderer): ?string |
36
|
|
|
{ |
37
|
64 |
|
$rendered = (string) $this->innerRenderer->render($node, $childRenderer); |
38
|
|
|
|
39
|
64 |
|
if ($rendered === '') { |
40
|
2 |
|
return ''; |
41
|
|
|
} |
42
|
|
|
|
43
|
62 |
|
$tags = (array) $this->config->get('disallowed_raw_html/disallowed_tags'); |
44
|
62 |
|
if (\count($tags) === 0) { |
45
|
|
|
return $rendered; |
46
|
|
|
} |
47
|
|
|
|
48
|
62 |
|
$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
|
62 |
|
return \preg_replace($regex, '<$1', $rendered); |
52
|
|
|
} |
53
|
|
|
|
54
|
154 |
|
public function setConfiguration(ConfigurationInterface $configuration): void |
55
|
|
|
{ |
56
|
154 |
|
$this->config = $configuration; |
57
|
|
|
|
58
|
154 |
|
if ($this->innerRenderer instanceof ConfigurationAwareInterface) { |
59
|
96 |
|
$this->innerRenderer->setConfiguration($configuration); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|