Skip to content

Commit

Permalink
Fix "Call to undefined method" errors for unclosed sections
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 22, 2023
1 parent 1b9fa69 commit e477165
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/main/php/com/handlebarsjs/HandlebarsParser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion src/test/php/com/handlebarsjs/unittest/ParsingTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('
Expand Down

0 comments on commit e477165

Please sign in to comment.