From a834ef7845e8afc3e65165b2e36648c218ee1493 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Fri, 30 Apr 2010 00:09:27 -0400 Subject: [PATCH 1/4] Added unescaped pragma. --- Mustache.php | 27 ++++++++++++++++--- examples/pragma_unescaped/PragmaUnescaped.php | 5 ++++ .../pragma_unescaped.mustache | 3 +++ .../pragma_unescaped/pragma_unescaped.txt | 2 ++ test/MustachePragmaUnescapedTest.php | 15 +++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 examples/pragma_unescaped/PragmaUnescaped.php create mode 100644 examples/pragma_unescaped/pragma_unescaped.mustache create mode 100644 examples/pragma_unescaped/pragma_unescaped.txt create mode 100644 test/MustachePragmaUnescapedTest.php diff --git a/Mustache.php b/Mustache.php index e47e532..8e87331 100644 --- a/Mustache.php +++ b/Mustache.php @@ -27,6 +27,18 @@ class Mustache { const PRAGMA_DOT_NOTATION = 'DOT-NOTATION'; + /** + * The {{%UNESCAPED}} pragma swaps the meaning of the {{normal}} and {{{unescaped}}} + * Mustache tags. That is, once this pragma is activated the {{normal}} tag will not be + * escaped while the {{{unescaped}}} tag will be escaped. + * + * Pragmas apply only to the current template. Partials, even those included after the + * {{%UNESCAPED}} call, will need their own pragma declaration. + * + * his may be useful in non-HTML Mustache situations. + */ + const PRAGMA_UNESCAPED = 'UNESCAPED'; + protected $tagRegEx; protected $template = ''; @@ -35,7 +47,8 @@ class Mustache { protected $pragmas = array(); protected $pragmasImplemented = array( - self::PRAGMA_DOT_NOTATION + self::PRAGMA_DOT_NOTATION, + self::PRAGMA_UNESCAPED ); /** @@ -310,11 +323,19 @@ class Mustache { break; case '{': case '&': - return $this->renderUnescaped($tag_name, $context); + if ($this->hasPragma(self::PRAGMA_UNESCAPED)) { + return $this->renderEscaped($tag_name, $context); + } else { + return $this->renderUnescaped($tag_name, $context); + } break; case '': default: - return $this->renderEscaped($tag_name, $context); + if ($this->hasPragma(self::PRAGMA_UNESCAPED)) { + return $this->renderUnescaped($tag_name, $context); + } else { + return $this->renderEscaped($tag_name, $context); + } break; } } diff --git a/examples/pragma_unescaped/PragmaUnescaped.php b/examples/pragma_unescaped/PragmaUnescaped.php new file mode 100644 index 0000000..59681a8 --- /dev/null +++ b/examples/pragma_unescaped/PragmaUnescaped.php @@ -0,0 +1,5 @@ + Shark'; +} \ No newline at end of file diff --git a/examples/pragma_unescaped/pragma_unescaped.mustache b/examples/pragma_unescaped/pragma_unescaped.mustache new file mode 100644 index 0000000..76095d7 --- /dev/null +++ b/examples/pragma_unescaped/pragma_unescaped.mustache @@ -0,0 +1,3 @@ +{{%UNESCAPED}} +{{vs}} +{{{vs}}} \ No newline at end of file diff --git a/examples/pragma_unescaped/pragma_unescaped.txt b/examples/pragma_unescaped/pragma_unescaped.txt new file mode 100644 index 0000000..2860f61 --- /dev/null +++ b/examples/pragma_unescaped/pragma_unescaped.txt @@ -0,0 +1,2 @@ +Bear > Shark +Bear > Shark \ No newline at end of file diff --git a/test/MustachePragmaUnescapedTest.php b/test/MustachePragmaUnescapedTest.php new file mode 100644 index 0000000..559eeab --- /dev/null +++ b/test/MustachePragmaUnescapedTest.php @@ -0,0 +1,15 @@ + 'Bear > Shark')); + + $this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{title}}')); + $this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{{title}}}')); + } + +} \ No newline at end of file From 37cf2706a96c70a5849288743622d4cb59a2fbde Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Fri, 30 Apr 2010 00:15:17 -0400 Subject: [PATCH 2/4] Allow whitespace in pragma tags. --- Mustache.php | 2 +- test/MustachePragmaTest.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index 8e87331..119d120 100644 --- a/Mustache.php +++ b/Mustache.php @@ -199,7 +199,7 @@ class Mustache { $otag = $this->prepareRegEx($this->otag); $ctag = $this->prepareRegEx($this->ctag); - $regex = '/' . $otag . '%([\\w_-]+)((?: [\\w]+=[\\w]+)*)' . $ctag . '\\n?/'; + $regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/'; return preg_replace_callback($regex, array($this, 'renderPragma'), $template); } diff --git a/test/MustachePragmaTest.php b/test/MustachePragmaTest.php index db21269..534aab3 100644 --- a/test/MustachePragmaTest.php +++ b/test/MustachePragmaTest.php @@ -25,6 +25,8 @@ class MustachePragmaTest extends PHPUnit_Framework_TestCase { public function testPragmaReplaceMultiple() { $m = new Mustache(); + $this->assertEquals('', $m->render('{{% DOT-NOTATION }}'), 'Pragmas should allow whitespace'); + $this->assertEquals('', $m->render('{{% DOT-NOTATION foo=bar }}'), 'Pragmas should allow whitespace'); $this->assertEquals($m->render("{{%DOT-NOTATION}}\n{{%DOT-NOTATION}}"), '', 'Multiple pragma tags not removed'); $this->assertEquals($m->render('{{%DOT-NOTATION}} {{%DOT-NOTATION}}'), ' ', 'Multiple pragma tags not removed'); } From d5de2d4af79f7560ff57bdeeaa7d62e66500582a Mon Sep 17 00:00:00 2001 From: geoffreyd Date: Fri, 30 Apr 2010 22:54:55 +0800 Subject: [PATCH 3/4] Fixed warning 'only variables should be passed by reference' --- Mustache.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index e1f69cd..a46af65 100644 --- a/Mustache.php +++ b/Mustache.php @@ -144,7 +144,8 @@ class Mustache { case '#': if ($this->varIsIterable($val)) { foreach ($val as $local_context) { - $replace .= $this->_render($content, $this->getContext($context, $local_context)); + $c = $this->getContext($context, $local_context); + $replace .= $this->_render($content, $c); } } else if ($val) { if (is_array($val) || is_object($val)) { From 9a30573718678da59d63574941df9b80af3e190d Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Fri, 30 Apr 2010 11:16:43 -0400 Subject: [PATCH 4/4] Fixed additional instance of 'only variables should be passed by reference' warning --- Mustache.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index 52e101b..6d2aeab 100644 --- a/Mustache.php +++ b/Mustache.php @@ -170,7 +170,8 @@ class Mustache { } } else if ($val) { if (is_array($val) || is_object($val)) { - $replace .= $this->_render($content, $this->getContext($context, $val)); + $c = $this->getContext($context, $val); + $replace .= $this->_render($content, $c); } else { $replace .= $content; }