Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,11 @@ protected static function detectQuote($string)
*/
protected static function analyze($token, &$context)
{
$count = preg_match_all('/(\s*)([^\s]+)/', $token, $matchedall);
$count = $context['flags']['advar'] ?
// Do not break quoted strings. Also, allow escaped quotes inside them.
preg_match_all('/(\s*)([^"\s]*"(\\\\\\\\.|[^"])*"|[^\'\s]*\'(\\\\\\\\.|[^\'])*\'|\S+)/', $token, $matchedall) :
preg_match_all('/(\s*)([^\s]+)/', $token, $matchedall);

// Parse arguments and deal with "..." or [...] or (...) or \'...\' or |...|
if (($count > 0) && $context['flags']['advar']) {
$vars = array();
Expand All @@ -488,16 +492,18 @@ protected static function analyze($token, &$context)
$quote = 0;
}
}
// if we are inside quotes, we should later skip stack changes
$quotes = preg_match("/^\".*\"$|^\'.*\'$/", $t);

// continue from previous match when expect something
if ($expect) {
$prev .= "{$matchedall[1][$index]}$t";
if (($quote === 0) && ($stack > 0) && preg_match('/(.+=)*(\\(+)/', $t, $m)) {
if (($quote === 0) && ($stack > 0) && preg_match('/(.+=)*(\\(+)/', $t, $m) && !$quotes) {
$stack += strlen($m[2]);
}
// end an argument when end with expected charactor
if (substr($t, -1, 1) === $expect) {
if ($stack > 0) {
if ($stack > 0 && !$quotes) {
preg_match('/(\\)+)$/', $t, $matchedq);
$stack -= isset($matchedq[0]) ? strlen($matchedq[0]) : 1;
if ($stack > 0) {
Expand Down
42 changes: 42 additions & 0 deletions tests/regressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,20 @@ public function issueProvider()
'expected' => 'ab'
),

array(
'id' => 297,
'template' => '{{test "foo" prop="\" "}}',
'options' => array(
'flags' => LightnCandy::FLAG_HANDLEBARS,
'helpers' => array(
'test' => function($arg1, $options) {
return $arg1 . " " . $options['hash']['prop'];
}
)
),
'expected' => 'foo " '
),

array(
'id' => 302,
'template' => "{{#*inline \"t1\"}}{{#if imageUrl}}<span />{{else}}<div />{{/if}}{{/inline}}{{#*inline \"t2\"}}{{#if imageUrl}}<span />{{else}}<div />{{/if}}{{/inline}}{{#*inline \"t3\"}}{{#if imageUrl}}<span />{{else}}<div />{{/if}}{{/inline}}",
Expand Down Expand Up @@ -1593,6 +1607,34 @@ public function issueProvider()
'expected' => '#a(0)=b-321-123#c(1)=d-321-123#e(2)=f-321-123'
),

array(
'id' => 357,
'template' => '{{echo (echo "foobar(moo).")}}',
'options' => array(
'flags' => LightnCandy::FLAG_HANDLEBARS,
'helpers' => array(
'echo' => function($arg1) {
return "ECHO: $arg1";
}
)
),
'expected' => 'ECHO: ECHO: foobar(moo).'
),

array(
'id' => 357,
'template' => '{{echo (echo "foobar(moo)." (echo "moobar(foo)"))}}',
'options' => array(
'flags' => LightnCandy::FLAG_HANDLEBARS,
'helpers' => array(
'echo' => function($arg1) {
return "ECHO: $arg1";
}
)
),
'expected' => 'ECHO: ECHO: foobar(moo).'
),

array(
'template' => '{{#each . as |v k|}}#{{k}}{{/each}}',
'data' => array('a' => array(), 'c' => array()),
Expand Down