Throw exception when something other than text or block tags are encountered inside of a parent tag. Other small cleanups

This commit is contained in:
Dan Miller
2014-04-09 16:15:05 +00:00
parent af8a62c5e2
commit d27840f8ef
4 changed files with 38 additions and 4 deletions
+1 -1
View File
@@ -246,7 +246,7 @@ class Mustache_Compiler
$method = $this->getFindMethod($id); $method = $this->getFindMethod($id);
$id = var_export($id, true); $id = var_export($id, true);
return sprintf($this->prepare(self::BLOCK_ARG, $level), $key, $key, $id, $this->flushIndent()); return sprintf($this->prepare(self::BLOCK_ARG, $level), $id, $key, $id, $this->flushIndent());
} }
const SECTION_CALL = ' const SECTION_CALL = '
+1 -1
View File
@@ -134,7 +134,7 @@ class Mustache_Context
public function findInBlock($id) public function findInBlock($id)
{ {
foreach($this->block_stack as $context) { foreach($this->block_stack as $context) {
if (is_array($context) && array_key_exists($id, $context)) { if (array_key_exists($id, $context)) {
return $context[$id]; return $context[$id];
} }
} }
+12 -1
View File
@@ -60,11 +60,13 @@ class Mustache_Parser
switch ($token[Mustache_Tokenizer::TYPE]) { switch ($token[Mustache_Tokenizer::TYPE]) {
case Mustache_Tokenizer::T_DELIM_CHANGE: case Mustache_Tokenizer::T_DELIM_CHANGE:
$this->checkIfTokenIsAllowedInParent($parent, $token);
$this->clearStandaloneLines($nodes, $tokens); $this->clearStandaloneLines($nodes, $tokens);
break; break;
case Mustache_Tokenizer::T_SECTION: case Mustache_Tokenizer::T_SECTION:
case Mustache_Tokenizer::T_INVERTED: case Mustache_Tokenizer::T_INVERTED:
$this->checkIfTokenIsAllowedInParent($parent, $token);
$this->clearStandaloneLines($nodes, $tokens); $this->clearStandaloneLines($nodes, $tokens);
$nodes[] = $this->buildTree($tokens, $token); $nodes[] = $this->buildTree($tokens, $token);
break; break;
@@ -97,6 +99,7 @@ class Mustache_Parser
return $parent; return $parent;
case Mustache_Tokenizer::T_PARTIAL: case Mustache_Tokenizer::T_PARTIAL:
$this->checkIfTokenIsAllowedInParent($parent, $token);
//store the whitespace prefix for laters! //store the whitespace prefix for laters!
if ($indent = $this->clearStandaloneLines($nodes, $tokens)) { if ($indent = $this->clearStandaloneLines($nodes, $tokens)) {
$token[Mustache_Tokenizer::INDENT] = $indent[Mustache_Tokenizer::VALUE]; $token[Mustache_Tokenizer::INDENT] = $indent[Mustache_Tokenizer::VALUE];
@@ -105,11 +108,12 @@ class Mustache_Parser
break; break;
case Mustache_Tokenizer::T_PARENT: case Mustache_Tokenizer::T_PARENT:
$this->checkIfTokenIsAllowedInParent($parent, $token);
$nodes[] = $this->buildTree($tokens, $token); $nodes[] = $this->buildTree($tokens, $token);
break; break;
case Mustache_Tokenizer::T_BLOCK_VAR: case Mustache_Tokenizer::T_BLOCK_VAR:
if ($parent[Mustache_Tokenizer::TYPE] == Mustache_Tokenizer::T_PARENT) { if ($parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) {
$token[Mustache_Tokenizer::TYPE] = Mustache_Tokenizer::T_BLOCK_ARG; $token[Mustache_Tokenizer::TYPE] = Mustache_Tokenizer::T_BLOCK_ARG;
} }
$this->clearStandaloneLines($nodes, $tokens); $this->clearStandaloneLines($nodes, $tokens);
@@ -215,4 +219,11 @@ class Mustache_Parser
return false; return false;
} }
private function checkIfTokenIsAllowedInParent($parent, $token)
{
if ($parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) {
throw new Mustache_Exception_SyntaxException('Illegal content in < parent tag', $token);
}
}
} }
@@ -322,7 +322,7 @@ class Mustache_Test_Functional_InheritanceTest extends PHPUnit_Framework_TestCas
$this->assertEquals('hmm', $tpl->render($data)); $this->assertEquals('hmm', $tpl->render($data));
} }
public function IgnoreTextInsideSuperTemplates() public function testIgnoreTextInsideSuperTemplates()
{ {
$partials = array( $partials = array(
'include' => '{{$foo}}default content{{/foo}}' 'include' => '{{$foo}}default content{{/foo}}'
@@ -338,4 +338,27 @@ class Mustache_Test_Functional_InheritanceTest extends PHPUnit_Framework_TestCas
$this->assertEquals('default content', $tpl->render($data)); $this->assertEquals('default content', $tpl->render($data));
} }
/**
* @expectedException Mustache_Exception_SyntaxException
* @expectedExceptionMessage Illegal content in < parent tag
*/
public function testOnlyBlockTagsAllowedInParent()
{
$partials = array(
'foo' => '{{$baz}}default content{{/baz}}'
);
$this->mustache->setPartials($partials);
$tpl = $this->mustache->loadTemplate(
'{{< foo }}{{# bar }}{{$ baz }}{{/ baz }}{{/ bar }}{{/ foo }}'
);
$data = array(
'bar' => 'set by user'
);
$tpl->render($data);
}
} }