0495f1200b8f5d4c47c03218a61ba354f64a4d75
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / HtmlentitiesViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
19
20 /**
21 * Applies htmlentities() escaping to a value
22 * @see http://www.php.net/manual/function.htmlentities.php
23 *
24 * = Examples =
25 *
26 * <code title="default notation">
27 * <f:format.htmlentities>{text}</f:format.htmlentities>
28 * </code>
29 * <output>
30 * Text with & " ' < > * replaced by HTML entities (htmlentities applied).
31 * </output>
32 *
33 * <code title="inline notation">
34 * {text -> f:format.htmlentities(encoding: 'ISO-8859-1')}
35 * </code>
36 * <output>
37 * Text with & " ' < > * replaced by HTML entities (htmlentities applied).
38 * </output>
39 *
40 * @api
41 */
42 class HtmlentitiesViewHelper extends AbstractEncodingViewHelper
43 {
44 use CompileWithContentArgumentAndRenderStatic;
45
46 /**
47 * Output gets encoded by this viewhelper
48 *
49 * @var bool
50 */
51 protected $escapeOutput = false;
52
53 /**
54 * This prevents double encoding as the whole output gets encoded at the end
55 *
56 * @var bool
57 */
58 protected $escapeChildren = false;
59
60 /**
61 * Initialize ViewHelper arguments
62 */
63 public function initializeArguments()
64 {
65 $this->registerArgument('value', 'string', 'string to format');
66 $this->registerArgument('keepQuotes', 'bool', 'If TRUE, single and double quotes won\'t be replaced (sets ENT_NOQUOTES flag).', false, false);
67 $this->registerArgument('encoding', 'string', '');
68 $this->registerArgument('doubleEncode', 'bool', 'If FALSE existing html entities won\'t be encoded, the default is to convert everything.', false, true);
69 }
70
71 /**
72 * Escapes special characters with their escaped counterparts as needed using PHPs htmlentities() function.
73 *
74 * @see http://www.php.net/manual/function.htmlentities.php
75 * @param array $arguments
76 * @param \Closure $renderChildrenClosure
77 * @param RenderingContextInterface $renderingContext
78 * @return mixed
79 */
80 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
81 {
82 $value = $renderChildrenClosure();
83 $encoding = $arguments['encoding'];
84 $keepQuotes = $arguments['keepQuotes'];
85 $doubleEncode = $arguments['doubleEncode'];
86
87 if (!is_string($value)) {
88 return $value;
89 }
90 if ($encoding === null) {
91 $encoding = self::resolveDefaultEncoding();
92 }
93 $flags = $keepQuotes ? ENT_NOQUOTES : ENT_COMPAT;
94 return htmlentities($value, $flags, $encoding, $doubleEncode);
95 }
96 }