Merge branch 'feature/test-coverage' of github.com:bobthecow/mustache.php into feature/test-coverage
Conflicts: Mustache.php
This commit is contained in:
+27
-4
@@ -27,6 +27,18 @@ class Mustache {
|
|||||||
|
|
||||||
const PRAGMA_DOT_NOTATION = 'DOT-NOTATION';
|
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 $_tagRegEx;
|
||||||
|
|
||||||
protected $_template = '';
|
protected $_template = '';
|
||||||
@@ -35,7 +47,8 @@ class Mustache {
|
|||||||
protected $_pragmas = array();
|
protected $_pragmas = array();
|
||||||
|
|
||||||
protected $_pragmasImplemented = array(
|
protected $_pragmasImplemented = array(
|
||||||
self::PRAGMA_DOT_NOTATION
|
self::PRAGMA_DOT_NOTATION,
|
||||||
|
self::PRAGMA_UNESCAPED
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -147,11 +160,13 @@ class Mustache {
|
|||||||
case '#':
|
case '#':
|
||||||
if ($this->varIsIterable($val)) {
|
if ($this->varIsIterable($val)) {
|
||||||
foreach ($val as $local_context) {
|
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) {
|
} else if ($val) {
|
||||||
if (is_array($val) || is_object($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 {
|
} else {
|
||||||
$replace .= $content;
|
$replace .= $content;
|
||||||
}
|
}
|
||||||
@@ -181,7 +196,7 @@ class Mustache {
|
|||||||
|
|
||||||
$otag = $this->prepareRegEx($this->_otag);
|
$otag = $this->prepareRegEx($this->_otag);
|
||||||
$ctag = $this->prepareRegEx($this->_ctag);
|
$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);
|
return preg_replace_callback($regex, array($this, 'renderPragma'), $template);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,11 +340,19 @@ class Mustache {
|
|||||||
break;
|
break;
|
||||||
case '{':
|
case '{':
|
||||||
case '&':
|
case '&':
|
||||||
|
if ($this->hasPragma(self::PRAGMA_UNESCAPED)) {
|
||||||
|
return $this->renderEscaped($tag_name, $context);
|
||||||
|
} else {
|
||||||
return $this->renderUnescaped($tag_name, $context);
|
return $this->renderUnescaped($tag_name, $context);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case '':
|
case '':
|
||||||
default:
|
default:
|
||||||
|
if ($this->hasPragma(self::PRAGMA_UNESCAPED)) {
|
||||||
|
return $this->renderUnescaped($tag_name, $context);
|
||||||
|
} else {
|
||||||
return $this->renderEscaped($tag_name, $context);
|
return $this->renderEscaped($tag_name, $context);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class PragmaUnescaped extends Mustache {
|
||||||
|
protected $vs = 'Bear > Shark';
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
{{%UNESCAPED}}
|
||||||
|
{{vs}}
|
||||||
|
{{{vs}}}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
Bear > Shark
|
||||||
|
Bear > Shark
|
||||||
@@ -25,6 +25,9 @@ class MustachePragmaTest extends PHPUnit_Framework_TestCase {
|
|||||||
|
|
||||||
public function testPragmaReplaceMultiple() {
|
public function testPragmaReplaceMultiple() {
|
||||||
$m = new Mustache();
|
$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}}\n{{%DOT-NOTATION}}"), 'Multiple pragma tags not removed');
|
||||||
$this->assertEquals(' ', $m->render('{{%DOT-NOTATION}} {{%DOT-NOTATION}}'), 'Multiple pragma tags not removed');
|
$this->assertEquals(' ', $m->render('{{%DOT-NOTATION}} {{%DOT-NOTATION}}'), 'Multiple pragma tags not removed');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once '../Mustache.php';
|
||||||
|
require_once 'PHPUnit/Framework.php';
|
||||||
|
|
||||||
|
class MustachePragmaUnescapedTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
|
public function testPragmaUnescaped() {
|
||||||
|
$m = new Mustache(null, array('title' => 'Bear > Shark'));
|
||||||
|
|
||||||
|
$this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{title}}'));
|
||||||
|
$this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{{title}}}'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user