* 1.2.0
+ * changed name regex to match PHP one "[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*" (works for blocks, tags, functions, filters, and macros)
* removed the possibility to use the "extends" tag from a block
* added "if" modifier support to "for" loops
const STATE_BLOCK = 1;
const STATE_VAR = 2;
- const REGEX_NAME = '/[A-Za-z_][A-Za-z0-9_]*/A';
+ const REGEX_NAME = '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A';
const REGEX_NUMBER = '/[0-9]+(?:\.[0-9]+)?/A';
const REGEX_STRING = '/"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As';
const PUNCTUATION = '()[]{}?:.,|';
*/
class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
{
+ public function testNameLabelForTag()
+ {
+ $template = '{% ☃ %}';
+
+ $lexer = new Twig_Lexer(new Twig_Environment());
+ $stream = $lexer->tokenize($template);
+
+ $stream->expect(Twig_Token::BLOCK_START_TYPE);
+ $this->assertSame('☃', $stream->expect(Twig_Token::NAME_TYPE)->getValue());
+ }
+
+ public function testNameLabelForFunction()
+ {
+ $template = '{{ ☃() }}';
+
+ $lexer = new Twig_Lexer(new Twig_Environment());
+ $stream = $lexer->tokenize($template);
+
+ $stream->expect(Twig_Token::VAR_START_TYPE);
+ $this->assertSame('☃', $stream->expect(Twig_Token::NAME_TYPE)->getValue());
+ }
+
public function testBracketsNesting()
{
$template = '{{ {"a":{"b":"c"}} }}';
}
}
+class TestTokenParser_☃ extends Twig_TokenParser
+{
+ public function parse(Twig_Token $token)
+ {
+ $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
+
+ return new Twig_Node_Print(new Twig_Node_Expression_Constant('☃', -1), -1);
+ }
+
+ public function getTag()
+ {
+ return '☃';
+ }
+}
+
class TestExtension extends Twig_Extension
{
+ public function getTokenParsers()
+ {
+ return array(
+ new TestTokenParser_☃(),
+ );
+ }
+
public function getFilters()
{
return array(
+ '☃' => new Twig_Filter_Method($this, '☃Filter'),
'escape_and_nl2br' => new Twig_Filter_Method($this, 'escape_and_nl2br', array('needs_environment' => true, 'is_safe' => array('html'))),
'nl2br' => new Twig_Filter_Method($this, 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))),
'escape_something' => new Twig_Filter_Method($this, 'escape_something', array('is_safe' => array('something'))),
public function getFunctions()
{
return array(
+ '☃' => new Twig_Function_Method($this, '☃Function'),
'safe_br' => new Twig_Function_Method($this, 'br', array('is_safe' => array('html'))),
'unsafe_br' => new Twig_Function_Method($this, 'br'),
);
}
+ public function ☃Filter($value)
+ {
+ return "☃{$value}☃";
+ }
+
+ public function ☃Function($value)
+ {
+ return "☃{$value}☃";
+ }
+
/**
* nl2br which also escapes, for testing escaper filters
*/