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 */ 22final class AutoEscapeTokenParser extends AbstractTokenParser 23{ 24 public function parse(Token $token) 25 { 26 $lineno = $token->getLine(); 27 $stream = $this->parser->getStream(); 28 29 if ($stream->test(/* Token::BLOCK_END_TYPE */ 3)) { 30 $value = 'html'; 31 } else { 32 $expr = $this->parser->getExpressionParser()->parseExpression(); 33 if (!$expr instanceof ConstantExpression) { 34 throw new SyntaxError('An escaping strategy must be a string or false.', $stream->getCurrent()->getLine(), $stream->getSourceContext()); 35 } 36 $value = $expr->getAttribute('value'); 37 } 38 39 $stream->expect(/* Token::BLOCK_END_TYPE */ 3); 40 $body = $this->parser->subparse([$this, 'decideBlockEnd'], true); 41 $stream->expect(/* Token::BLOCK_END_TYPE */ 3); 42 43 return new AutoEscapeNode($value, $body, $lineno, $this->getTag()); 44 } 45 46 public function decideBlockEnd(Token $token) 47 { 48 return $token->test('endautoescape'); 49 } 50 51 public function getTag() 52 { 53 return 'autoescape'; 54 } 55} 56 57class_alias('Twig\TokenParser\AutoEscapeTokenParser', 'Twig_TokenParser_AutoEscape'); 58