[991] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * This file is part of Twig. |
---|
| 5 | * |
---|
| 6 | * (c) 2009 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 | class Twig_TokenParser_AutoEscape extends Twig_TokenParser |
---|
| 31 | { |
---|
| 32 | /** |
---|
| 33 | * Parses a token and returns a node. |
---|
| 34 | * |
---|
| 35 | * @param Twig_Token $token A Twig_Token instance |
---|
| 36 | * |
---|
| 37 | * @return Twig_NodeInterface A Twig_NodeInterface instance |
---|
| 38 | */ |
---|
| 39 | public function parse(Twig_Token $token) |
---|
| 40 | { |
---|
| 41 | $lineno = $token->getLine(); |
---|
| 42 | $stream = $this->parser->getStream(); |
---|
| 43 | |
---|
| 44 | if ($stream->test(Twig_Token::BLOCK_END_TYPE)) { |
---|
| 45 | $value = 'html'; |
---|
| 46 | } else { |
---|
| 47 | $expr = $this->parser->getExpressionParser()->parseExpression(); |
---|
| 48 | if (!$expr instanceof Twig_Node_Expression_Constant) { |
---|
| 49 | throw new Twig_Error_Syntax('An escaping strategy must be a string or a Boolean.', $stream->getCurrent()->getLine(), $stream->getFilename()); |
---|
| 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(Twig_Token::NAME_TYPE)) { |
---|
| 60 | if (false === $value) { |
---|
| 61 | throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $stream->getCurrent()->getLine(), $stream->getFilename()); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | $value = $stream->next()->getValue(); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | $stream->expect(Twig_Token::BLOCK_END_TYPE); |
---|
| 69 | $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); |
---|
| 70 | $stream->expect(Twig_Token::BLOCK_END_TYPE); |
---|
| 71 | |
---|
| 72 | return new Twig_Node_AutoEscape($value, $body, $lineno, $this->getTag()); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | public function decideBlockEnd(Twig_Token $token) |
---|
| 76 | { |
---|
| 77 | return $token->test('endautoescape'); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Gets the tag name associated with this token parser. |
---|
| 82 | * |
---|
| 83 | * @return string The tag name |
---|
| 84 | */ |
---|
| 85 | public function getTag() |
---|
| 86 | { |
---|
| 87 | return 'autoescape'; |
---|
| 88 | } |
---|
| 89 | } |
---|