Skip to content

Commit

Permalink
add filter support everywhere (e.g. in if tag)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberklin authored and speedmax committed Aug 6, 2010
1 parent 6c0697e commit 79befdd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
64 changes: 45 additions & 19 deletions h2o/context.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,34 +88,48 @@ function isDefined($key) {
*
* Variable name
*
* @param $name
* @param $var variable name or array(0 => variable name, 'filters' => filters array)
* @return unknown_type
*/
function resolve($name) {
function resolve($var) {

# if $var is array - it contains filters to apply
$filters = array();
if ( is_array($var) ) {

$name = array_shift($var);
$filters = isset($var['filters'])? $var['filters'] : array();

}
else $name = $var;

$result = null;

# Lookup basic types, null, boolean, numeric and string
# Variable starts with : (:users.name) to short-circuit lookup
if ($name[0] === ':') {
$object = $this->getVariable(substr($name, 1));
if (!is_null($object)) return $object;
if (!is_null($object)) $result = $object;
} else {
if ($name === 'true') {
return true;
$result = true;
}
elseif ($name === 'false') {
return false;
$result = false;
}
elseif (preg_match('/^-?\d+(\.\d+)?$/', $name, $matches)) {
return isset($matches[1])? floatval($name) : intval($name);
$result = isset($matches[1])? floatval($name) : intval($name);
}
elseif (preg_match('/^"([^"\\\\]*(?:\\.[^"\\\\]*)*)"|' .
'\'([^\'\\\\]*(?:\\.[^\'\\\\]*)*)\'$/', $name)) {
return stripcslashes(substr($name, 1, -1));
$result = stripcslashes(substr($name, 1, -1));
}
}
if (!empty(self::$lookupTable)) {
return $this->externalLookup($name);
$result = $this->externalLookup($name);
}
return null;
$result = $this->applyFilters($result,$filters);
return $result;
}

function getVariable($name) {
Expand Down Expand Up @@ -159,15 +173,10 @@ function getVariable($name) {
}

function applyFilters($object, $filters) {
$safe = false;

foreach ($filters as $filter) {
$name = substr(array_shift($filter), 1);
$args = $filter;
$safe = !$safe && $name === 'safe';

if ($this->autoescape && $escaped = $name === 'escape')
continue;

if (isset(h2o::$filters[$name])) {
foreach ($args as $i => $argument) {
Expand All @@ -185,14 +194,31 @@ function applyFilters($object, $filters) {
$object = call_user_func_array(h2o::$filters[$name], $args);
}
}
$should_escape = $this->autoescape || isset($escaped) && $escaped;

if ($should_escape && !$safe) {
$object = htmlspecialchars($object);
}
return $object;
}

function escape($value, $var) {

$safe = false;
$filters = (is_array($var) && isset($var['filters']))? $var['filters'] : array();

foreach ( $filters as $filter ) {

$name = substr(array_shift($filter), 1);
$safe = !$safe && ($name === 'safe');

$escaped = $name === 'escape';
}

$should_escape = $this->autoescape || isset($escaped) && $escaped;

if ( ($should_escape && !$safe)) {
$value = htmlspecialchars($value);
}

return $value;
}

function externalLookup($name) {
if (!empty(self::$lookupTable)) {
foreach (self::$lookupTable as $lookup) {
Expand Down
4 changes: 2 additions & 2 deletions h2o/nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function __construct($variable, $filters, $position = 0) {

function render($context, $stream) {
$value = $context->resolve($this->variable);
$value = $context->applyFilters($value, $this->filters);
$stream->write($value);
$value = $context->escape($value, $this->variable);
$stream->write($value);
}
}

Expand Down
8 changes: 6 additions & 2 deletions h2o/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ static function parseArguments($source = null, $fpos = 0){
$current_buffer = &$filter_buffer;
}
elseif ($token == 'filter_end') {
if (count($filter_buffer))
$result[] = $filter_buffer;
if (count($filter_buffer)) {

$i = count($result)-1;
if ( is_array($result[$i]) ) $result[$i]['filters'][] = $filter_buffer;
else $result[$i] = array(0 => $result[$i], 'filters' => array($filter_buffer));
}
$current_buffer = &$result;
}
elseif ($token == 'boolean') {
Expand Down
6 changes: 5 additions & 1 deletion spec/parser_spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ class Describe_Argument_Lexer extends SimpleSpec {
function should_parse_named_arguments() {
$result = $this->parse("something | filter 11, name: 'something', age: 18, var: variable, active: true");
$expected = array(
':something', array(':filter', 11, array('name' => "'something'", 'age' => 18, 'var' => ':variable', 'active'=>'true'))
array(
':something', 'filters' => array(
array(':filter', 11, array('name' => "'something'", 'age' => 18, 'var' => ':variable', 'active'=>'true'))
)
)
);
expects($result)->should_be($expected);
}
Expand Down

0 comments on commit 79befdd

Please sign in to comment.