4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Marks a section of a template to be escaped or not.
16 * {% autoescape true %}
17 * Everything will be automatically escaped in this block
20 * {% autoescape false %}
21 * Everything will be outputed as is in this block
24 * {% autoescape true js %}
25 * Everything will be automatically escaped in this block
26 * using the js escaping strategy
32 class Twig_TokenParser_AutoEscape extends Twig_TokenParser
34 public function parse(Twig_Token $token)
36 $lineno = $token->getLine();
37 $stream = $this->parser->getStream();
39 if ($stream->test(Twig_Token::BLOCK_END_TYPE)) {
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());
46 $value = $expr->getAttribute('value');
48 $compat = true === $value || false === $value;
50 if (true === $value) {
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);
57 if (false === $value) {
58 throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
61 $value = $stream->next()->getValue();
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);
69 return new Twig_Node_AutoEscape($value, $body, $lineno, $this->getTag());
72 public function decideBlockEnd(Twig_Token $token)
74 return $token->test('endautoescape');
77 public function getTag()
83 class_alias('Twig_TokenParser_AutoEscape', 'Twig\TokenParser\AutoEscapeTokenParser', false);