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
12namespace Twig\TokenParser;
13
14use Twig\Error\SyntaxError;
15use Twig\Node\AutoEscapeNode;
16use Twig\Node\Expression\ConstantExpression;
17use Twig\Token;
18
19/**
20 * Marks a section of a template to be escaped or not.
21 *
22 *   {% autoescape true %}
23 *     Everything will be automatically escaped in this block
24 *   {% endautoescape %}
25 *
26 *   {% autoescape false %}
27 *     Everything will be outputed as is in this block
28 *   {% endautoescape %}
29 *
30 *   {% autoescape true js %}
31 *     Everything will be automatically escaped in this block
32 *     using the js escaping strategy
33 *   {% endautoescape %}
34 *
35 * @final
36 */
37class AutoEscapeTokenParser extends AbstractTokenParser
38{
39    public function parse(Token $token)
40    {
41        $lineno = $token->getLine();
42        $stream = $this->parser->getStream();
43
44        if ($stream->test(Token::BLOCK_END_TYPE)) {
45            $value = 'html';
46        } else {
47            $expr = $this->parser->getExpressionParser()->parseExpression();
48            if (!$expr instanceof ConstantExpression) {
49                throw new SyntaxError('An escaping strategy must be a string or a bool.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
50            }
51            $value = $expr->getAttribute('value');
52
53            $compat = true === $value || false === $value;
54
55            if (true === $value) {
56                $value = 'html';
57            }
58
59            if ($compat && $stream->test(Token::NAME_TYPE)) {
60                @trigger_error('Using the autoescape tag with "true" or "false" before the strategy name is deprecated since version 1.21.', E_USER_DEPRECATED);
61
62                if (false === $value) {
63                    throw new SyntaxError('Unexpected escaping strategy as you set autoescaping to false.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
64                }
65
66                $value = $stream->next()->getValue();
67            }
68        }
69
70        $stream->expect(Token::BLOCK_END_TYPE);
71        $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
72        $stream->expect(Token::BLOCK_END_TYPE);
73
74        return new AutoEscapeNode($value, $body, $lineno, $this->getTag());
75    }
76
77    public function decideBlockEnd(Token $token)
78    {
79        return $token->test('endautoescape');
80    }
81
82    public function getTag()
83    {
84        return 'autoescape';
85    }
86}
87
88class_alias('Twig\TokenParser\AutoEscapeTokenParser', 'Twig_TokenParser_AutoEscape');
89