Skip to content

Commit

Permalink
Merge branch 'release/2.13.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Nov 23, 2019
2 parents fe8fe72 + 819a3e1 commit e95c5a0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 43 deletions.
14 changes: 12 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ sudo: false
matrix:
include:
- php: 5.2
dist: precise
- php: 5.3
dist: precise
- php: 5.4
dist: trusty
- php: 5.5
dist: trusty
- php: 5.6
- php: 7.0
- php: 7.1
- php: 7.2
- php: 7.3
- php: 7.4snapshot
- php: hhvm
dist: trusty
allow_failures:
- php: hhvm
- php: 7.4snapshot

script:
- '[[ "$TRAVIS_PHP_VERSION" = 5.2* ]] && phpunit || vendor/bin/phpunit --verbose'
- '[[ "$TRAVIS_PHP_VERSION" = 5.2* ]] && phpunit || vendor/bin/phpunit --verbose'

install:
- '[[ "$TRAVIS_PHP_VERSION" = 5.2* ]] || composer install'
- '[[ "$TRAVIS_PHP_VERSION" = 5.2* ]] || composer install'
2 changes: 1 addition & 1 deletion src/Mustache/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class Mustache_Engine
{
const VERSION = '2.12.0';
const VERSION = '2.13.0';
const SPEC_VERSION = '1.1.2';

const PRAGMA_FILTERS = 'FILTERS';
Expand Down
4 changes: 2 additions & 2 deletions src/Mustache/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function buildTree(array &$tokens, array $parent = null)
case Mustache_Tokenizer::T_BLOCK_VAR:
if ($this->pragmaBlocks) {
// BLOCKS pragma is enabled, let's do this!
if ($parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) {
if (isset($parent) && $parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) {
$token[Mustache_Tokenizer::TYPE] = Mustache_Tokenizer::T_BLOCK_ARG;
}
$this->clearStandaloneLines($nodes, $tokens);
Expand Down Expand Up @@ -275,7 +275,7 @@ private function tokenIsWhitespace(array $token)
*/
private function checkIfTokenIsAllowedInParent($parent, array $token)
{
if ($parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) {
if (isset($parent) && $parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) {
throw new Mustache_Exception_SyntaxException('Illegal content in < parent tag', $token);
}
}
Expand Down
91 changes: 53 additions & 38 deletions src/Mustache/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,20 @@ class Mustache_Tokenizer
private $tokens;
private $seenTag;
private $line;

private $otag;
private $ctag;
private $otagChar;
private $otagLen;

private $ctag;
private $ctagChar;
private $ctagLen;

/**
* Scan and tokenize template source.
*
* @throws Mustache_Exception_SyntaxException when mismatched section tags are encountered
* @throws Mustache_Exception_InvalidArgumentException when $delimiters string is invalid
*
* @param string $text Mustache template source to tokenize
* @param string $delimiters Optionally, pass initial opening and closing delimiters (default: null)
Expand Down Expand Up @@ -110,12 +115,13 @@ public function scan($text, $delimiters = null)
for ($i = 0; $i < $len; $i++) {
switch ($this->state) {
case self::IN_TEXT:
if ($this->tagChange($this->otag, $this->otagLen, $text, $i)) {
$char = $text[$i];
// Test whether it's time to change tags.
if ($char === $this->otagChar && substr($text, $i, $this->otagLen) === $this->otag) {
$i--;
$this->flushBuffer();
$this->state = self::IN_TAG_TYPE;
} else {
$char = $text[$i];
$this->buffer .= $char;
if ($char === "\n") {
$this->flushBuffer();
Expand Down Expand Up @@ -151,7 +157,9 @@ public function scan($text, $delimiters = null)
break;

default:
if ($this->tagChange($this->ctag, $this->ctagLen, $text, $i)) {
$char = $text[$i];
// Test whether it's time to change tags.
if ($char === $this->ctagChar && substr($text, $i, $this->ctagLen) === $this->ctag) {
$token = array(
self::TYPE => $this->tagType,
self::NAME => trim($this->buffer),
Expand Down Expand Up @@ -196,7 +204,7 @@ public function scan($text, $delimiters = null)
$this->state = self::IN_TEXT;
$this->tokens[] = $token;
} else {
$this->buffer .= $text[$i];
$this->buffer .= $char;
}
break;
}
Expand All @@ -219,16 +227,20 @@ public function scan($text, $delimiters = null)
*/
private function reset()
{
$this->state = self::IN_TEXT;
$this->tagType = null;
$this->buffer = '';
$this->tokens = array();
$this->seenTag = false;
$this->line = 0;
$this->otag = '{{';
$this->ctag = '}}';
$this->otagLen = 2;
$this->ctagLen = 2;
$this->state = self::IN_TEXT;
$this->tagType = null;
$this->buffer = '';
$this->tokens = array();
$this->seenTag = false;
$this->line = 0;

$this->otag = '{{';
$this->otagChar = '{';
$this->otagLen = 2;

$this->ctag = '}}';
$this->ctagChar = '}';
$this->ctagLen = 2;
}

/**
Expand All @@ -249,6 +261,8 @@ private function flushBuffer()
/**
* Change the current Mustache delimiters. Set new `otag` and `ctag` values.
*
* @throws Mustache_Exception_SyntaxException when delimiter string is invalid
*
* @param string $text Mustache template source
* @param int $index Current tokenizer index
*
Expand All @@ -260,28 +274,44 @@ private function changeDelimiters($text, $index)
$close = '=' . $this->ctag;
$closeIndex = strpos($text, $close, $index);

$this->setDelimiters(trim(substr($text, $startIndex, $closeIndex - $startIndex)));

$this->tokens[] = array(
$token = array(
self::TYPE => self::T_DELIM_CHANGE,
self::LINE => $this->line,
);

try {
$this->setDelimiters(trim(substr($text, $startIndex, $closeIndex - $startIndex)));
} catch (Mustache_Exception_InvalidArgumentException $e) {
throw new Mustache_Exception_SyntaxException($e->getMessage(), $token);
}

$this->tokens[] = $token;

return $closeIndex + strlen($close) - 1;
}

/**
* Set the current Mustache `otag` and `ctag` delimiters.
*
* @throws Mustache_Exception_InvalidArgumentException when delimiter string is invalid
*
* @param string $delimiters
*/
private function setDelimiters($delimiters)
{
list($otag, $ctag) = explode(' ', $delimiters);
$this->otag = $otag;
$this->ctag = $ctag;
$this->otagLen = strlen($otag);
$this->ctagLen = strlen($ctag);
if (!preg_match('/^\s*(\S+)\s+(\S+)\s*$/', $delimiters, $matches)) {
throw new Mustache_Exception_InvalidArgumentException(sprintf('Invalid delimiters: %s', $delimiters));
}

list($_, $otag, $ctag) = $matches;

$this->otag = $otag;
$this->otagChar = $otag[0];
$this->otagLen = strlen($otag);

$this->ctag = $ctag;
$this->ctagChar = $ctag[0];
$this->ctagLen = strlen($ctag);
}

/**
Expand Down Expand Up @@ -309,19 +339,4 @@ private function addPragma($text, $index)

return $end + $this->ctagLen - 1;
}

/**
* Test whether it's time to change tags.
*
* @param string $tag Current tag name
* @param int $tagLen Current tag name length
* @param string $text Mustache template source
* @param int $index Current tokenizer index
*
* @return bool True if this is a closing section tag
*/
private function tagChange($tag, $tagLen, $text, $index)
{
return substr($text, $index, $tagLen) === $tag;
}
}

0 comments on commit e95c5a0

Please sign in to comment.