From 4ea393adb08085f916c2029de41fc382a8bc4bda Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 6 Sep 2011 13:42:02 +0200 Subject: [PATCH] added an exception for unclosed tags --- CHANGELOG | 1 + lib/Twig/Parser.php | 4 ++++ .../Tests/Fixtures/exceptions/unclosed_tag.test | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 0 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/exceptions/unclosed_tag.test diff --git a/CHANGELOG b/CHANGELOG index e2e8468..dbb09db 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.2.0 + * enhanced the exception when a tag remains unclosed * added support for empty Countable objects for the "empty" test * fixed algorithm that determines if a template using inheritance is valid (no output between block definitions) * added better support for encoding problems when escaping a string (available as of PHP 5.4) diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 0a4ae3e..8c98288 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -140,6 +140,10 @@ class Twig_Parser implements Twig_ParserInterface $subparser = $this->handlers->getTokenParser($token->getValue()); if (null === $subparser) { + if (null !== $test) { + throw new Twig_Error_Syntax(sprintf('Unexpected tag name "%s" (expecting closing tag for the "%s" tag defined near line %s)', $token->getValue(), $test[0]->getTag(), $lineno), $token->getLine(), $this->stream->getFilename()); + } + throw new Twig_Error_Syntax(sprintf('Unknown tag name "%s"', $token->getValue()), $token->getLine(), $this->stream->getFilename()); } diff --git a/test/Twig/Tests/Fixtures/exceptions/unclosed_tag.test b/test/Twig/Tests/Fixtures/exceptions/unclosed_tag.test new file mode 100644 index 0000000..02245e9 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/unclosed_tag.test @@ -0,0 +1,20 @@ +--TEST-- +Exception for an unclosed tag +--TEMPLATE-- +{% block foo %} + {% if foo %} + + + + + {% for i in fo %} + + + + {% endfor %} + + + +{% endblock %} +--EXCEPTION-- +Twig_Error_Syntax: Unexpected tag name "endblock" (expecting closing tag for the "if" tag defined near line 4) in "index.twig" at line 16 -- 1.7.2.5