Merge branch 'dev' into feature/higher-order-sections
Conflicts: Mustache.php test/MustacheSpecTest.php
This commit is contained in:
+92
-30
@@ -467,9 +467,10 @@ class Mustache {
|
|||||||
* @param string $ctag
|
* @param string $ctag
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _prepareTagRegEx($otag, $ctag) {
|
protected function _prepareTagRegEx($otag, $ctag, $first = false) {
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'/(?P<whitespace>(?<=\\n)[ \\t]*)?%s(?P<type>[%s]?)(?P<tag_name>.+?)(?:\\2|})?%s(?:\\s*(?=\\n))?/s',
|
'/(?P<leading>(?:%s\\r?\\n)[ \\t]*)?%s(?P<type>[%s]?)(?P<tag_name>.+?)(?:\\2|})?%s(?P<trailing>\\s*(?:\\r?\\n|\\Z))?/s',
|
||||||
|
($first ? '\\A|' : ''),
|
||||||
preg_quote($otag, '/'),
|
preg_quote($otag, '/'),
|
||||||
self::TAG_TYPES,
|
self::TAG_TYPES,
|
||||||
preg_quote($ctag, '/')
|
preg_quote($ctag, '/')
|
||||||
@@ -491,7 +492,8 @@ class Mustache {
|
|||||||
$otag_orig = $this->_otag;
|
$otag_orig = $this->_otag;
|
||||||
$ctag_orig = $this->_ctag;
|
$ctag_orig = $this->_ctag;
|
||||||
|
|
||||||
$this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag);
|
$first = true;
|
||||||
|
$this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag, true);
|
||||||
|
|
||||||
$html = '';
|
$html = '';
|
||||||
$matches = array();
|
$matches = array();
|
||||||
@@ -501,10 +503,16 @@ class Mustache {
|
|||||||
$modifier = $matches['type'][0];
|
$modifier = $matches['type'][0];
|
||||||
$tag_name = trim($matches['tag_name'][0]);
|
$tag_name = trim($matches['tag_name'][0]);
|
||||||
|
|
||||||
if (isset($matches['whitespace']) && $matches['whitespace'][1] > -1) {
|
if (isset($matches['leading']) && $matches['leading'][1] > -1) {
|
||||||
$whitespace = $matches['whitespace'][0];
|
$leading = $matches['leading'][0];
|
||||||
} else {
|
} else {
|
||||||
$whitespace = null;
|
$leading = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($matches['trailing']) && $matches['trailing'][1] > -1) {
|
||||||
|
$trailing = $matches['trailing'][0];
|
||||||
|
} else {
|
||||||
|
$trailing = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$html .= substr($template, 0, $offset);
|
$html .= substr($template, 0, $offset);
|
||||||
@@ -515,7 +523,12 @@ class Mustache {
|
|||||||
}
|
}
|
||||||
$template = substr($template, $next_offset);
|
$template = substr($template, $next_offset);
|
||||||
|
|
||||||
$html .= $this->_renderTag($modifier, $tag_name, $whitespace);
|
$html .= $this->_renderTag($modifier, $tag_name, $leading, $trailing);
|
||||||
|
|
||||||
|
if ($first == true) {
|
||||||
|
$first = false;
|
||||||
|
$this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_otag = $otag_orig;
|
$this->_otag = $otag_orig;
|
||||||
@@ -533,20 +546,22 @@ class Mustache {
|
|||||||
* @access protected
|
* @access protected
|
||||||
* @param string $modifier
|
* @param string $modifier
|
||||||
* @param string $tag_name
|
* @param string $tag_name
|
||||||
|
* @param string $leading Whitespace
|
||||||
|
* @param string $trailing Whitespace
|
||||||
* @throws MustacheException Unmatched section tag encountered.
|
* @throws MustacheException Unmatched section tag encountered.
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _renderTag($modifier, $tag_name, $whitespace) {
|
protected function _renderTag($modifier, $tag_name, $leading, $trailing) {
|
||||||
switch ($modifier) {
|
switch ($modifier) {
|
||||||
case '=':
|
case '=':
|
||||||
return $this->_changeDelimiter($tag_name);
|
return $this->_changeDelimiter($tag_name, $leading, $trailing);
|
||||||
break;
|
break;
|
||||||
case '!':
|
case '!':
|
||||||
return $this->_renderComment($tag_name);
|
return $this->_renderComment($tag_name, $leading, $trailing);
|
||||||
break;
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
case '<':
|
case '<':
|
||||||
return $this->_renderPartial($tag_name, $whitespace);
|
return $this->_renderPartial($tag_name, $leading, $trailing);
|
||||||
break;
|
break;
|
||||||
case '{':
|
case '{':
|
||||||
// strip the trailing } ...
|
// strip the trailing } ...
|
||||||
@@ -555,24 +570,41 @@ class Mustache {
|
|||||||
}
|
}
|
||||||
case '&':
|
case '&':
|
||||||
if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
|
if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
|
||||||
return $this->_renderEscaped($tag_name);
|
return $this->_renderEscaped($tag_name, $leading, $trailing);
|
||||||
} else {
|
} else {
|
||||||
return $this->_renderUnescaped($tag_name);
|
return $this->_renderUnescaped($tag_name, $leading, $trailing);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '#':
|
case '#':
|
||||||
case '^':
|
case '^':
|
||||||
case '/':
|
case '/':
|
||||||
// remove any leftovers from _renderSections
|
// remove any leftovers from _renderSections
|
||||||
return '';
|
return $leading . $trailing;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
|
||||||
|
return $this->_renderUnescaped($modifier . $tag_name, $leading, $trailing);
|
||||||
|
} else {
|
||||||
|
return $this->_renderEscaped($modifier . $tag_name, $leading, $trailing);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) {
|
|
||||||
return $this->_renderUnescaped($modifier . $tag_name);
|
|
||||||
} else {
|
|
||||||
return $this->_renderEscaped($modifier . $tag_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if any of its args contains the "\r" character.
|
||||||
|
*
|
||||||
|
* @access protected
|
||||||
|
* @param string $str
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
protected function _stringHasR($str) {
|
||||||
|
foreach (func_get_args() as $arg) {
|
||||||
|
if (strpos($arg, "\r") !== false) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -580,10 +612,12 @@ class Mustache {
|
|||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $tag_name
|
* @param string $tag_name
|
||||||
|
* @param string $leading Whitespace
|
||||||
|
* @param string $trailing Whitespace
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _renderEscaped($tag_name) {
|
protected function _renderEscaped($tag_name, $leading, $trailing) {
|
||||||
return htmlentities($this->_renderUnescaped($tag_name), ENT_COMPAT, $this->_charset);
|
return $leading . htmlentities($this->_renderUnescaped($tag_name), ENT_COMPAT, $this->_charset) . $trailing;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -591,28 +625,38 @@ class Mustache {
|
|||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $tag_name
|
* @param string $tag_name
|
||||||
|
* @param string $leading Whitespace
|
||||||
|
* @param string $trailing Whitespace
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _renderComment($tag_name) {
|
protected function _renderComment($tag_name, $leading, $trailing) {
|
||||||
|
if ($leading !== null && $trailing !== null) {
|
||||||
|
if (strpos($leading, "\n") === false) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
return $this->_stringHasR($leading, $trailing) ? "\r\n" : "\n";
|
||||||
|
}
|
||||||
|
return $leading . $trailing;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the requested tag unescaped.
|
* Return the requested tag unescaped.
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $tag_name
|
* @param string $tag_name
|
||||||
|
* @param string $leading Whitespace
|
||||||
|
* @param string $trailing Whitespace
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _renderUnescaped($tag_name) {
|
protected function _renderUnescaped($tag_name, $leading, $trailing) {
|
||||||
$val = $this->_getVariable($tag_name);
|
$val = $this->_getVariable($tag_name);
|
||||||
|
|
||||||
if ($this->_varIsCallable($val)) {
|
if ($this->_varIsCallable($val)) {
|
||||||
$key = is_object($val) ? spl_object_hash($val) : serialize($val);
|
$key = is_object($val) ? spl_object_hash($val) : serialize($val);
|
||||||
return $this->_renderTemplate(call_user_func($val));
|
$val = $this->_renderTemplate(call_user_func($val));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $val;
|
return $leading . $val . $trailing;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -620,14 +664,24 @@ class Mustache {
|
|||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $tag_name
|
* @param string $tag_name
|
||||||
|
* @param string $leading Whitespace
|
||||||
|
* @param string $trailing Whitespace
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _renderPartial($tag_name, $whitespace = '') {
|
protected function _renderPartial($tag_name, $leading, $trailing) {
|
||||||
|
$partial = $this->_getPartial($tag_name);
|
||||||
|
if ($leading !== null && $trailing !== null) {
|
||||||
|
$whitespace = trim($leading, "\r\n");
|
||||||
|
$partial = preg_replace('/(\\r?\\n)(?!$)/s', "\\1" . $whitespace, $partial);
|
||||||
|
}
|
||||||
|
|
||||||
$view = clone($this);
|
$view = clone($this);
|
||||||
|
|
||||||
$partial = $whitespace . preg_replace('/\n(?!$)/s', "\n" . $whitespace, $this->_getPartial($tag_name));
|
if ($leading !== null && $trailing !== null) {
|
||||||
|
return $leading . $view->render($partial);
|
||||||
return $view->render($partial);
|
} else {
|
||||||
|
return $leading . $view->render($partial) . $trailing;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -636,17 +690,25 @@ class Mustache {
|
|||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $tag_name
|
* @param string $tag_name
|
||||||
|
* @param string $leading Whitespace
|
||||||
|
* @param string $trailing Whitespace
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _changeDelimiter($tag_name) {
|
protected function _changeDelimiter($tag_name, $leading, $trailing) {
|
||||||
list($otag, $ctag) = explode(' ', $tag_name);
|
list($otag, $ctag) = explode(' ', $tag_name);
|
||||||
$this->_otag = $otag;
|
$this->_otag = $otag;
|
||||||
$this->_ctag = $ctag;
|
$this->_ctag = $ctag;
|
||||||
|
|
||||||
$this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag);
|
$this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag);
|
||||||
|
|
||||||
|
if ($leading !== null && $trailing !== null) {
|
||||||
|
if (strpos($leading, "\n") === false) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
return $this->_stringHasR($leading, $trailing) ? "\r\n" : "\n";
|
||||||
|
}
|
||||||
|
return $leading . $trailing;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Push a local context onto the stack.
|
* Push a local context onto the stack.
|
||||||
|
|||||||
@@ -81,8 +81,11 @@ And render it:
|
|||||||
Known Issues
|
Known Issues
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
* As of v1.1.2, there are a couple of whitespace bugs around section tags.
|
||||||
* Things get weird when you change delimiters inside a section -- `delimiters` example currently fails with an
|
* Things get weird when you change delimiters inside a section -- `delimiters` example currently fails with an
|
||||||
"unclosed section" exception.
|
"unclosed section" exception.
|
||||||
|
* The current spec test exposes several whitespace bugs (which are mostly instances of the exact same whitespace
|
||||||
|
bug) ... Despite these failing tests, this version is actually *closer* to correct than previous releases.
|
||||||
|
|
||||||
|
|
||||||
See Also
|
See Also
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
|||||||
* @group comments
|
* @group comments
|
||||||
* @dataProvider loadCommentSpec
|
* @dataProvider loadCommentSpec
|
||||||
*/
|
*/
|
||||||
public function testCommentSpec($template, $data, $partials, $expected, $desc) {
|
public function testCommentSpec($desc, $template, $data, $partials, $expected) {
|
||||||
$m = new Mustache($template, $data, $partials);
|
$m = new Mustache($template, $data, $partials);
|
||||||
$this->assertEquals($expected, $m->render(), $desc);
|
$this->assertEquals($expected, $m->render(), $desc);
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
|||||||
* @group delimiters
|
* @group delimiters
|
||||||
* @dataProvider loadDelimitersSpec
|
* @dataProvider loadDelimitersSpec
|
||||||
*/
|
*/
|
||||||
public function testDelimitersSpec($template, $data, $partials, $expected, $desc) {
|
public function testDelimitersSpec($desc, $template, $data, $partials, $expected) {
|
||||||
$m = new Mustache($template, $data, $partials);
|
$m = new Mustache($template, $data, $partials);
|
||||||
$this->assertEquals($expected, $m->render(), $desc);
|
$this->assertEquals($expected, $m->render(), $desc);
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
|||||||
* @group interpolation
|
* @group interpolation
|
||||||
* @dataProvider loadInterpolationSpec
|
* @dataProvider loadInterpolationSpec
|
||||||
*/
|
*/
|
||||||
public function testInterpolationSpec($template, $data, $partials, $expected, $desc) {
|
public function testInterpolationSpec($desc, $template, $data, $partials, $expected) {
|
||||||
$m = new Mustache($template, $data, $partials);
|
$m = new Mustache($template, $data, $partials);
|
||||||
$this->assertEquals($expected, $m->render(), $desc);
|
$this->assertEquals($expected, $m->render(), $desc);
|
||||||
}
|
}
|
||||||
@@ -52,7 +52,7 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
|||||||
* @group inverted-sections
|
* @group inverted-sections
|
||||||
* @dataProvider loadInvertedSpec
|
* @dataProvider loadInvertedSpec
|
||||||
*/
|
*/
|
||||||
public function testInvertedSpec($template, $data, $partials, $expected, $desc) {
|
public function testInvertedSpec($desc, $template, $data, $partials, $expected) {
|
||||||
$m = new Mustache($template, $data, $partials);
|
$m = new Mustache($template, $data, $partials);
|
||||||
$this->assertEquals($expected, $m->render(), $desc);
|
$this->assertEquals($expected, $m->render(), $desc);
|
||||||
}
|
}
|
||||||
@@ -61,7 +61,7 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
|||||||
* @group lambdas
|
* @group lambdas
|
||||||
* @dataProvider loadLambdasSpec
|
* @dataProvider loadLambdasSpec
|
||||||
*/
|
*/
|
||||||
public function testLambdasSpec($template, $data, $partials, $expected, $desc) {
|
public function testLambdasSpec($desc, $template, $data, $partials, $expected) {
|
||||||
if (!version_compare(PHP_VERSION, '5.3.0', '>=')) {
|
if (!version_compare(PHP_VERSION, '5.3.0', '>=')) {
|
||||||
$this->markTestSkipped('Unable to test Lambdas spec with PHP < 5.3.');
|
$this->markTestSkipped('Unable to test Lambdas spec with PHP < 5.3.');
|
||||||
}
|
}
|
||||||
@@ -94,7 +94,7 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
|||||||
* @group partials
|
* @group partials
|
||||||
* @dataProvider loadPartialsSpec
|
* @dataProvider loadPartialsSpec
|
||||||
*/
|
*/
|
||||||
public function testPartialsSpec($template, $data, $partials, $expected, $desc) {
|
public function testPartialsSpec($desc, $template, $data, $partials, $expected) {
|
||||||
$m = new Mustache($template, $data, $partials);
|
$m = new Mustache($template, $data, $partials);
|
||||||
$this->assertEquals($expected, $m->render(), $desc);
|
$this->assertEquals($expected, $m->render(), $desc);
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
|||||||
* @group sections
|
* @group sections
|
||||||
* @dataProvider loadSectionsSpec
|
* @dataProvider loadSectionsSpec
|
||||||
*/
|
*/
|
||||||
public function testSectionsSpec($template, $data, $partials, $expected, $desc) {
|
public function testSectionsSpec($desc, $template, $data, $partials, $expected) {
|
||||||
$m = new Mustache($template, $data, $partials);
|
$m = new Mustache($template, $data, $partials);
|
||||||
$this->assertEquals($expected, $m->render(), $desc);
|
$this->assertEquals($expected, $m->render(), $desc);
|
||||||
}
|
}
|
||||||
@@ -162,11 +162,11 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
|||||||
$spec = $yaml->parse($file);
|
$spec = $yaml->parse($file);
|
||||||
foreach ($spec['tests'] as $test) {
|
foreach ($spec['tests'] as $test) {
|
||||||
$data[] = array(
|
$data[] = array(
|
||||||
|
$test['name'] . ': ' . $test['desc'],
|
||||||
$test['template'],
|
$test['template'],
|
||||||
$test['data'],
|
$test['data'],
|
||||||
isset($test['partials']) ? $test['partials'] : array(),
|
isset($test['partials']) ? $test['partials'] : array(),
|
||||||
$test['expected'],
|
$test['expected'],
|
||||||
$test['desc']
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
|
|||||||
+1
-1
Submodule test/spec updated: 3383fa66e8...bf6288ed6b
Reference in New Issue
Block a user