Merge pull request #212 from fenom-template/develop

Fix #195; Catch Throwable exceptions;
This commit is contained in:
Ivan Shalganov 2016-02-23 13:21:43 +03:00
commit d52aaa57a2
3 changed files with 22 additions and 8 deletions

View File

@ -402,7 +402,7 @@ class Compiler
$tag["last"][] = false; $tag["last"][] = false;
$tokens->next(); $tokens->next();
} else { } else {
$tag["last"][] = $tag->tpl->parseScalar($tokens, false); $tag["last"][] = $tag->tpl->parseScalar($tokens);
} }
if ($tokens->is(',')) { if ($tokens->is(',')) {
$tokens->next(); $tokens->next();

View File

@ -330,6 +330,7 @@ class Template extends Render
private function _appendText($text) private function _appendText($text)
{ {
$this->_line += substr_count($text, "\n"); $this->_line += substr_count($text, "\n");
$strip = $this->_options & Fenom::AUTO_STRIP;
if ($this->_filters) { if ($this->_filters) {
if (strpos($text, "<?") === false) { if (strpos($text, "<?") === false) {
foreach ($this->_filters as $filter) { foreach ($this->_filters as $filter) {
@ -344,13 +345,12 @@ class Template extends Render
} }
} }
} }
$text = implode('<?php echo "<?"; ?>', $fragments); $text = implode('<?php echo "<?"; ?>' . ($strip ? '' : PHP_EOL), $fragments);
} }
} else { } else {
$text = str_replace("<?", '<?php echo "<?"; ?>' . PHP_EOL, $text); $text = str_replace("<?", '<?php echo "<?"; ?>' . ($strip ? '' : PHP_EOL), $text);
} }
if($this->_options & Fenom::AUTO_STRIP) { if($strip) {
$text = preg_replace('/\s+/uS', ' ', str_replace(array("\r", "\n"), " ", $text)); $text = preg_replace('/\s+/uS', ' ', str_replace(array("\r", "\n"), " ", $text));
$text = str_replace("> <", "><", $text); $text = str_replace("> <", "><", $text);
} }
@ -563,6 +563,8 @@ class Template extends Render
throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e); throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e); throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
} catch (\Throwable $e) {
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
} }
} }
@ -718,7 +720,7 @@ class Template extends Render
$exp[] = $this->parseIs($tokens, $item, $var); $exp[] = $this->parseIs($tokens, $item, $var);
} elseif ($operator == "in" || ($operator == "not" && $tokens->isNextToken("in"))) { } elseif ($operator == "in" || ($operator == "not" && $tokens->isNextToken("in"))) {
$item = array_pop($exp); $item = array_pop($exp);
$exp[] = $this->parseIn($tokens, $item, $var); $exp[] = $this->parseIn($tokens, $item);
} else { } else {
break; break;
} }
@ -785,7 +787,7 @@ class Template extends Render
switch($tokens->key()) { switch($tokens->key()) {
case T_LNUMBER: case T_LNUMBER:
case T_DNUMBER: case T_DNUMBER:
$code = $unary . $this->parseScalar($tokens, true); $code = $unary . $this->parseScalar($tokens);
break; break;
case T_CONSTANT_ENCAPSED_STRING: case T_CONSTANT_ENCAPSED_STRING:
case '"': case '"':
@ -793,7 +795,7 @@ class Template extends Render
if ($unary) { if ($unary) {
throw new UnexpectedTokenException($tokens->back()); throw new UnexpectedTokenException($tokens->back());
} }
$code = $this->parseScalar($tokens, true); $code = $this->parseScalar($tokens);
break; break;
case '$': case '$':
$code = $this->parseAccessor($tokens, $is_var); $code = $this->parseAccessor($tokens, $is_var);

View File

@ -349,6 +349,18 @@ class FenomTest extends \Fenom\TestCase
TPL; TPL;
$this->assertRender($tpl, '<div class="item item-one"><a href="/item/1">number one</a></div>'); $this->assertRender($tpl, '<div class="item item-one"><a href="/item/1">number one</a></div>');
} }
/**
* Bug #195
* @group strip-xml
*/
public function testStripXML() {
$this->fenom->setOptions(Fenom::AUTO_STRIP);
$tpl = <<<TPL
<?xml version="1.0"?>
TPL;
$this->assertRender($tpl, '<?xml version="1.0"?>');
}
} }