fixed embedded templates starting with a BOM

This commit is contained in:
Fabien Potencier
2019-01-13 15:10:13 +01:00
parent 547fc6120e
commit a573d464af
3 changed files with 19 additions and 6 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.37.0 (2019-XX-XX)
* fixed embedded templates starting with a BOM
* fixed using a Twig_TemplateWrapper instance as an argument to extends
* switched generated code to use the PHP short array notation
* dropped PHP 5.3 support
+5 -1
View File
@@ -382,7 +382,11 @@ class Twig_Parser implements Twig_ParserInterface
(!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
) {
if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
throw new Twig_Error_Syntax('A template that extends another one cannot start with a byte order mark (BOM); it must be removed.', $node->getTemplateLine(), $this->stream->getSourceContext());
$t = substr($node->getAttribute('data'), 3);
if ('' === $t || ctype_space($t)) {
// bypass empty nodes starting with a BOM
return;
}
}
throw new Twig_Error_Syntax('A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?', $node->getTemplateLine(), $this->stream->getSourceContext());
+13 -5
View File
@@ -99,13 +99,21 @@ class Twig_Tests_ParserTest extends \PHPUnit\Framework\TestCase
}
/**
* @expectedException Twig_Error_Syntax
* @expectedExceptionMessage A template that extends another one cannot start with a byte order mark (BOM); it must be removed at line 1
* @dataProvider getFilterBodyNodesWithBOMData
*/
public function testFilterBodyNodesWithBOM()
public function testFilterBodyNodesWithBOM($emptyNode)
{
$parser = $this->getParser();
$parser->filterBodyNodes(new Twig_Node_Text(chr(0xEF).chr(0xBB).chr(0xBF), 1));
$this->assertSame(null, $this->getParser()->filterBodyNodes(new Twig_Node_Text(chr(0xEF).chr(0xBB).chr(0xBF).$emptyNode, 1)));
}
public function getFilterBodyNodesWithBOMData()
{
return [
[' '],
["\t"],
["\n"],
["\n\t\n "],
];
}
public function testParseIsReentrant()