From e477165a0a4bcbcb584876719ae4cb853c2319ee Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 22 Oct 2023 14:12:42 +0200 Subject: [PATCH] Fix "Call to undefined method" errors for unclosed sections --- ChangeLog.md | 1 + .../com/handlebarsjs/HandlebarsParser.class.php | 3 ++- .../handlebarsjs/unittest/ParsingTest.class.php | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 9115b57..86bcd81 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -5,6 +5,7 @@ HandleBars change log ## 9.2.0 / ????-??-?? +* Fixed *Call to undefined method* errors for unclosed sections - @thekid * Merged PR #29: Implement inverse blocks - @thekid * Added PHP 8.4 to the test matrix - @thekid * Optimized parsing `{{.}}` by not parsing following text - @thekid diff --git a/src/main/php/com/handlebarsjs/HandlebarsParser.class.php b/src/main/php/com/handlebarsjs/HandlebarsParser.class.php index 25e9d15..16145b7 100755 --- a/src/main/php/com/handlebarsjs/HandlebarsParser.class.php +++ b/src/main/php/com/handlebarsjs/HandlebarsParser.class.php @@ -326,7 +326,8 @@ public function parse(Tokenizer $tokens, $start= '{{', $end= '}}', $indent= '') // Check for unclosed sections if (!empty($state->parents)) { - throw new TemplateFormatException('Unclosed section '.$state->target->name()); + $block= array_pop($state->parents); + throw new TemplateFormatException('Unclosed section {{#'.$block->name().'}}'); } return $state->target; diff --git a/src/test/php/com/handlebarsjs/unittest/ParsingTest.class.php b/src/test/php/com/handlebarsjs/unittest/ParsingTest.class.php index 0a4aafd..d2bd7af 100755 --- a/src/test/php/com/handlebarsjs/unittest/ParsingTest.class.php +++ b/src/test/php/com/handlebarsjs/unittest/ParsingTest.class.php @@ -152,11 +152,26 @@ public function quoted($literal, $value) { ); } - #[Test, Expect(class: TemplateFormatException::class, message: '/Illegal nesting, no start tag/')] + #[Test, Expect(class: TemplateFormatException::class, message: 'Illegal nesting, expected {{/if}}, have {{/unless}}')] + public function illegal_nesting() { + $this->parse('{{#if test}}X{{/unless}}'); + } + + #[Test, Expect(class: TemplateFormatException::class, message: 'Illegal nesting, no start tag, but have {{/if}}')] public function no_start_tag() { $this->parse('{{if test}}X{{/if}}'); } + #[Test, Expect(class: TemplateFormatException::class, message: 'Unclosed section {{#section}}')] + public function unclosed_section() { + $this->parse('{{#section}}X'); + } + + #[Test, Expect(class: TemplateFormatException::class, message: 'Unclosed section {{#if}}')] + public function unclosed_if() { + $this->parse('{{#if test}}X'); + } + #[Test] public function multiline_tag() { $p= $this->parse(trim('