Commit e7f03862 authored by Taddeus Kroes's avatar Taddeus Kroes

Some bugfixes in Template class:

- Multiline bracket notation is no longer allowed for expressions because of
  JavaScript/CSS clashes.
- Variables in expressions can now be preceded by characters unequal to `$',
  which will be prefixed to the variable evaluation.
parent 97c7d6c3
...@@ -188,7 +188,7 @@ class Template extends Node { ...@@ -188,7 +188,7 @@ class Template extends Node {
$line_count += substr_count($before, "\n"); $line_count += substr_count($before, "\n");
// Everything before the new block belongs to its parent // Everything before the new block belongs to its parent
$current->add('html')->set('content', $before); $html = $current->add('html')->set('content', $before);
if( $brackets_content == 'end' ) { if( $brackets_content == 'end' ) {
// {end} encountered, go one level up in the tree // {end} encountered, go one level up in the tree
...@@ -201,6 +201,9 @@ class Template extends Node { ...@@ -201,6 +201,9 @@ class Template extends Node {
$block_name = substr($brackets_content, 6); $block_name = substr($brackets_content, 6);
// Go one level deeper into the tree // Go one level deeper into the tree
$current = $current->add('block')->set('name', $block_name); $current = $current->add('block')->set('name', $block_name);
} elseif( strpos($brackets_content, "\n") !== false ) {
// Bracket content contains newlines, so it is probably JavaScript or CSS
$html->set('content', $before . '{' . $brackets_content . '}');
} else { } else {
// Variable or something else // Variable or something else
$current->add('expression')->set('content', $brackets_content); $current->add('expression')->set('content', $brackets_content);
...@@ -213,7 +216,7 @@ class Template extends Node { ...@@ -213,7 +216,7 @@ class Template extends Node {
throw new ParseError($this, 'missing {end}', $line_count + 1); throw new ParseError($this, 'missing {end}', $line_count + 1);
// Add the last remaining content to the root node // Add the last remaining content to the root node
$root->add('html')->set('content', $after); $after && $root->add('html')->set('content', $after);
$this->root_block = $root; $this->root_block = $root;
} }
...@@ -272,12 +275,13 @@ class Template extends Node { ...@@ -272,12 +275,13 @@ class Template extends Node {
* @throws \UnexpectedValueException In some other error situations. * @throws \UnexpectedValueException In some other error situations.
*/ */
private static function evaluate_variable(array $matches, Node $data) { private static function evaluate_variable(array $matches, Node $data) {
$variable = $matches[1]; $before = $matches[1];
$variable = $matches[2];
$value = $data->get($variable); $value = $data->get($variable);
if( count($matches) == 3 ) { if( count($matches) == 4 ) {
// $<name>.<name> // $<name>.<name>
$attribute = $matches[2]; $attribute = $matches[3];
if( $value === null ) { if( $value === null ) {
throw new \UnexpectedValueException( throw new \UnexpectedValueException(
...@@ -300,9 +304,9 @@ class Template extends Node { ...@@ -300,9 +304,9 @@ class Template extends Node {
} else { } else {
$attr_error('variable is no array or object'); $attr_error('variable is no array or object');
} }
} elseif( count($matches) == 4 ) { } elseif( count($matches) == 5 ) {
// $<name>.<name>() // $<name>.<name>()
$method = $matches[2]; $method = $matches[3];
if( $value === null ) { if( $value === null ) {
throw new \UnexpectedValueException( throw new \UnexpectedValueException(
...@@ -324,7 +328,7 @@ class Template extends Node { ...@@ -324,7 +328,7 @@ class Template extends Node {
} }
} }
return $value; return $before . $value;
} }
/** /**
...@@ -416,7 +420,7 @@ class Template extends Node { ...@@ -416,7 +420,7 @@ class Template extends Node {
if( preg_match("/^([^?]*?)\s*\?([^:]*)(?::(.*))?$/", $expression, $matches) ) { if( preg_match("/^([^?]*?)\s*\?([^:]*)(?::(.*))?$/", $expression, $matches) ) {
// <nested_exp>?<nested_exp> | <nested_exp>?<nested_exp>:<nested_exp> // <nested_exp>?<nested_exp> | <nested_exp>?<nested_exp>:<nested_exp>
return self::evaluate_condition($matches, $data); return self::evaluate_condition($matches, $data);
} elseif( preg_match("/^\\$($name)(?:\.($name)(\(\))?)?$/", $expression, $matches) ) { } elseif( preg_match("/^(.*?)\\$($name)(?:\.($name)(\(\))?)?$/", $expression, $matches) ) {
// $<name> | $<name>.<name> | $<name>.<name>() // $<name> | $<name>.<name> | $<name>.<name>()
return self::evaluate_variable($matches, $data); return self::evaluate_variable($matches, $data);
} elseif( preg_match("/^($function)\((.+?)\)?$/", $expression, $matches) ) { } elseif( preg_match("/^($function)\((.+?)\)?$/", $expression, $matches) ) {
......
...@@ -2,4 +2,7 @@ foo ...@@ -2,4 +2,7 @@ foo
my_foobar_variable my_foobar_variable
bar bar
my_foobaz_variable my_foobaz_variable
baz baz
\ No newline at end of file {
no_variable
}
\ No newline at end of file
...@@ -2,4 +2,7 @@ foo ...@@ -2,4 +2,7 @@ foo
{$foobar} {$foobar}
bar bar
{strtolower($foobaz)} {strtolower($foobaz)}
baz baz
\ No newline at end of file {
no_variable
}
\ No newline at end of file
...@@ -142,12 +142,11 @@ class TemplateTest extends PHPUnit_Framework_TestCase { ...@@ -142,12 +142,11 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
function test_parse_blocks_blocks() { function test_parse_blocks_blocks() {
$tpl = new Template('blocks'); $tpl = new Template('blocks');
$root_block = $this->get_property($tpl, 'root_block'); $root_block = $this->get_property($tpl, 'root_block');
$this->assert_is_block_node($root_block, null, 3); $this->assert_is_block_node($root_block, null, 2);
list($before, $foo, $after) = $root_block->get_children(); list($before, $foo) = $root_block->get_children();
$this->assert_is_html_node($before, ''); $this->assert_is_html_node($before, '');
$this->assert_is_block_node($foo, 'foo', 3); $this->assert_is_block_node($foo, 'foo', 3);
$this->assert_is_html_node($after, '');
list($foofoo, $bar, $foobaz) = $foo->get_children(); list($foofoo, $bar, $foobaz) = $foo->get_children();
$this->assert_is_html_node($foofoo, "\nfoofoo\n\t"); $this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
...@@ -189,7 +188,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase { ...@@ -189,7 +188,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
$this->assert_is_exp_node($foobar, '$foobar'); $this->assert_is_exp_node($foobar, '$foobar');
$this->assert_is_html_node($bar, "\nbar\n"); $this->assert_is_html_node($bar, "\nbar\n");
$this->assert_is_exp_node($foobaz, 'strtolower($foobaz)'); $this->assert_is_exp_node($foobaz, 'strtolower($foobaz)');
$this->assert_is_html_node($baz, "\nbaz"); $this->assert_is_html_node($baz, "\nbaz\n{\nno_variable\n}");
} }
/** /**
...@@ -304,7 +303,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase { ...@@ -304,7 +303,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
* @depends test_evaluate_condition_if * @depends test_evaluate_condition_if
* @depends test_evaluate_condition_if_else * @depends test_evaluate_condition_if_else
*/ */
function test_evaluate_condition_spaces() { function test_evaluate_condition_extended() {
$this->assert_evaluates(' bar ', '$true? bar : baz'); $this->assert_evaluates(' bar ', '$true? bar : baz');
$this->assert_evaluates(' baz', '$false? bar : baz'); $this->assert_evaluates(' baz', '$false? bar : baz');
...@@ -313,6 +312,8 @@ class TemplateTest extends PHPUnit_Framework_TestCase { ...@@ -313,6 +312,8 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
$this->assert_evaluates(' Foo bar ', '$true ? Foo bar : Baz foo'); $this->assert_evaluates(' Foo bar ', '$true ? Foo bar : Baz foo');
$this->assert_evaluates(' Baz foo', '$false ? Foo bar : Baz foo'); $this->assert_evaluates(' Baz foo', '$false ? Foo bar : Baz foo');
$this->assert_evaluates('| bar', '$true ?| $foo');
} }
/** /**
...@@ -343,7 +344,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase { ...@@ -343,7 +344,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
/** /**
* @depends test_evaluate_variable_success * @depends test_evaluate_variable_success
* @depends test_evaluate_no_expression * @depends test_evaluate_no_expression
* @depends test_evaluate_condition_spaces * @depends test_evaluate_condition_extended
* @depends test_evaluate_function_success * @depends test_evaluate_function_success
* @depends test_evaluate_default_value * @depends test_evaluate_default_value
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment