Skip to content

Commit

Permalink
Add support for literals enclosed in []
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 22, 2023
1 parent 89bdb2a commit a0051cc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 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 / ????-??-??

* Added support for literals enclosed in `[]` - @thekid
* Optimized helpers invoked as part of a block - @thekid
* Fixed *Call to undefined method* errors for unclosed sections - @thekid
* Merged PR #29: Implement inverse blocks - @thekid
Expand Down
51 changes: 30 additions & 21 deletions src/main/php/com/handlebarsjs/HandlebarsParser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,41 @@ public function options($tag) {
$p= strcspn($tag, "\r\n\t ", $o);
continue;
} else {
$p= strcspn($tag, ' =', $o);

// [token] vs. token
if ('[' === $tag[$o]) {
$p= strcspn($tag, ']', $o);
$token= substr($tag, $o + 1, $p - 1);
$p++;
} else {
$p= strcspn($tag, $key ? '=' : ' =', $o);
$token= substr($tag, $o, $p);
}

// key=value vs. value
if ($o + $p < $l && '=' === $tag[$o + $p]) {
$key= substr($tag, $o, $p);
$key= $token;
continue;
} else if ('true' === $token) {
$value= new Constant(true);
} else if ('false' === $token) {
$value= new Constant(false);
} else if ('null' === $token) {
$value= new Constant(null);
} else if ('.' === $token) {
$value= new Lookup(null);
} else if ('as' === $token) { // Block parameters (as |...|)
$o= strpos($tag, '|', $o);
$p= strcspn($tag, '|', $o + 1);
$value= new BlockParams(explode(' ', trim(substr($tag, $o + 1, $p))));
$p++;
} else if (strspn($token, '-.0123456789') === strlen($token)) {
$value= new Constant(strstr($token, '.') ? (float)$token : (int)$token);
} else {
$token= substr($tag, $o, $p);
if ('true' === $token) {
$value= new Constant(true);
} else if ('false' === $token) {
$value= new Constant(false);
} else if ('null' === $token) {
$value= new Constant(null);
} else if ('.' === $token) {
$value= new Lookup(null);
} else if ('as' === $token) { // Block parameters (as |...|)
$o= strpos($tag, '|', $o);
$p= strcspn($tag, '|', $o + 1);
$value= new BlockParams(explode(' ', trim(substr($tag, $o + 1, $p))));
$p++;
} else if (strspn($token, '-.0123456789') === strlen($token)) {
$value= new Constant(strstr($token, '.') ? (float)$token : (int)$token);
} else {
$value= new Lookup($token);
}
$value= new Lookup($token);
}
}

if ($key) {
$parsed[$key]= $value;
$key= null;
Expand Down
30 changes: 27 additions & 3 deletions src/test/php/com/handlebarsjs/unittest/ParsingTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public function dynamic_partial_with_lookup_helper() {
);
}

#[Test, Values(['tagName="h1"', '[tagName]="h1"'])]
public function partial_with_context($context) {
#[Test]
public function partial_with_context() {
Assert::equals(
new Nodes([new PartialNode(new Constant('userMessage'), ['tagName' => new Quoted('h1')])]),
$this->parse("{{> userMessage {$context}}}")
$this->parse('{{> userMessage tagName="h1"}}')
);
}

Expand All @@ -103,6 +103,30 @@ public function dynamic_partial_with_context() {
);
}

#[Test]
public function context_option_with_braces() {
Assert::equals(
new Nodes([new PartialNode(new Constant('userMessage'), [new Lookup('tag name')])]),
$this->parse('{{> userMessage [tag name]}}')
);
}

#[Test]
public function context_name_with_braces() {
Assert::equals(
new Nodes([new PartialNode(new Constant('userMessage'), ['tag name' => new Quoted('h1')])]),
$this->parse('{{> userMessage [tag name]="h1"}}')
);
}

#[Test]
public function context_value_with_braces() {
Assert::equals(
new Nodes([new PartialNode(new Constant('userMessage'), ['tagName' => new Lookup('tag value')])]),
$this->parse('{{> userMessage tagName=[tag value]}}')
);
}

#[Test]
public function partial_block() {
Assert::equals(
Expand Down

0 comments on commit a0051cc

Please sign in to comment.