|
0
|
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 |
$value = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue(); |
|
|
43 |
if (!in_array($value, array('true', 'false'))) { |
|
|
44 |
throw new Twig_Error_Syntax("Autoescape value must be 'true' or 'false'", $lineno); |
|
|
45 |
} |
|
|
46 |
$value = 'true' === $value ? 'html' : false; |
|
|
47 |
|
|
|
48 |
if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE)) { |
|
|
49 |
if (false === $value) { |
|
|
50 |
throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $lineno); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
$value = $this->parser->getStream()->next()->getValue(); |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); |
|
|
57 |
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); |
|
|
58 |
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); |
|
|
59 |
|
|
|
60 |
return new Twig_Node_AutoEscape($value, $body, $lineno, $this->getTag()); |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
public function decideBlockEnd(Twig_Token $token) |
|
|
64 |
{ |
|
|
65 |
return $token->test('endautoescape'); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
/** |
|
|
69 |
* Gets the tag name associated with this token parser. |
|
|
70 |
* |
|
|
71 |
* @param string The tag name |
|
|
72 |
*/ |
|
|
73 |
public function getTag() |
|
|
74 |
{ |
|
|
75 |
return 'autoescape'; |
|
|
76 |
} |
|
|
77 |
} |