From 914557014b67f93ec0b6431605921cced837a9cf Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 26 Jul 2011 09:05:10 -0400 Subject: [PATCH] * Get the correct column number in parsing exceptions * Throw an exception when nesting two macros --- testing/tests/views/template_test.php | 36 ++++++++++++++++++++++++++- views/template.php | 22 +++++++++++----- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/testing/tests/views/template_test.php b/testing/tests/views/template_test.php index 0a64e18..62e6846 100644 --- a/testing/tests/views/template_test.php +++ b/testing/tests/views/template_test.php @@ -45,7 +45,7 @@ class TemplateTest extends \PHPUnit_Framework_TestCase public function testRender2Vars() { - $template = Template::NewWithData('Hello, {% $name %}. Today is the {% $date->day %} of July.'); + $template = Template::NewWithData('Hello, {% $name %}. Today is the {% $date->day | int %} of July.'); $date = new \stdClass(); $date->day = 26; $template->name = 'Robert'; @@ -65,4 +65,38 @@ class TemplateTest extends \PHPUnit_Framework_TestCase $template->user->logged_in = FALSE; $this->assertEquals('You are not logged in', $this->_Render($template)); } + + public function testExceptions() + { + try { + $catch = FALSE; + $template = Template::NewWithData('Hello %}'); + } catch (\hoplite\views\TemplateException $e) { + $message = $e->GetMessage(); + // Check that the column number is correct. + $this->assertTrue(strpos($message, '1:6') !== FALSE); + $catch = TRUE; + } + $this->assertTrue($catch); + + try { + $catch = FALSE; + $template = Template::NewWithData("Salve\n{% {%"); + } catch (\hoplite\views\TemplateException $e) { + $message = $e->GetMessage(); + $this->assertTrue(strpos($message, '2:4') !== FALSE); + $catch = TRUE; + } + $this->assertTrue($catch); + + try { + $catch = FALSE; + $template = Template::NewWithData("Salve\n\n{% \$name {!%"); + } catch (\hoplite\views\TemplateException $e) { + $message = $e->GetMessage(); + $this->assertTrue(strpos($message, '3:10') !== FALSE); + $catch = TRUE; + } + $this->assertTrue($catch); + } } diff --git a/views/template.php b/views/template.php index f9cb5e6..ede5723 100644 --- a/views/template.php +++ b/views/template.php @@ -119,7 +119,7 @@ class Template $i = 0; // The current position of the iterator. $looking_for_end = FALSE; // Whehter or not an end tag is expected. $line_number = 1; // The current line number. - $column_number = 0; // The current column number. + $i_last_line = 0; // The value of |i| at the previous new line, used for column numbering. while ($i < $length) { // See how far the current position is from the end of the string. @@ -128,14 +128,16 @@ class Template // When a new line is reached, update the counters. if ($data[$i] == "\n") { ++$line_number; - $column_number = 0; + $i_last_line = $i; } // Check for simple PHP short-tag expansion. if ($delta >= 3 && substr($data, $i, 3) == '{!%') { // If an expansion has already been opened, then it's an error to nest. - if ($looking_for_end) - throw new TemplateException("Unexpected start of expansion at line $line_number:$column_number"); + if ($looking_for_end) { + $column = $i - $i_last_line; + throw new TemplateException("Unexpected start of expansion at line $line_number:$column"); + } $looking_for_end = TRUE; $processed .= '<' . '?php'; @@ -148,8 +150,10 @@ class Template // Check for an end tag. if ($substr == '%}') { // If an end tag was encountered without an open tag, that's an error. - if (!$looking_for_end) - throw new TemplateException("Unexpected end of expansion at line $line_number:$column_number"); + if (!$looking_for_end) { + $column = $i - $i_last_line; + throw new TemplateException("Unexpected end of expansion at line $line_number:$column"); + } // If this is a macro, it's time to process it. if ($in_macro) @@ -163,6 +167,12 @@ class Template } // Check for the beginning of a macro. else if ($substr == '{%') { + // If an expansion has already been opened, then it's an error to nest. + if ($looking_for_end) { + $column = $i - $i_last_line; + throw new TemplateException("Unexpected start of expansion at line $line_number:$column"); + } + $processed .= '<' . '?php echo '; $macro = ''; $in_macro = TRUE; -- 2.43.5