Skip to content

Commit

Permalink
fix #234
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordius Chen committed Jul 18, 2016
1 parent 5ec035f commit 71abc98
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
19 changes: 14 additions & 5 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected static function getVariableNames(&$context, $vn, $blockParams = null)
* @return array<string> code representing passed expression
*/
public static function compileSubExpression(&$context, $vars) {
$ret = static::customHelper($context, $vars, true, true);
$ret = static::customHelper($context, $vars, true, true, true);

if (($ret === null) && $context['flags']['lambda']) {
$ret = static::compileVariable($context, $vars, true, true);
Expand Down Expand Up @@ -562,11 +562,17 @@ protected static function with(&$context, $vars) {
* @param array<boolean|integer|string|array> $vars parsed arguments list
* @param boolean $raw is this {{{ token or not
* @param boolean $nosep true to compile without seperator
* @param boolean $subExp true when compile for subexpression
*
* @return string|null Return compiled code segment for the token when the token is custom helper
*/
protected static function customHelper(&$context, $vars, $raw, $nosep) {
protected static function customHelper(&$context, $vars, $raw, $nosep, $subExp = false) {
if (!isset($context['helpers'][$vars[0][0]])) {
if ($subExp) {
if ($vars[0][0] == 'lookup') {
return static::compileLookup($context, $vars, $raw, true);
}
}
return;
}

Expand Down Expand Up @@ -620,16 +626,19 @@ protected static function compileLog(&$context, &$vars, $raw) {
* @param array<string,array|string|integer> $context current compile context
* @param array<boolean|integer|string|array> $vars parsed arguments list
* @param boolean $raw is this {{{ token or not
* @param boolean $nosep true to compile without seperator
*
* @return string Return compiled code segment for the token
*/
protected static function compileLookup(&$context, &$vars, $raw) {
protected static function compileLookup(&$context, &$vars, $raw, $nosep = false) {
$v2 = static::getVariableName($context, $vars[2]);
$v = static::getVariableName($context, $vars[1], $v2);
$sep = $nosep ? '' : $context['ops']['seperator'];

if ($context['flags']['hbesc'] || $context['flags']['jsobj'] || $context['flags']['jstrue'] || $context['flags']['debug']) {
return $context['ops']['seperator'] . static::getFuncName($context, $raw ? 'raw' : $context['ops']['enc'], $v[1]) . "\$cx, {$v[0]}){$context['ops']['seperator']}";
return $sep . static::getFuncName($context, $raw ? 'raw' : $context['ops']['enc'], $v[1]) . "\$cx, {$v[0]}){$sep}";
} else {
return $raw ? "{$context['ops']['seperator']}$v[0]{$context['ops']['seperator']}" : "{$context['ops']['seperator']}htmlentities((string){$v[0]}, ENT_QUOTES, 'UTF-8'){$context['ops']['seperator']}";
return $raw ? "{$sep}$v[0]{$sep}" : "{$sep}htmlentities((string){$v[0]}, ENT_QUOTES, 'UTF-8'){$sep}";
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public static function subexpression($expression, &$context) {
$vars = static::analyze(substr($expression, 1, -1), $context);
$avars = static::advancedVariable($vars, $context, $expression);
if (isset($avars[0][0]) && !$context['flags']['exhlp']) {
if (!Validator::helper($context, $avars[0][0])) {
if (!Validator::helper($context, $avars[0][0], true)) {
$context['error'][] = "Can not find custom helper function defination {$avars[0][0]}() !";
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,15 +722,27 @@ public static function lookup(&$context, $vars) {
*
* @param array<string,array|string|integer> $context current compile context
* @param string $name token name
* @param boolean $checkSubexp true when check for subexpression
*
* @return boolean Return true when it is custom helper
*/
public static function helper(&$context, $name) {
public static function helper(&$context, $name, $checkSubexp = false) {
if (static::resolveHelper($context, $name)) {
$context['usedFeature']['helper']++;
return true;
}

if ($checkSubexp) {
switch ($name) {
case 'if':
case 'unless':
case 'with':
case 'each':
case 'lookup':
return $context['flags']['nohbh'] ? false : true;
}
}

return false;
}

Expand Down
10 changes: 5 additions & 5 deletions tests/regressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1135,13 +1135,13 @@ public function issueProvider()
'id' => 234,
'template' => '{{> (lookup foo 2)}}',
'data' => array('foo' => array('a', 'b', 'c')),
'partials' => Array(
'a' => '1st',
'b' => '2nd',
'c' => '3rd'
),
'options' => Array(
'flags' => LightnCandy::FLAG_HANDLEBARS | LightnCandy::FLAG_RUNTIMEPARTIAL,
'partials' => Array(
'a' => '1st',
'b' => '2nd',
'c' => '3rd'
)
),
'expected' => '3rd'
),
Expand Down

0 comments on commit 71abc98

Please sign in to comment.