From 083d8390003f43d89d770fd6ab35cd476cfdb22f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 31 Oct 2011 18:46:05 +0100 Subject: [PATCH] added a better error message when a template is empty but contain a BOM --- CHANGELOG | 1 + lib/Twig/Parser.php | 6 +++++- test/Twig/Tests/ParserTest.php | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d758fa9..cb519ca 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 0fc11cb..72910b5 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -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 diff --git a/test/Twig/Tests/ParserTest.php b/test/Twig/Tests/ParserTest.php index 2b3a058..d0db089 100644 --- a/test/Twig/Tests/ParserTest.php +++ b/test/Twig/Tests/ParserTest.php @@ -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()); -- 1.7.2.5