176820ead4d0000a634dd294103029778eef010c
[yaffs-website] / vendor / twig / twig / lib / Twig / TokenParser / AutoEscape.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * Marks a section of a template to be escaped or not.
14  *
15  * <pre>
16  * {% autoescape true %}
17  *   Everything will be automatically escaped in this block
18  * {% endautoescape %}
19  *
20  * {% autoescape false %}
21  *   Everything will be outputed as is in this block
22  * {% endautoescape %}
23  *
24  * {% autoescape true js %}
25  *   Everything will be automatically escaped in this block
26  *   using the js escaping strategy
27  * {% endautoescape %}
28  * </pre>
29  *
30  * @final
31  */
32 class Twig_TokenParser_AutoEscape extends Twig_TokenParser
33 {
34     public function parse(Twig_Token $token)
35     {
36         $lineno = $token->getLine();
37         $stream = $this->parser->getStream();
38
39         if ($stream->test(Twig_Token::BLOCK_END_TYPE)) {
40             $value = 'html';
41         } else {
42             $expr = $this->parser->getExpressionParser()->parseExpression();
43             if (!$expr instanceof Twig_Node_Expression_Constant) {
44                 throw new Twig_Error_Syntax('An escaping strategy must be a string or a bool.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
45             }
46             $value = $expr->getAttribute('value');
47
48             $compat = true === $value || false === $value;
49
50             if (true === $value) {
51                 $value = 'html';
52             }
53
54             if ($compat && $stream->test(Twig_Token::NAME_TYPE)) {
55                 @trigger_error('Using the autoescape tag with "true" or "false" before the strategy name is deprecated since version 1.21.', E_USER_DEPRECATED);
56
57                 if (false === $value) {
58                     throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
59                 }
60
61                 $value = $stream->next()->getValue();
62             }
63         }
64
65         $stream->expect(Twig_Token::BLOCK_END_TYPE);
66         $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
67         $stream->expect(Twig_Token::BLOCK_END_TYPE);
68
69         return new Twig_Node_AutoEscape($value, $body, $lineno, $this->getTag());
70     }
71
72     public function decideBlockEnd(Twig_Token $token)
73     {
74         return $token->test('endautoescape');
75     }
76
77     public function getTag()
78     {
79         return 'autoescape';
80     }
81 }