[limb-svn] r6505 - in 3.x/trunk/limb/macro: src tests/cases

svn at limb-project.com svn at limb-project.com
Thu Nov 8 16:23:49 MSK 2007


Author: serega
Date: 2007-11-08 16:23:49 +0300 (Thu, 08 Nov 2007)
New Revision: 6505
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6505

Modified:
   3.x/trunk/limb/macro/src/lmbMacroExpressionNode.class.php
   3.x/trunk/limb/macro/src/lmbMacroFilterParser.class.php
   3.x/trunk/limb/macro/tests/cases/lmbMacroFilterParserTest.class.php
   3.x/trunk/limb/macro/tests/cases/lmbMacroFiltersAcceptanceTest.class.php
   3.x/trunk/limb/macro/tests/cases/lmbMacroOutputExpressionTest.class.php
Log:
-- filter now supports params. Filter params is any php-code (note that $#var is parsed as $this->var anyway), variable or constant.

Modified: 3.x/trunk/limb/macro/src/lmbMacroExpressionNode.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroExpressionNode.class.php	2007-11-08 10:53:24 UTC (rev 6504)
+++ 3.x/trunk/limb/macro/src/lmbMacroExpressionNode.class.php	2007-11-08 13:23:49 UTC (rev 6505)
@@ -85,6 +85,7 @@
 
     $filter_class = $filter_info->getClass();
     $filter = new $filter_class($base);
+    $filter->setParams($params);
     return $filter;
   }
 

Modified: 3.x/trunk/limb/macro/src/lmbMacroFilterParser.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroFilterParser.class.php	2007-11-08 10:53:24 UTC (rev 6504)
+++ 3.x/trunk/limb/macro/src/lmbMacroFilterParser.class.php	2007-11-08 13:23:49 UTC (rev 6505)
@@ -59,9 +59,10 @@
     $this->position = 0;
 
     $filters_expressions = $this->_extractFiltersExpressions();
-
+    
     foreach($filters_expressions as $filter_expression)
     {
+      $filter_spec = array();
       $result = preg_match('/\G\s*([A-Za-z][A-Za-z0-9_.]*)/u', $filter_expression, $match, PREG_OFFSET_CAPTURE);
 
       if(!$result)
@@ -69,13 +70,16 @@
 
       $position = $match[0][1];
       $filter_name = $match[1][0];
-      $filters[$filter_name] = array('name' => $filter_name,
-                                     'expression' => $filter_expression,
-                                     'params' => "");
+      $filter_spec = array('name' => $filter_name,
+                           'expression' => $filter_expression,
+                           'params' => array());
 
       $params_expression = substr($filter_expression, strlen($filter_name));
       if(!$params_expression)
+      {
+        $filters[] = $filter_spec;
         continue;
+      }
 
       if(strlen($params_expression))
       {
@@ -84,16 +88,57 @@
           $this->context->raise('Unexpected symbol after filter name');
       }
 
-      $params = substr($params_expression, $params_start_position + 1);
-      if (strlen($params) == 0)
+      $params_str = substr($params_expression, $params_start_position + 1);
+      if (strlen($params_str) == 0)
         $this->context->raise('Filter params expected after ":" symbol');
 
-      $filters[$filter_name]['params'] = $params;
+      $filter_spec['params'] = $this->_parseParams($params_str);
+      $filters[] = $filter_spec; 
     }
 
     return $filters;
   }
+  
+  protected function _parseParams($params_string)
+  {
+    $params = array();
+    
+    $length = strlen($params_string);
+    $this->position = 0;
+    $this->text = $params_string;
 
+    do
+    {
+      $token = $this->getToken('/\G("|\'|,|[^\'",]+)/u');
+      if ($token === FALSE)
+      {
+        $filters_expressions[] = $this->text;
+        break;
+      }
+
+      if ($token == '"' || $token == "'")
+      {
+        $string = $this->getToken('/\G([^' . $token . ']*' . $token . ')/u');
+
+        if ($string === FALSE)
+          $this->context->raise("Expecting a string literal in filter param");
+      }
+      elseif($token == ',')
+      {
+        $params[] = substr($this->text, 0, $this->position - 1);
+        $this->text = substr($this->text, $this->position);
+        $length = strlen($this->text);
+        $this->position = 0;
+      }
+    }
+    while($this->position < $length);
+
+    //ensures the last param added
+    $params[] = substr($this->text, 0, $this->position );
+    
+    return $params;
+  }
+
   protected function _extractFiltersExpressions()
   {
     $length = strlen($this->text);
@@ -128,9 +173,6 @@
 
     //ensures the last filter expression added
     $filters_expressions[] = substr($this->text, 0, $this->position );
-    $this->text = substr($this->text, $this->position);
-    $length = strlen($this->text);
-    $this->position = 0;
 
     return $filters_expressions;
   }

Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroFilterParserTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroFilterParserTest.class.php	2007-11-08 10:53:24 UTC (rev 6504)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroFilterParserTest.class.php	2007-11-08 13:23:49 UTC (rev 6505)
@@ -26,9 +26,9 @@
   function testName()
   {
     $filters = $this->parser->parse($expression = 'filter');
-    $this->assertEqual($filters, array('filter' => array('name' => 'filter',
-                                                         'expression' => 'filter',
-                                                         'params' => "")));
+    $this->assertEqual($filters, array(array('name' => 'filter',
+                                             'expression' => 'filter',
+                                             'params' => array())));
   }
 
   function testEmptyName()
@@ -79,58 +79,58 @@
   function testOneParam()
   {
     $filters = $this->parser->parse($expression = 'filter:$arg');
-    $this->assertEqual($filters, array('filter' => array('name' => 'filter',
-                                                         'expression' => 'filter:$arg',
-                                                         'params' => '$arg')));
+    $this->assertEqual($filters, array(array('name' => 'filter',
+                                             'expression' => 'filter:$arg',
+                                             'params' => array('$arg'))));
   }
 
   function testTwoParams()
   {
     $filters = $this->parser->parse($expression = 'filter:$arg1,"arg2"');
-    $this->assertEqual($filters, array('filter' => array('name' => 'filter',
-                                                         'expression' => 'filter:$arg1,"arg2"',
-                                                         'params' => '$arg1,"arg2"')));
+    $this->assertEqual($filters, array(array('name' => 'filter',
+                                             'expression' => 'filter:$arg1,"arg2"',
+                                             'params' => array('$arg1','"arg2"'))));
   }
 
   function testSpaceInParams()
   {
     $filters = $this->parser->parse($expression = 'filter:" "');
-    $this->assertEqual($filters, array('filter' => array('name' => 'filter',
-                                                         'expression' => 'filter:" "',
-                                                         'params' => '" "')));
+    $this->assertEqual($filters, array(array('name' => 'filter',
+                                             'expression' => 'filter:" "',
+                                             'params' => array('" "'))));
   }
 
   function testTwoFiltersNoParams()
   {
     $filters = $this->parser->parse($expression = 'filter1|filter2');
-    $this->assertEqual($filters, array('filter1' => array('name' => 'filter1',
-                                                         'expression' => 'filter1',
-                                                         'params' => ''),
-                                       'filter2' => array('name' => 'filter2',
-                                                         'expression' => 'filter2',
-                                                         'params' => '')));
+    $this->assertEqual($filters, array(array('name' => 'filter1',
+                                             'expression' => 'filter1',
+                                             'params' => array()),
+                                       array('name' => 'filter2',
+                                             'expression' => 'filter2',
+                                             'params' => array())));
   }
 
   function testTwoFiltersWithParams()
   {
     $filters = $this->parser->parse($expression = 'filter1: $arg1, arg2 |filter2: arg3');
-    $this->assertEqual($filters, array('filter1' => array('name' => 'filter1',
-                                                         'expression' => 'filter1: $arg1, arg2 ',
-                                                         'params' => ' $arg1, arg2 '),
-                                       'filter2' => array('name' => 'filter2',
-                                                         'expression' => 'filter2: arg3',
-                                                         'params' => ' arg3')));
+    $this->assertEqual($filters, array(array('name' => 'filter1',
+                                             'expression' => 'filter1: $arg1, arg2 ',
+                                             'params' => array(' $arg1',' arg2 ')),
+                                       array('name' => 'filter2',
+                                             'expression' => 'filter2: arg3',
+                                             'params' => array(' arg3'))));
   }
 
   function testTwoFiltersWithSeparatorInParams()
   {
     $filters = $this->parser->parse($expression = 'filter1: "x|y", arg2 |filter2: arg3');
-    $this->assertEqual($filters, array('filter1' => array('name' => 'filter1',
-                                                         'expression' => 'filter1: "x|y", arg2 ',
-                                                         'params' => ' "x|y", arg2 '),
-                                       'filter2' => array('name' => 'filter2',
-                                                         'expression' => 'filter2: arg3',
-                                                         'params' => ' arg3')));
+    $this->assertEqual($filters, array(array('name' => 'filter1',
+                                             'expression' => 'filter1: "x|y", arg2 ',
+                                             'params' => array(' "x|y"',' arg2 ')),
+                                       array('name' => 'filter2',
+                                             'expression' => 'filter2: arg3',
+                                             'params' => array(' arg3'))));
   }
 }
 

Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroFiltersAcceptanceTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroFiltersAcceptanceTest.class.php	2007-11-08 10:53:24 UTC (rev 6504)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroFiltersAcceptanceTest.class.php	2007-11-08 13:23:49 UTC (rev 6505)
@@ -27,7 +27,10 @@
 {
   function getValue()
   {
-    return 'trim(' . $this->base->getValue() . ')';
+    if(!isset($this->params[0]))
+      return 'trim(' . $this->base->getValue() . ')';
+    else
+      return 'trim(' . $this->base->getValue() . ', ' . $this->params[0] . ')';
   }
 }
 
@@ -58,5 +61,24 @@
     $out = $tpl->render();
     $this->assertEqual($out, 'HELLO');
   }
+  
+  function testFilterWithParams()
+  {
+    $code = '{$#var|trim|trim:"/"|uppercase}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+    $tpl->set('var', '  /hello/  ');
+    $out = $tpl->render();
+    $this->assertEqual($out, 'HELLO');
+  }
+
+  function testFilterWithVariablesInParams()
+  {
+    $code = '{$#var|trim|trim:$#foo|uppercase}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+    $tpl->set('var', '  /hello/  ');
+    $tpl->set('foo', '/');
+    $out = $tpl->render();
+    $this->assertEqual($out, 'HELLO');
+  }
 }
 

Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroOutputExpressionTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroOutputExpressionTest.class.php	2007-11-08 10:53:24 UTC (rev 6504)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroOutputExpressionTest.class.php	2007-11-08 13:23:49 UTC (rev 6505)
@@ -199,7 +199,7 @@
     $this->assertEqual($out, '<h1>20</h1>');
   }
 
-  function testNestedExpressionPaths()
+  function testNestedFunctionCalls()
   {
     $code = '<h1>{$#bar->func2($#foo->func2(10, 20), "aaa")}</h1>';
     $tpl = $this->_createMacroTemplate($code, 'tpl.html');



More information about the limb-svn mailing list