|
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
|
|
|
private const DEFAULT_DISALLOWED_TAGS = [ |
|
25
|
|
|
'title', |
|
26
|
|
|
'textarea', |
|
27
|
|
|
'style', |
|
28
|
|
|
'xmp', |
|
29
|
|
|
'iframe', |
|
30
|
|
|
'noembed', |
|
31
|
|
|
'noframes', |
|
32
|
|
|
'script', |
|
33
|
|
|
'plaintext', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var NodeRendererInterface |
|
38
|
|
|
* |
|
39
|
|
|
* @psalm-readonly |
|
40
|
|
|
*/ |
|
41
|
|
|
private $innerRenderer; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ConfigurationInterface |
|
45
|
|
|
* |
|
46
|
|
|
* @psalm-readonly-allow-private-mutation |
|
47
|
|
|
*/ |
|
48
|
|
|
private $config; |
|
49
|
|
|
|
|
50
|
204 |
|
public function __construct(NodeRendererInterface $innerRenderer) |
|
51
|
|
|
{ |
|
52
|
204 |
|
$this->innerRenderer = $innerRenderer; |
|
53
|
204 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
96 |
|
public function render(Node $node, ChildNodeRendererInterface $childRenderer) |
|
59
|
|
|
{ |
|
60
|
96 |
|
$rendered = (string) $this->innerRenderer->render($node, $childRenderer); |
|
61
|
|
|
|
|
62
|
96 |
|
if ($rendered === '') { |
|
63
|
3 |
|
return ''; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
93 |
|
$tags = (array) $this->config->get('disallowed_raw_html/disallowed_tags', self::DEFAULT_DISALLOWED_TAGS); |
|
67
|
93 |
|
if (\count($tags) === 0) { |
|
68
|
|
|
return $rendered; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
93 |
|
$regex = \sprintf('/<(\/?(?:%s)[ \/>])/i', \implode('|', \array_map('preg_quote', $tags))); |
|
72
|
|
|
|
|
73
|
|
|
// Match these types of tags: <title> </title> <title x="sdf"> <title/> <title /> |
|
74
|
93 |
|
return \preg_replace($regex, '<$1', $rendered); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
201 |
|
public function setConfiguration(ConfigurationInterface $configuration): void |
|
78
|
|
|
{ |
|
79
|
201 |
|
$this->config = $configuration; |
|
80
|
|
|
|
|
81
|
201 |
|
if ($this->innerRenderer instanceof ConfigurationAwareInterface) { |
|
82
|
117 |
|
$this->innerRenderer->setConfiguration($configuration); |
|
83
|
|
|
} |
|
84
|
201 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|