added a better error message when a template is empty but contain a BOM

This commit is contained in:
Fabien Potencier
2011-10-31 18:46:05 +01:00
parent f5adcb6970
commit 083d839000
3 changed files with 16 additions and 1 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.4.0
* added a better error message when a template is empty but contain a BOM
* fixed in operator for empty strings
* fixed the "defined" test and the "default" filter (now works with more than one call (foo.bar.foo) and for both values of the strict_variables option)
* changed the way extensions are loaded (addFilter/addFunction/addGlobal/addTest/addNodeVisitor/addTokenParser/addExtension can now be called in any order)
+5 -1
View File
@@ -316,7 +316,11 @@ class Twig_Parser implements Twig_ParserInterface
||
(!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
) {
throw new Twig_Error_Syntax('A template that extends another one cannot have a body.', $node->getLine(), $this->stream->getFilename());
if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
throw new Twig_Error_Syntax('A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed.', $node->getLine(), $this->stream->getFilename());
} else {
throw new Twig_Error_Syntax('A template that extends another one cannot have a body.', $node->getLine(), $this->stream->getFilename());
}
}
// bypass "set" nodes as they "capture" the output
+10
View File
@@ -66,6 +66,16 @@ class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase
);
}
/**
* @expectedException Twig_Error_Syntax
* @expectedExceptionMessage A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed at line 0.
*/
public function testFilterBodyNodesWithBOM()
{
$parser = $this->getParserForFilterBodyNodes();
$parser->filterBodyNodes(new Twig_Node_Text(chr(0xEF).chr(0xBB).chr(0xBF), 0));
}
protected function getParserForFilterBodyNodes()
{
$parser = new TestParser(new Twig_Environment());