[BUGFIX] Optimize implementation of CompilableInterface
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / HtmlspecialcharsViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
14 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
15
16
17 /**
18 * Applies htmlspecialchars() escaping to a value
19 * @see http://www.php.net/manual/function.htmlspecialchars.php
20 *
21 * = Examples =
22 *
23 * <code title="default notation">
24 * <f:format.htmlspecialchars>{text}</f:format.htmlspecialchars>
25 * </code>
26 * <output>
27 * Text with & " ' < > * replaced by HTML entities (htmlspecialchars applied).
28 * </output>
29 *
30 * <code title="inline notation">
31 * {text -> f:format.htmlspecialchars(encoding: 'ISO-8859-1')}
32 * </code>
33 * <output>
34 * Text with & " ' < > * replaced by HTML entities (htmlspecialchars applied).
35 * </output>
36 *
37 * @api
38 */
39 class HtmlspecialcharsViewHelper extends AbstractEncodingViewHelper implements CompilableInterface {
40
41 /**
42 * Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
43 * can decode the text's entities.
44 *
45 * @var bool
46 */
47 protected $escapingInterceptorEnabled = FALSE;
48
49 /**
50 * Escapes special characters with their escaped counterparts as needed using PHPs htmlspecialchars() function.
51 *
52 * @param string $value string to format
53 * @param bool $keepQuotes if TRUE, single and double quotes won't be replaced (sets ENT_NOQUOTES flag)
54 * @param string $encoding
55 * @param bool $doubleEncode If FALSE existing html entities won't be encoded, the default is to convert everything.
56 * @return string the altered string
57 * @see http://www.php.net/manual/function.htmlspecialchars.php
58 * @api
59 */
60 public function render($value = NULL, $keepQuotes = FALSE, $encoding = NULL, $doubleEncode = TRUE) {
61 if ($value === NULL) {
62 $value = $this->renderChildren();
63 }
64 if (!is_string($value)) {
65 return $value;
66 }
67 if ($encoding === NULL) {
68 $encoding = self::resolveDefaultEncoding();
69 }
70 $flags = $keepQuotes ? ENT_NOQUOTES : ENT_COMPAT;
71 return htmlspecialchars($value, $flags, $encoding, $doubleEncode);
72 }
73
74 /**
75 * This ViewHelper is used a *lot* because it is used by the escape interceptor.
76 * Therefore we render it to raw PHP code during compilation
77 *
78 * @param string $argumentsVariableName
79 * @param string $renderChildrenClosureVariableName
80 * @param string $initializationPhpCode
81 * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode
82 * @param \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler
83 * @return string
84 */
85 public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler) {
86 $valueVariableName = $templateCompiler->variableName('value');
87 $initializationPhpCode .= sprintf('%s = (%s[\'value\'] !== NULL ? %s[\'value\'] : %s());', $valueVariableName, $argumentsVariableName, $argumentsVariableName, $renderChildrenClosureVariableName) . LF;
88
89 return sprintf('(!is_string(%s) ? %s : htmlspecialchars(%s, (%s[\'keepQuotes\'] ? ENT_NOQUOTES : ENT_COMPAT), (%s[\'encoding\'] !== NULL ? %s[\'encoding\'] : \\TYPO3\\CMS\\Fluid\\Core\\Compiler\\AbstractCompiledTemplate::resolveDefaultEncoding()), %s[\'doubleEncode\']))',
90 $valueVariableName, $valueVariableName, $valueVariableName, $argumentsVariableName, $argumentsVariableName, $argumentsVariableName, $argumentsVariableName);
91 }
92
93 }