From 77f9a37b9ecb216753c0fbd7df17ad8352226f47 Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 00:12:04 -0700 Subject: [PATCH 1/9] Moving $orig_tags to render --- Mustache.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Mustache.php b/Mustache.php index 19e9cbb..b6b08e1 100644 --- a/Mustache.php +++ b/Mustache.php @@ -175,6 +175,9 @@ class Mustache { public function render($template = null, $view = null, $partials = null) { if ($template === null) $template = $this->_template; if ($partials !== null) $this->_partials = $partials; + + $otag_orig = $this->_otag; + $ctag_orig = $this->_ctag; if ($view) { $this->_context = array($view); @@ -183,7 +186,12 @@ class Mustache { } $template = $this->_renderPragmas($template); - return $this->_renderTemplate($template, $this->_context); + $template = $this->_renderTemplate($template, $this->_context); + + $this->_otag = $otag_orig; + $this->_ctag = $ctag_orig; + + return $template; } /** @@ -475,9 +483,6 @@ class Mustache { return $template; } - $otag_orig = $this->_otag; - $ctag_orig = $this->_ctag; - $first = true; $this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag, true); @@ -517,9 +522,6 @@ class Mustache { } } - $this->_otag = $otag_orig; - $this->_ctag = $ctag_orig; - return $html . $template; } From 5e7f0ea4bded0f3c52c6959632de2c1a9f73e680 Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 00:12:47 -0700 Subject: [PATCH 2/9] Relocating _renderTags($before) since _renderTags now has the ability to modify delimiters --- Mustache.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index b6b08e1..d0c0da6 100644 --- a/Mustache.php +++ b/Mustache.php @@ -219,8 +219,11 @@ class Mustache { * @return string Rendered Mustache template. */ protected function _renderTemplate($template) { + if ($section = $this->_findSection($template)) { list($before, $type, $tag_name, $content, $after) = $section; + + $rendered_before = $this->_renderTags($before); $renderedContent = ''; $val = $this->_getVariable($tag_name); @@ -252,7 +255,7 @@ class Mustache { break; } - return $this->_renderTags($before) . $renderedContent . $this->_renderTemplate($after); + return $rendered_before . $renderedContent . $this->_renderTemplate($after); } return $this->_renderTags($template); From eff36b5540ae0b13b4f99d601b22240881a42395 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 03:26:27 -0400 Subject: [PATCH 3/9] Coding style? Conventions? Consistency? Who needs that?. Mebbe I should get some sleep :) --- Mustache.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Mustache.php b/Mustache.php index d0c0da6..779ff34 100644 --- a/Mustache.php +++ b/Mustache.php @@ -225,13 +225,13 @@ class Mustache { $rendered_before = $this->_renderTags($before); - $renderedContent = ''; + $rendered_content = ''; $val = $this->_getVariable($tag_name); switch($type) { // inverted section case '^': if (empty($val)) { - $renderedContent = $this->_renderTemplate($content); + $rendered_content = $this->_renderTemplate($content); } break; @@ -240,22 +240,22 @@ class Mustache { if ($this->_varIsIterable($val)) { foreach ($val as $local_context) { $this->_pushContext($local_context); - $renderedContent .= $this->_renderTemplate($content); + $rendered_content .= $this->_renderTemplate($content); $this->_popContext(); } } else if ($val) { if (is_array($val) || is_object($val)) { $this->_pushContext($val); - $renderedContent = $this->_renderTemplate($content); + $rendered_content = $this->_renderTemplate($content); $this->_popContext(); } else { - $renderedContent = $this->_renderTemplate($content); + $rendered_content = $this->_renderTemplate($content); } } break; } - return $rendered_before . $renderedContent . $this->_renderTemplate($after); + return $rendered_before . $rendered_content . $this->_renderTemplate($after); } return $this->_renderTags($template); From a7cf3e7309dbbdedf58bcdaca9a9bd5ba7cc6cfd Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 00:31:36 -0700 Subject: [PATCH 4/9] Adding test for delimiters --- test/MustacheTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/MustacheTest.php b/test/MustacheTest.php index 70622b4..aded7bc 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -402,6 +402,15 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>')); } + /** + * @group delimiters + */ + public function testStickyDelimiters() { + $m = new Mustache(null, array('result' => 'FAIL')); + $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]')); + $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{#result}}FAIL{{/result}}{{result}}[[={{ }}=]]')); + } + /** * @group sections * @dataProvider poorlyNestedSections From a2f3662b6b897651429c2d9a50ab7b65a5f5446d Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 03:54:15 -0400 Subject: [PATCH 5/9] update failing test case, test for sticky delims between renders. --- test/MustacheTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/MustacheTest.php b/test/MustacheTest.php index aded7bc..d62d648 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -36,7 +36,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase { const TEST_CLASS = 'Mustache'; protected $knownIssues = array( - 'Delimiters' => "Known issue: sections don't respect delimiter changes", + // Just the whitespace ones... ); /** @@ -408,7 +408,8 @@ class MustacheTest extends PHPUnit_Framework_TestCase { public function testStickyDelimiters() { $m = new Mustache(null, array('result' => 'FAIL')); $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]')); - $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{#result}}FAIL{{/result}}{{result}}[[={{ }}=]]')); + $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]]')); + $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]][[={{ }}=]]')); } /** From 25070141a945c009180d49912eb020adeda9c941 Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 00:55:31 -0700 Subject: [PATCH 6/9] Fixing broken test --- test/MustacheTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/MustacheTest.php b/test/MustacheTest.php index aded7bc..8245e5d 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -408,7 +408,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase { public function testStickyDelimiters() { $m = new Mustache(null, array('result' => 'FAIL')); $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]')); - $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{#result}}FAIL{{/result}}{{result}}[[={{ }}=]]')); + $this->assertEquals('{{#result}}{{/result}}', $m->render('{{=[[ ]]=}}{{#result}}{{/result}}[[={{ }}=]]')); } /** From a16d9ed51c1c234b78cdfc7144171feee47c7a3b Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 04:00:13 -0400 Subject: [PATCH 7/9] add a test that changes delimiters all over the place... inside and outside sections. --- README.markdown | 2 -- test/MustacheTest.php | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index c3f6831..2c13ab3 100644 --- a/README.markdown +++ b/README.markdown @@ -81,8 +81,6 @@ And render it: Known Issues ------------ - * Things get weird when you change delimiters inside a section -- `delimiters` example currently fails with an - "unclosed section" exception. * As of v1.1.2, there are a couple of whitespace bugs around section tags... Despite these failing tests, this version is actually *closer* to correct than previous releases. diff --git a/test/MustacheTest.php b/test/MustacheTest.php index d62d648..7edf232 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -410,6 +410,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]')); $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]]')); $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]][[={{ }}=]]')); + $this->assertEquals('{{ result }}', $m->render('{{#result}}{{=[[ ]]=}}{{ result }}[[/result]][[^result]][[={{ }}=]][[ result ]]{{/result}}')); } /** From 1abf854af3ca3bb6b7943a32ed7d5dc69ddaefa3 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 04:00:49 -0400 Subject: [PATCH 8/9] Make section rendering delimiter-chage-aware. Fixes longstanding section/delimiter bug. --- Mustache.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index 779ff34..ef3af03 100644 --- a/Mustache.php +++ b/Mustache.php @@ -271,7 +271,7 @@ class Mustache { */ protected function _prepareSectionRegEx($otag, $ctag) { return sprintf( - '/(?:(?<=\\n)[ \\t]*)?%s(?P[%s])(?P.+?)%s\\n?/s', + '/(?:(?<=\\n)[ \\t]*)?%s(?:(?P[%s])(?P.+?)|=(?P.*?)=)%s\\n?/s', preg_quote($otag, '/'), self::SECTION_TYPES, preg_quote($ctag, '/') @@ -297,6 +297,12 @@ class Mustache { $section_stack = array(); $matches = array(); while (preg_match($regEx, $template, $matches, PREG_OFFSET_CAPTURE, $search_offset)) { + if (isset($matches['delims'][0])) { + list($otag, $ctag) = explode(' ', $matches['delims'][0]); + $regEx = $this->_prepareSectionRegEx($otag, $ctag); + $search_offset = $matches[0][1] + strlen($matches[0][0]); + continue; + } $match = $matches[0][0]; $offset = $matches[0][1]; From 527bca37f35cc95c7a7c58cb56dce987b45e4209 Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 01:01:33 -0700 Subject: [PATCH 9/9] Fixing testStickyDelimiters --- Mustache.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Mustache.php b/Mustache.php index d0c0da6..3a64e6d 100644 --- a/Mustache.php +++ b/Mustache.php @@ -219,7 +219,6 @@ class Mustache { * @return string Rendered Mustache template. */ protected function _renderTemplate($template) { - if ($section = $this->_findSection($template)) { list($before, $type, $tag_name, $content, $after) = $section; @@ -271,7 +270,7 @@ class Mustache { */ protected function _prepareSectionRegEx($otag, $ctag) { return sprintf( - '/(?:(?<=\\n)[ \\t]*)?%s(?P[%s])(?P.+?)%s\\n?/s', + '/(?:(?<=\\n)[ \\t]*)?%s(?:(?P[%s])(?P.+?)|=(?P.*?)=)%s\\n?/s', preg_quote($otag, '/'), self::SECTION_TYPES, preg_quote($ctag, '/') @@ -297,11 +296,18 @@ class Mustache { $section_stack = array(); $matches = array(); while (preg_match($regEx, $template, $matches, PREG_OFFSET_CAPTURE, $search_offset)) { - + $match = $matches[0][0]; $offset = $matches[0][1]; $type = $matches['type'][0]; $tag_name = trim($matches['tag_name'][0]); + + if (isset($matches['delims'][0])) { + list($otag, $ctag) = explode(' ', $matches['delims'][0]); + $regEx = $this->_prepareSectionRegEx($otag, $ctag); + $search_offset = $offset + strlen($match); + continue; + } $search_offset = $offset + strlen($match);