Merge branch 'release/0.2.4'

This commit is contained in:
Justin Hileman
2010-06-13 01:49:55 -04:00
8 changed files with 125 additions and 32 deletions
+18 -28
View File
@@ -164,8 +164,8 @@ class Mustache {
* @return string * @return string
*/ */
protected function _renderSection($template) { protected function _renderSection($template) {
$otag = $this->_prepareRegEx($this->_otag); $otag = preg_quote($this->_otag, '/');
$ctag = $this->_prepareRegEx($this->_ctag); $ctag = preg_quote($this->_ctag, '/');
$regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m'; $regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m';
$matches = array(); $matches = array();
@@ -227,8 +227,8 @@ class Mustache {
return $template; return $template;
} }
$otag = $this->_prepareRegEx($this->_otag); $otag = preg_quote($this->_otag, '/');
$ctag = $this->_prepareRegEx($this->_ctag); $ctag = preg_quote($this->_ctag, '/');
$regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/'; $regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/';
return preg_replace_callback($regex, array($this, '_renderPragma'), $template); return preg_replace_callback($regex, array($this, '_renderPragma'), $template);
} }
@@ -324,8 +324,11 @@ class Mustache {
return $template; return $template;
} }
$otag = $this->_prepareRegEx($this->_otag); $otag_orig = $this->_otag;
$ctag = $this->_prepareRegEx($this->_ctag); $ctag_orig = $this->_ctag;
$otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($this->_ctag, '/');
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/"; $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
@@ -342,6 +345,9 @@ class Mustache {
$template = substr($template, $offset + strlen($tag)); $template = substr($template, $offset + strlen($tag));
} }
$this->_otag = $otag_orig;
$this->_ctag = $ctag_orig;
return $html . $template; return $html . $template;
} }
@@ -460,8 +466,8 @@ class Mustache {
$this->_otag = $tags[0]; $this->_otag = $tags[0];
$this->_ctag = $tags[1]; $this->_ctag = $tags[1];
$otag = $this->_prepareRegEx($this->_otag); $otag = preg_quote($this->_otag, '/');
$ctag = $this->_prepareRegEx($this->_ctag); $ctag = preg_quote($this->_ctag, '/');
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/"; $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
return ''; return '';
} }
@@ -470,8 +476,8 @@ class Mustache {
* Push a local context onto the stack. * Push a local context onto the stack.
* *
* @access protected * @access protected
* @param array $local_context * @param array &$local_context
* @return array * @return void
*/ */
protected function _pushContext(&$local_context) { protected function _pushContext(&$local_context) {
$new = array(); $new = array();
@@ -537,11 +543,11 @@ class Mustache {
* *
* @access protected * @access protected
* @param string $tag_name * @param string $tag_name
* @param array &$context * @param array $context
* @throws MustacheException Unknown variable name. * @throws MustacheException Unknown variable name.
* @return string * @return string
*/ */
protected function _findVariableInContext($tag_name, &$context) { protected function _findVariableInContext($tag_name, $context) {
foreach ($context as $view) { foreach ($context as $view) {
if (is_object($view)) { if (is_object($view)) {
if (isset($view->$tag_name)) { if (isset($view->$tag_name)) {
@@ -593,22 +599,6 @@ class Mustache {
protected function _varIsIterable($var) { protected function _varIsIterable($var) {
return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($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);
}
} }
@@ -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;
}
}
@@ -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}}
@@ -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
View File
@@ -31,4 +31,31 @@ class MustachePragmaDotNotationTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three'); $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
View File
@@ -9,7 +9,9 @@ class MustachePragmaUnescapedTest extends PHPUnit_Framework_TestCase {
$m = new Mustache(null, array('title' => 'Bear > Shark')); $m = new Mustache(null, array('title' => 'Bear > Shark'));
$this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{title}}')); $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 &gt; Shark', $m->render('{{%UNESCAPED}}{{{title}}}'));
$this->assertEquals('Bear > Shark', $m->render('{{{title}}}'));
} }
} }
+21 -4
View File
@@ -176,11 +176,11 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
*/ */
public function testResetTemplateForMultipleInvocations() { public function testResetTemplateForMultipleInvocations() {
$m = new Mustache('Sirve.'); $m = new Mustache('Sirve.');
$m->render('No sirve.'); $this->assertEquals('No sirve.', $m->render('No sirve.'));
$this->assertEquals('Sirve.', $m->render()); $this->assertEquals('Sirve.', $m->render());
$m2 = new Mustache(); $m2 = new Mustache();
$m2->render('No sirve.'); $this->assertEquals('No sirve.', $m2->render('No sirve.'));
$this->assertEquals('', $m2->render()); $this->assertEquals('', $m2->render());
} }
@@ -193,7 +193,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
*/ */
public function test__clone($class, $template, $output) { public function test__clone($class, $template, $output) {
if ($class == 'Delimiters') { if ($class == 'Delimiters') {
$this->markTestSkipped("Known issue: sections don't respect delimeter changes"); $this->markTestSkipped("Known issue: sections don't respect delimiter changes");
return; return;
} }
@@ -224,7 +224,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
*/ */
public function testExamples($class, $template, $output) { public function testExamples($class, $template, $output) {
if ($class == 'Delimiters') { if ($class == 'Delimiters') {
$this->markTestSkipped("Known issue: sections don't respect delimeter changes"); $this->markTestSkipped("Known issue: sections don't respect delimiter changes");
return; return;
} }
@@ -289,4 +289,21 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
} }
return $ret; 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
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuite name="Mustache">
<directory>./</directory>
</testsuite>
</phpunit>