Throw inheritance behind a BLOCKS pragma.
Update tests, known pragmas, and parsing.
This commit is contained in:
@@ -27,10 +27,12 @@ class Mustache_Engine
|
||||
const SPEC_VERSION = '1.1.2';
|
||||
|
||||
const PRAGMA_FILTERS = 'FILTERS';
|
||||
const PRAGMA_BLOCKS = 'BLOCKS';
|
||||
|
||||
// Known pragmas
|
||||
private static $knownPragmas = array(
|
||||
self::PRAGMA_FILTERS => true,
|
||||
self::PRAGMA_BLOCKS => true,
|
||||
);
|
||||
|
||||
// Template cache
|
||||
|
||||
@@ -133,11 +133,20 @@ class Mustache_Parser
|
||||
break;
|
||||
|
||||
case Mustache_Tokenizer::T_BLOCK_VAR:
|
||||
if (isset($this->pragmas[Mustache_Engine::PRAGMA_BLOCKS])) {
|
||||
// BLOCKS pragma is enabled, let's do this!
|
||||
if ($parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) {
|
||||
$token[Mustache_Tokenizer::TYPE] = Mustache_Tokenizer::T_BLOCK_ARG;
|
||||
}
|
||||
$this->clearStandaloneLines($nodes, $tokens);
|
||||
$nodes[] = $this->buildTree($tokens, $token);
|
||||
} else {
|
||||
// pretend this was just a normal "escaped" token...
|
||||
$token[Mustache_Tokenizer::TYPE] = Mustache_Tokenizer::T_ESCAPED;
|
||||
// TODO: figure out how to figure out if there was a space after this dollar:
|
||||
$token[Mustache_Tokenizer::NAME] = '$' . $token[Mustache_Tokenizer::NAME];
|
||||
$nodes[] = $token;
|
||||
}
|
||||
break;
|
||||
|
||||
case Mustache_Tokenizer::T_PRAGMA:
|
||||
|
||||
@@ -11,7 +11,9 @@ class Mustache_Test_Functional_InheritanceTest extends PHPUnit_Framework_TestCas
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->mustache = new Mustache_Engine;
|
||||
$this->mustache = new Mustache_Engine(array(
|
||||
'pragmas' => array(Mustache_Engine::PRAGMA_BLOCKS),
|
||||
));
|
||||
}
|
||||
|
||||
public function getIllegalInheritanceExamples()
|
||||
|
||||
@@ -117,6 +117,54 @@ class Mustache_Test_ParserTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
),
|
||||
|
||||
// This *would* be an invalid inheritance parse tree, but that pragma
|
||||
// isn't enabled so it'll thunk it back into an "escaped" token:
|
||||
array(
|
||||
array(
|
||||
array(
|
||||
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_BLOCK_VAR,
|
||||
Mustache_Tokenizer::NAME => 'foo',
|
||||
Mustache_Tokenizer::OTAG => '{{',
|
||||
Mustache_Tokenizer::CTAG => '}}',
|
||||
Mustache_Tokenizer::LINE => 0,
|
||||
),
|
||||
array(
|
||||
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT,
|
||||
Mustache_Tokenizer::LINE => 0,
|
||||
Mustache_Tokenizer::VALUE => 'bar'
|
||||
),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
|
||||
Mustache_Tokenizer::NAME => '$foo',
|
||||
Mustache_Tokenizer::OTAG => '{{',
|
||||
Mustache_Tokenizer::CTAG => '}}',
|
||||
Mustache_Tokenizer::LINE => 0,
|
||||
),
|
||||
array(
|
||||
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT,
|
||||
Mustache_Tokenizer::LINE => 0,
|
||||
Mustache_Tokenizer::VALUE => 'bar'
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInheritanceTokenSets
|
||||
*/
|
||||
public function testParseWithInheritance($tokens, $expected)
|
||||
{
|
||||
$parser = new Mustache_Parser;
|
||||
$parser->setPragmas(array(Mustache_Engine::PRAGMA_BLOCKS));
|
||||
$this->assertEquals($expected, $parser->parse($tokens));
|
||||
}
|
||||
|
||||
public function getInheritanceTokenSets()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
array(
|
||||
@@ -228,7 +276,7 @@ class Mustache_Test_ParserTest extends PHPUnit_Framework_TestCase
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -310,6 +358,33 @@ class Mustache_Test_ParserTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// This *would* be a valid inheritance parse tree, but that pragma
|
||||
// isn't enabled here so it's going to fail :)
|
||||
array(
|
||||
array(
|
||||
array(
|
||||
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_BLOCK_VAR,
|
||||
Mustache_Tokenizer::NAME => 'foo',
|
||||
Mustache_Tokenizer::OTAG => '{{',
|
||||
Mustache_Tokenizer::CTAG => '}}',
|
||||
Mustache_Tokenizer::LINE => 0,
|
||||
),
|
||||
array(
|
||||
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT,
|
||||
Mustache_Tokenizer::LINE => 0,
|
||||
Mustache_Tokenizer::VALUE => 'bar'
|
||||
),
|
||||
array(
|
||||
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION,
|
||||
Mustache_Tokenizer::NAME => 'foo',
|
||||
Mustache_Tokenizer::OTAG => '{{',
|
||||
Mustache_Tokenizer::CTAG => '}}',
|
||||
Mustache_Tokenizer::LINE => 0,
|
||||
Mustache_Tokenizer::INDEX => 11,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user