-
Notifications
You must be signed in to change notification settings - Fork 92
/
StripTagsViewHelper.php
118 lines (107 loc) · 3.25 KB
/
StripTagsViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\ViewHelpers\Format;
use Stringable;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
/**
* Removes tags from the given string (applying PHPs :php:`strip_tags()` function)
* See https://www.php.net/manual/function.strip-tags.php.
*
* Examples
* ========
*
* Default notation
* ----------------
*
* ::
*
* <f:format.stripTags>Some Text with <b>Tags</b> and an Ümlaut.</f:format.stripTags>
*
* Some Text with Tags and an Ümlaut. :php:`strip_tags()` applied.
*
* .. note::
* Encoded entities are not decoded.
*
* Default notation with allowedTags
* ---------------------------------
*
* ::
*
* <f:format.stripTags allowedTags="<p><span><div><script>">
* <p>paragraph</p><span>span</span><div>divider</div><iframe>iframe</iframe><script>script</script>
* </f:format.stripTags>
*
* Output::
*
* <p>paragraph</p><span>span</span><div>divider</div>iframe<script>script</script>
*
* Inline notation
* ---------------
*
* ::
*
* {text -> f:format.stripTags()}
*
* Text without tags :php:`strip_tags()` applied.
*
* Inline notation with allowedTags
* --------------------------------
*
* ::
*
* {text -> f:format.stripTags(allowedTags: "<p><span><div><script>")}
*
* Text with p, span, div and script Tags inside, all other tags are removed.
*/
final class StripTagsViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;
/**
* No output escaping as some tags may be allowed
*
* @var bool
*/
protected $escapeOutput = false;
public function initializeArguments(): void
{
$this->registerArgument('value', 'string', 'string to format');
$this->registerArgument('allowedTags', 'string', 'Optional string of allowed tags as required by PHPs strip_tags() function');
}
/**
* To ensure all tags are removed, child node's output must not be escaped
*
* @var bool
*/
protected $escapeChildren = false;
/**
* Applies strip_tags() on the specified value if it's string-able.
*
* @see https://www.php.net/manual/function.strip-tags.php
* @return string
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{
$value = $renderChildrenClosure();
$allowedTags = $arguments['allowedTags'];
if (is_array($value)) {
throw new \InvalidArgumentException('Specified array cannot be converted to string.', 1700819707);
}
if (is_object($value) && !($value instanceof Stringable)) {
throw new \InvalidArgumentException('Specified object cannot be converted to string.', 1700819706);
}
return strip_tags((string)$value, $allowedTags);
}
/**
* Explicitly set argument name to be used as content.
*/
public function resolveContentArgumentName(): string
{
return 'value';
}
}