Skip to content

Commit

Permalink
Throw syntax exception for unclosed tags.
Browse files Browse the repository at this point in the history
Fixes #395
  • Loading branch information
bobthecow committed Jul 28, 2022
1 parent c971867 commit 64e5609
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Mustache/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ public function scan($text, $delimiters = '')
}
}

if ($this->state !== self::IN_TEXT) {
$this->throwUnclosedTagException();
}

$this->flushBuffer();

// Restore the user's encoding...
Expand Down Expand Up @@ -279,6 +283,10 @@ private function changeDelimiters($text, $index)
$close = '=' . $this->ctag;
$closeIndex = strpos($text, $close, $index);

if ($closeIndex === false) {
$this->throwUnclosedTagException();
}

$token = array(
self::TYPE => self::T_DELIM_CHANGE,
self::LINE => $this->line,
Expand Down Expand Up @@ -333,6 +341,10 @@ private function setDelimiters($delimiters)
private function addPragma($text, $index)
{
$end = strpos($text, $this->ctag, $index);
if ($end === false) {
$this->throwUnclosedTagException();
}

$pragma = trim(substr($text, $index + 2, $end - $index - 2));

// Pragmas are hoisted to the front of the template.
Expand All @@ -344,4 +356,23 @@ private function addPragma($text, $index)

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

private function throwUnclosedTagException()
{
$name = trim($this->buffer);
if ($name !== '') {
$msg = sprintf('Unclosed tag: %s on line %d', $name, $this->line);
} else {
$msg = sprintf('Unclosed tag on line %d', $this->line);
}

throw new Mustache_Exception_SyntaxException($msg, array(
self::TYPE => $this->tagType,
self::NAME => $name,
self::OTAG => $this->otag,
self::CTAG => $this->ctag,
self::LINE => $this->line,
self::INDEX => $this->seenTag - $this->otagLen,
));
}
}
39 changes: 39 additions & 0 deletions test/Mustache/Test/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,43 @@ public function getTokens()
),
);
}

/**
* @dataProvider getUnclosedTags
* @expectedException Mustache_Exception_SyntaxException
*/
public function testUnclosedTagsThrowExceptions($text)
{
$tokenizer = new Mustache_Tokenizer();
$tokenizer->scan($text, null);
}

public function getUnclosedTags()
{
return array(
array('{{ name'),
array('{{ name }'),
array('{{{ name'),
array('{{{ name }'),
array('{{& name'),
array('{{& name }'),
array('{{# name'),
array('{{# name }'),
array('{{^ name'),
array('{{^ name }'),
array('{{/ name'),
array('{{/ name }'),
array('{{> name'),
array('{{< name'),
array('{{> name }'),
array('{{< name }'),
array('{{$ name'),
array('{{$ name }'),
array('{{= <% %>'),
array('{{= <% %>='),
array('{{= <% %>=}'),
array('{{% name'),
array('{{% name }'),
);
}
}

0 comments on commit 64e5609

Please sign in to comment.