added support for escaping tags. fixes #53

This commit is contained in:
behrooz shabani (everplays) 2014-03-08 14:47:06 +03:30
parent 9c91ea7d64
commit da443c5fd5
2 changed files with 36 additions and 2 deletions

View File

@ -55,6 +55,7 @@ class Tokenizer
const T_UNESCAPED = '{'; const T_UNESCAPED = '{';
const T_UNESCAPED_2 = '&'; const T_UNESCAPED_2 = '&';
const T_TEXT = '_t'; const T_TEXT = '_t';
const T_ESCAPE = "\\";
// Valid token types // Valid token types
private static $_tagTypes = array( private static $_tagTypes = array(
@ -122,13 +123,22 @@ class Tokenizer
$len = strlen($text); $len = strlen($text);
for ($i = 0; $i < $len; $i++) { for ($i = 0; $i < $len; $i++) {
$this->escaping = $this->tagChange(self::T_ESCAPE, $text, $i);
switch ($this->state) { switch ($this->state) {
case self::IN_TEXT: case self::IN_TEXT:
if ($this->tagChange($this->otag, $text, $i)) { if ($this->tagChange(self::T_UNESCAPED.$this->otag, $text, $i) and $this->escaped) {
$this->buffer .= "{{{";
$i += 2;
continue;
} elseif ($this->tagChange($this->otag, $text, $i) and !$this->escaped) {
$i--; $i--;
$this->flushBuffer(); $this->flushBuffer();
$this->state = self::IN_TAG_TYPE; $this->state = self::IN_TAG_TYPE;
} else { } elseif ($this->escaped and $this->escaping) {
$this->buffer .= "\\\\";
} elseif (!$this->escaping) {
if ($text[$i] == "\n") { if ($text[$i] == "\n") {
$this->filterLine(); $this->filterLine();
} else { } else {
@ -212,6 +222,8 @@ class Tokenizer
} }
break; break;
} }
$this->escaped = ($this->escaping and !$this->escaped);
} }
$this->filterLine(true); $this->filterLine(true);
@ -227,6 +239,8 @@ class Tokenizer
protected function reset() protected function reset()
{ {
$this->state = self::IN_TEXT; $this->state = self::IN_TEXT;
$this->escaped = false;
$this->escaping = false;
$this->tagType = null; $this->tagType = null;
$this->tag = null; $this->tag = null;
$this->buffer = ''; $this->buffer = '';

View File

@ -117,6 +117,26 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
array('data' => array('safe' => '<strong>Test</strong>')), array('data' => array('safe' => '<strong>Test</strong>')),
'<strong>Test</strong>' '<strong>Test</strong>'
), ),
array(
"\{{data}}", // is equal to \\{{data}}
array('data' => 'foo'),
'{{data}}',
),
array(
'\\\\{{data}}',
array('data' => 'foo'),
'\\\\foo'
),
array(
'\\\{{data}}', // is equal to \\\\{{data}} in php
array('data' => 'foo'),
'\\\\foo'
),
array(
'\{{{data}}}',
array('data' => 'foo'),
'{{{data}}}'
),
); );
} }