rebase of j-applese3d:smarty5

This commit is contained in:
Simon Wisselink
2024-03-18 16:00:35 +01:00
parent 82397ec7f0
commit 23b3c21d73
5 changed files with 1470 additions and 1294 deletions

View File

@@ -131,6 +131,7 @@ class TemplateLexer
'OPENB' => '[', 'OPENB' => '[',
'CLOSEB' => ']', 'CLOSEB' => ']',
'PTR' => '->', 'PTR' => '->',
'NSPTR' => '?->',
'APTR' => '=>', 'APTR' => '=>',
'EQUAL' => '=', 'EQUAL' => '=',
'NUMBER' => 'number', 'NUMBER' => 'number',
@@ -567,7 +568,7 @@ class TemplateLexer
public function yylex3() public function yylex3()
{ {
if (!isset($this->yy_global_pattern3)) { if (!isset($this->yy_global_pattern3)) {
$this->yy_global_pattern3 = $this->replace("/\G(\\s*SMARTYrdel)|\G((SMARTYldel)SMARTYal)|\G([\"])|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|\G([$][0-9]*[a-zA-Z_]\\w*)|\G([$])|\G(\\s+is\\s+in\\s+)|\G(\\s+as\\s+)|\G(\\s+to\\s+)|\G(\\s+step\\s+)|\G(\\s+instanceof\\s+)|\G(\\s*([!=][=]{1,2}|[<][=>]?|[>][=]?|[&|]{2})\\s*)|\G(\\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even|div)\\s+by\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even))|\G([!]\\s*|not\\s+)|\G([(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\\s*)|\G(\\s*[(]\\s*)|\G(\\s*[)])|\G(\\[\\s*)|\G(\\s*\\])|\G(\\s*[-][>]\\s*)|\G(\\s*[=][>]\\s*)|\G(\\s*[=]\\s*)|\G(([+]|[-]){2})|\G(\\s*([+]|[-])\\s*)|\G(\\s*([*]{1,2}|[%\/^&]|[<>]{2})\\s*)|\G([@])|\G(array\\s*[(]\\s*)|\G([#])|\G(\\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\\s*[=]\\s*)|\G(([0-9]*[a-zA-Z_]\\w*)?(\\\\[0-9]*[a-zA-Z_]\\w*)+)|\G([0-9]*[a-zA-Z_]\\w*)|\G(\\d+)|\G([`])|\G([|][@]?)|\G([.])|\G(\\s*[,]\\s*)|\G(\\s*[;]\\s*)|\G([:]{2})|\G(\\s*[:]\\s*)|\G(\\s*[?]\\s*)|\G(0[xX][0-9a-fA-F]+)|\G(\\s+)|\G([\S\s])/isS"); $this->yy_global_pattern3 = $this->replace("/\G(\\s*SMARTYrdel)|\G((SMARTYldel)SMARTYal)|\G([\"])|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|\G([$][0-9]*[a-zA-Z_]\\w*)|\G([$])|\G(\\s+is\\s+in\\s+)|\G(\\s+as\\s+)|\G(\\s+to\\s+)|\G(\\s+step\\s+)|\G(\\s+instanceof\\s+)|\G(\\s*([!=][=]{1,2}|[<][=>]?|[>][=]?|[&|]{2})\\s*)|\G(\\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even|div)\\s+by\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even))|\G([!]\\s*|not\\s+)|\G([(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\\s*)|\G(\\s*[(]\\s*)|\G(\\s*[)])|\G(\\[\\s*)|\G(\\s*\\])|\G(\\s*[-][>]\\s*)|\G(\\s*[?][-][>]\\s*)|\G(\\s*[=][>]\\s*)|\G(\\s*[=]\\s*)|\G(([+]|[-]){2})|\G(\\s*([+]|[-])\\s*)|\G(\\s*([*]{1,2}|[%\/^&]|[<>]{2})\\s*)|\G([@])|\G(array\\s*[(]\\s*)|\G([#])|\G(\\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\\s*[=]\\s*)|\G(([0-9]*[a-zA-Z_]\\w*)?(\\\\[0-9]*[a-zA-Z_]\\w*)+)|\G([0-9]*[a-zA-Z_]\\w*)|\G(\\d+)|\G([`])|\G([|][@]?)|\G([.])|\G(\\s*[,]\\s*)|\G(\\s*[;]\\s*)|\G([:]{2})|\G(\\s*[:]\\s*)|\G(\\s*[?]\\s*)|\G(0[xX][0-9a-fA-F]+)|\G(\\s+)|\G([\S\s])/isS");
} }
if (!isset($this->dataLength)) { if (!isset($this->dataLength)) {
$this->dataLength = strlen($this->data); $this->dataLength = strlen($this->data);
@@ -737,44 +738,49 @@ class TemplateLexer
public function yy_r3_33() public function yy_r3_33()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_APTR; $this->token = \Smarty\Parser\TemplateParser::TP_NSPTR;
} }
public function yy_r3_34() public function yy_r3_34()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_EQUAL; $this->token = \Smarty\Parser\TemplateParser::TP_APTR;
} }
public function yy_r3_35() public function yy_r3_35()
{
$this->token = \Smarty\Parser\TemplateParser::TP_EQUAL;
}
public function yy_r3_36()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_INCDEC; $this->token = \Smarty\Parser\TemplateParser::TP_INCDEC;
} }
public function yy_r3_37() public function yy_r3_38()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_UNIMATH; $this->token = \Smarty\Parser\TemplateParser::TP_UNIMATH;
} }
public function yy_r3_39() public function yy_r3_40()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_MATH; $this->token = \Smarty\Parser\TemplateParser::TP_MATH;
} }
public function yy_r3_41() public function yy_r3_42()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_AT; $this->token = \Smarty\Parser\TemplateParser::TP_AT;
} }
public function yy_r3_42() public function yy_r3_43()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_ARRAYOPEN; $this->token = \Smarty\Parser\TemplateParser::TP_ARRAYOPEN;
} }
public function yy_r3_43() public function yy_r3_44()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_HATCH; $this->token = \Smarty\Parser\TemplateParser::TP_HATCH;
} }
public function yy_r3_44() public function yy_r3_45()
{ {
// resolve conflicts with shorttag and right_delimiter starting with '=' // resolve conflicts with shorttag and right_delimiter starting with '='
@@ -786,73 +792,73 @@ class TemplateLexer
$this->token = \Smarty\Parser\TemplateParser::TP_ATTR; $this->token = \Smarty\Parser\TemplateParser::TP_ATTR;
} }
} }
public function yy_r3_45() public function yy_r3_46()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_NAMESPACE; $this->token = \Smarty\Parser\TemplateParser::TP_NAMESPACE;
} }
public function yy_r3_48() public function yy_r3_49()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_ID; $this->token = \Smarty\Parser\TemplateParser::TP_ID;
} }
public function yy_r3_49() public function yy_r3_50()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_INTEGER; $this->token = \Smarty\Parser\TemplateParser::TP_INTEGER;
} }
public function yy_r3_50() public function yy_r3_51()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_BACKTICK; $this->token = \Smarty\Parser\TemplateParser::TP_BACKTICK;
$this->yypopstate(); $this->yypopstate();
} }
public function yy_r3_51() public function yy_r3_52()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_VERT; $this->token = \Smarty\Parser\TemplateParser::TP_VERT;
} }
public function yy_r3_52() public function yy_r3_53()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_DOT; $this->token = \Smarty\Parser\TemplateParser::TP_DOT;
} }
public function yy_r3_53() public function yy_r3_54()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_COMMA; $this->token = \Smarty\Parser\TemplateParser::TP_COMMA;
} }
public function yy_r3_54() public function yy_r3_55()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_SEMICOLON; $this->token = \Smarty\Parser\TemplateParser::TP_SEMICOLON;
} }
public function yy_r3_55() public function yy_r3_56()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_DOUBLECOLON; $this->token = \Smarty\Parser\TemplateParser::TP_DOUBLECOLON;
} }
public function yy_r3_56() public function yy_r3_57()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_COLON; $this->token = \Smarty\Parser\TemplateParser::TP_COLON;
} }
public function yy_r3_57() public function yy_r3_58()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_QMARK; $this->token = \Smarty\Parser\TemplateParser::TP_QMARK;
} }
public function yy_r3_58() public function yy_r3_59()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_HEX; $this->token = \Smarty\Parser\TemplateParser::TP_HEX;
} }
public function yy_r3_59() public function yy_r3_60()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_SPACE; $this->token = \Smarty\Parser\TemplateParser::TP_SPACE;
} }
public function yy_r3_60() public function yy_r3_61()
{ {
$this->token = \Smarty\Parser\TemplateParser::TP_TEXT; $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;

View File

@@ -131,6 +131,7 @@ class TemplateLexer
'OPENB' => '[', 'OPENB' => '[',
'CLOSEB' => ']', 'CLOSEB' => ']',
'PTR' => '->', 'PTR' => '->',
'NSPTR' => '?->',
'APTR' => '=>', 'APTR' => '=>',
'EQUAL' => '=', 'EQUAL' => '=',
'NUMBER' => 'number', 'NUMBER' => 'number',
@@ -310,6 +311,7 @@ class TemplateLexer
equal = ~\s*[=]\s*~ equal = ~\s*[=]\s*~
space = ~\s+~ space = ~\s+~
ptr = ~\s*[-][>]\s*~ ptr = ~\s*[-][>]\s*~
nsptr = ~\s*[?][-][>]\s*~
aptr = ~\s*[=][>]\s*~ aptr = ~\s*[=][>]\s*~
singlequotestring = ~'[^'\\]*(?:\\.[^'\\]*)*'~ singlequotestring = ~'[^'\\]*(?:\\.[^'\\]*)*'~
backtick = ~[`]~ backtick = ~[`]~
@@ -514,6 +516,9 @@ class TemplateLexer
ptr { ptr {
$this->token = \Smarty\Parser\TemplateParser::TP_PTR; $this->token = \Smarty\Parser\TemplateParser::TP_PTR;
} }
nsptr {
$this->token = \Smarty\Parser\TemplateParser::TP_NSPTR;
}
aptr { aptr {
$this->token = \Smarty\Parser\TemplateParser::TP_APTR; $this->token = \Smarty\Parser\TemplateParser::TP_APTR;
} }

File diff suppressed because it is too large Load Diff

View File

@@ -432,6 +432,16 @@ tag(res) ::= LDEL ID(i) PTR ID(me) modifierlist(l) attributes(a). {
res = $this->compiler->compileTag(i,a,array('modifierlist'=>l, 'object_method'=>me)); res = $this->compiler->compileTag(i,a,array('modifierlist'=>l, 'object_method'=>me));
} }
// registered ns object tag
tag(res) ::= LDEL ID(i) NSPTR ID(m) attributes(a). {
res = $this->compiler->compileTag(i,a,array('object_method'=>m));
}
// registered ns object tag with modifiers
tag(res) ::= LDEL ID(i) NSPTR ID(me) modifierlist(l) attributes(a). {
res = $this->compiler->compileTag(i,a,array('modifierlist'=>l, 'object_method'=>me));
}
// {if}, {elseif} and {while} tag // {if}, {elseif} and {while} tag
tag(res) ::= LDELIF(i) expr(ie). { tag(res) ::= LDELIF(i) expr(ie). {
$tag = trim(substr(i,$this->compiler->getLdelLength())); $tag = trim(substr(i,$this->compiler->getLdelLength()));
@@ -1056,6 +1066,40 @@ objectelement(res)::= PTR method(f). {
res = '->'.f; res = '->'.f;
} }
// variable
objectelement(res)::= NSPTR ID(i) arrayindex(a). {
if ($this->security && substr(i,0,1) === '_') {
$this->compiler->trigger_template_error (self::ERR1);
}
res = '?->'.i.a;
}
objectelement(res)::= NSPTR varvar(v) arrayindex(a). {
if ($this->security) {
$this->compiler->trigger_template_error (self::ERR2);
}
res = '?->{'.$this->compiler->compileVariable(v).a.'}';
}
objectelement(res)::= NSPTR LDEL expr(e) RDEL arrayindex(a). {
if ($this->security) {
$this->compiler->trigger_template_error (self::ERR2);
}
res = '?->{'.e.a.'}';
}
objectelement(res)::= NSPTR ID(ii) LDEL expr(e) RDEL arrayindex(a). {
if ($this->security) {
$this->compiler->trigger_template_error (self::ERR2);
}
res = '?->{\''.ii.'\'.'.e.a.'}';
}
// method
objectelement(res)::= NSPTR method(f). {
res = '?->'.f;
}
// //
// function // function

View File

@@ -93,6 +93,33 @@ class ObjectVariableTest extends PHPUnit_Smarty
$tpl->assign('object', $object); $tpl->assign('object', $object);
$this->assertEquals('hello world', $this->smarty->fetch($tpl)); $this->assertEquals('hello world', $this->smarty->fetch($tpl));
} }
public function testNullSafeOperatorSimple()
{
$object = new VariableObject;
$tpl = $this->smarty->createTemplate('string:{$object?->hello}');
$tpl->assign('object', $object);
$this->assertEquals('hello_world', $this->smarty->fetch($tpl));
$object = null;
$tpl = $this->smarty->createTemplate('string:{$object?->hello}');
$tpl->assign('object', $object);
$this->assertEquals((string)null, $this->smarty->fetch($tpl));
}
public function testNullSafeOperatorChaining()
{
$object = new VariableObject;
$tpl = $this->smarty->createTemplate('string:{$object?->returnSelf()?->myhello()}');
$tpl->assign('object', $object);
$this->assertEquals('hello world', $this->smarty->fetch($tpl));
$tpl = $this->smarty->createTemplate('string:{$object?->returnNull()?->myhello()}');
$tpl->assign('object', $object);
$this->assertEquals((string)null, $this->smarty->fetch($tpl));
}
} }
Class VariableObject Class VariableObject
@@ -103,4 +130,14 @@ Class VariableObject
{ {
return 'hello world'; return 'hello world';
} }
public function returnSelf()
{
return $this;
}
public function returnNull()
{
return null;
}
} }