Skip to content

Commit

Permalink
Merge branch 'release/0.2.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Jun 13, 2010
2 parents c3541fa + b300b31 commit f6c4473
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 32 deletions.
46 changes: 18 additions & 28 deletions Mustache.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ protected function _renderTemplate($template) {
* @return string
*/
protected function _renderSection($template) {
$otag = $this->_prepareRegEx($this->_otag);
$ctag = $this->_prepareRegEx($this->_ctag);
$otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($this->_ctag, '/');
$regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m';
$matches = array();
Expand Down Expand Up @@ -227,8 +227,8 @@ protected function _renderPragmas($template) {
return $template;
}

$otag = $this->_prepareRegEx($this->_otag);
$ctag = $this->_prepareRegEx($this->_ctag);
$otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($this->_ctag, '/');
$regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/';
return preg_replace_callback($regex, array($this, '_renderPragma'), $template);
}
Expand Down Expand Up @@ -324,8 +324,11 @@ protected function _renderTags($template) {
return $template;
}

$otag = $this->_prepareRegEx($this->_otag);
$ctag = $this->_prepareRegEx($this->_ctag);
$otag_orig = $this->_otag;
$ctag_orig = $this->_ctag;

$otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($this->_ctag, '/');

$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";

Expand All @@ -342,6 +345,9 @@ protected function _renderTags($template) {
$template = substr($template, $offset + strlen($tag));
}

$this->_otag = $otag_orig;
$this->_ctag = $ctag_orig;

return $html . $template;
}

Expand Down Expand Up @@ -460,8 +466,8 @@ protected function _changeDelimiter($tag_name) {
$this->_otag = $tags[0];
$this->_ctag = $tags[1];

$otag = $this->_prepareRegEx($this->_otag);
$ctag = $this->_prepareRegEx($this->_ctag);
$otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($this->_ctag, '/');
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
return '';
}
Expand All @@ -470,8 +476,8 @@ protected function _changeDelimiter($tag_name) {
* Push a local context onto the stack.
*
* @access protected
* @param array $local_context
* @return array
* @param array &$local_context
* @return void
*/
protected function _pushContext(&$local_context) {
$new = array();
Expand Down Expand Up @@ -537,11 +543,11 @@ protected function _getVariable($tag_name) {
*
* @access protected
* @param string $tag_name
* @param array &$context
* @param array $context
* @throws MustacheException Unknown variable name.
* @return string
*/
protected function _findVariableInContext($tag_name, &$context) {
protected function _findVariableInContext($tag_name, $context) {
foreach ($context as $view) {
if (is_object($view)) {
if (isset($view->$tag_name)) {
Expand Down Expand Up @@ -593,22 +599,6 @@ protected function _getPartial($tag_name) {
protected function _varIsIterable($var) {
return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var))));
}

/**
* Prepare a string to be used in a regular expression.
*
* @access protected
* @param string $str
* @return string
*/
protected function _prepareRegEx($str) {
$replace = array(
'\\' => '\\\\', '^' => '\^', '.' => '\.', '$' => '\$', '|' => '\|', '(' => '\(',
')' => '\)', '[' => '\[', ']' => '\]', '*' => '\*', '+' => '\+', '?' => '\?',
'{' => '\{', '}' => '\}', ',' => '\,'
);
return strtr($str, $replace);
}
}


Expand Down
24 changes: 24 additions & 0 deletions examples/grand_parent_context/GrandParentContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class GrandParentContext extends Mustache {
public $grand_parent_id = 'grand_parent1';
public $parent_contexts = array();

public function __construct() {
parent::__construct();

$this->parent_contexts[] = array('parent_id' => 'parent1', 'child_contexts' => array(
array('child_id' => 'parent1-child1'),
array('child_id' => 'parent1-child2')
));

$parent2 = new stdClass();
$parent2->parent_id = 'parent2';
$parent2->child_contexts = array(
array('child_id' => 'parent2-child1'),
array('child_id' => 'parent2-child2')
);

$this->parent_contexts[] = $parent2;
}
}
10 changes: 10 additions & 0 deletions examples/grand_parent_context/grand_parent_context.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{grand_parent_id}}
{{#parent_contexts}}
{{grand_parent_id}}
{{parent_id}}
{{#child_contexts}}
{{grand_parent_id}}
{{parent_id}}
{{child_id}}
{{/child_contexts}}
{{/parent_contexts}}
17 changes: 17 additions & 0 deletions examples/grand_parent_context/grand_parent_context.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
grand_parent1
grand_parent1
parent1
grand_parent1
parent1
parent1-child1
grand_parent1
parent1
parent1-child2
grand_parent1
parent2
grand_parent1
parent2
parent2-child1
grand_parent1
parent2
parent2-child2
27 changes: 27 additions & 0 deletions test/MustachePragmaDotNotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,31 @@ public function testDeepTraversal() {
$this->assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three');
}

public function testDotNotationContext() {
$data = array('parent' => array('items' => array(
array('item' => array('index' => 1)),
array('item' => array('index' => 2)),
array('item' => array('index' => 3)),
array('item' => array('index' => 4)),
array('item' => array('index' => 5)),
)));

$m = new Mustache('', $data);
$this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent}}{{#items}}{{item.index}}{{/items}}{{/parent}}'));
}

public function testDotNotationSectionNames() {
$data = array('parent' => array('items' => array(
array('item' => array('index' => 1)),
array('item' => array('index' => 2)),
array('item' => array('index' => 3)),
array('item' => array('index' => 4)),
array('item' => array('index' => 5)),
)));

$m = new Mustache('', $data);
$this->assertEquals('.....', $m->render('{{%DOT-NOTATION}}{{#parent.items}}.{{/parent.items}}'));
$this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{item.index}}{{/parent.items}}'));
$this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{#item}}{{index}}{{/item}}{{/parent.items}}'));
}
}
2 changes: 2 additions & 0 deletions test/MustachePragmaUnescapedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public function testPragmaUnescaped() {
$m = new Mustache(null, array('title' => 'Bear > Shark'));

$this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{title}}'));
$this->assertEquals('Bear &gt; Shark', $m->render('{{title}}'));
$this->assertEquals('Bear &gt; Shark', $m->render('{{%UNESCAPED}}{{{title}}}'));
$this->assertEquals('Bear > Shark', $m->render('{{{title}}}'));
}

}
25 changes: 21 additions & 4 deletions test/MustacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ public function testMultipleInvocationsWithTags() {
*/
public function testResetTemplateForMultipleInvocations() {
$m = new Mustache('Sirve.');
$m->render('No sirve.');
$this->assertEquals('No sirve.', $m->render('No sirve.'));
$this->assertEquals('Sirve.', $m->render());

$m2 = new Mustache();
$m2->render('No sirve.');
$this->assertEquals('No sirve.', $m2->render('No sirve.'));
$this->assertEquals('', $m2->render());
}

Expand All @@ -193,7 +193,7 @@ public function testResetTemplateForMultipleInvocations() {
*/
public function test__clone($class, $template, $output) {
if ($class == 'Delimiters') {
$this->markTestSkipped("Known issue: sections don't respect delimeter changes");
$this->markTestSkipped("Known issue: sections don't respect delimiter changes");
return;
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public function test__clone($class, $template, $output) {
*/
public function testExamples($class, $template, $output) {
if ($class == 'Delimiters') {
$this->markTestSkipped("Known issue: sections don't respect delimeter changes");
$this->markTestSkipped("Known issue: sections don't respect delimiter changes");
return;
}

Expand Down Expand Up @@ -289,4 +289,21 @@ public function getExamples() {
}
return $ret;
}

public function testCrazyDelimiters() {
$m = new Mustache(null, array('result' => 'success'));
$this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
$this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))'));
$this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}'));
$this->assertEquals('success', $m->render('{{=<.. ..>=}}<.. result ..>'));
$this->assertEquals('success', $m->render('{{=^^ ^^}}^^ result ^^'));
$this->assertEquals('success', $m->render('{{=// \\\\}}// result \\\\'));
}

public function testResetDelimiters() {
$m = new Mustache(null, array('result' => 'success'));
$this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
$this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>'));
$this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
}
}
6 changes: 6 additions & 0 deletions test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuite name="Mustache">
<directory>./</directory>
</testsuite>
</phpunit>

1 comment on commit f6c4473

@bobthecow
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset custom delimiters between successive calls to render()
PHPUnit config file and additional tests.

Please sign in to comment.