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

svn at limb-project.com svn at limb-project.com
Fri Sep 28 09:25:42 MSD 2007


Author: pachanga
Date: 2007-09-28 09:25:42 +0400 (Fri, 28 Sep 2007)
New Revision: 6340
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6340

Modified:
   3.x/trunk/limb/macro/src/tags/output.tag.php
   3.x/trunk/limb/macro/tests/cases/tags/lmbMacroOutputTagTest.class.php
Log:
-- making output tag implementation more robust for incomplete data inputs

Modified: 3.x/trunk/limb/macro/src/tags/output.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/output.tag.php	2007-09-27 22:04:18 UTC (rev 6339)
+++ 3.x/trunk/limb/macro/src/tags/output.tag.php	2007-09-28 05:25:42 UTC (rev 6340)
@@ -29,29 +29,29 @@
     if(strpos($this->tag, '.') === false)
       return 'echo ' . $this->tag . ';';
 
-    $result = '';
+    $expr = '';
 
+    $items = explode('.', $this->tag);
+
+    //first item is variable itself
+    $var = $items[0];
     $tmp = $code->getTempVarRef();
     $code->writePHP($tmp . "='';");
-
-    $items = explode('.', $this->tag);
-    //first item is variable itself
-    $prev = $items[0];
     for($i=1; $i<sizeof($items); $i++)
     {
       $item = $items[$i];
-      $result .= 'if((is_array(' . $prev . ') && isset(' . $prev . '["' . $item . '"])) || ' . 
-                 '(is_object(' . $prev . ') && ' . $tmp . '=' . $prev . '->get("' . $item . '")))' .  
-                 '{ if(is_array(' . $prev . '))' . $tmp . ' = ' . $prev . '["' . $item . '"];';
-      $prev = $tmp;
+      $expr .= 'if((is_array(' . $var . ') && isset(' . $var . '["' . $item . '"])) || ' . 
+               '(is_object(' . $var . ') && ' . $tmp . '=' . $var . '->get("' . $item . '")))' .  
+               '{if(is_array(' . $var . '))' . $tmp . ' = ' . $var . '["' . $item . '"];';
+      $var = $tmp;
     }
 
     //closing brackets
     for($i=1; $i<sizeof($items); $i++)
-      $result .= '}';
+      $expr .= '}else{' . $tmp . '="";}';
 
-    $result .= "echo $tmp;";
-    return $result;
+    $expr .= "echo $tmp;";
+    return $expr;
   }
 }
 

Modified: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroOutputTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroOutputTagTest.class.php	2007-09-27 22:04:18 UTC (rev 6339)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroOutputTagTest.class.php	2007-09-28 05:25:42 UTC (rev 6340)
@@ -48,6 +48,27 @@
     $this->assertEqual($out, 'Hey');
   }
 
+  function testBrokenChainOutputForArray()
+  {
+    $content = '<%$#var.foo.bar.baz%>';
+
+    $tpl = $this->_createTemplate($content, 'tpl.html');
+
+    $macro = $this->_createMacro($tpl);
+
+    $macro->set('var', null);
+    $out = $macro->render();
+    $this->assertEqual($out, '');
+
+    $macro->set('var', array('foo' => null));
+    $out = $macro->render();
+    $this->assertEqual($out, '');
+
+    $macro->set('var', array('foo' => array('bar' => null)));
+    $out = $macro->render();
+    $this->assertEqual($out, '');
+  }
+
   function testSimpleChainedOutputForObject()
   {
     $content = '<%$#var.foo.bar%>';
@@ -61,6 +82,27 @@
     $this->assertEqual($out, 'Hey');
   }
 
+  function testBrokenChainOutputForObject()
+  {
+    $content = '<%$#var.foo.bar.baz%>';
+
+    $tpl = $this->_createTemplate($content, 'tpl.html');
+
+    $macro = $this->_createMacro($tpl);
+
+    $macro->set('var', null);
+    $out = $macro->render();
+    $this->assertEqual($out, '');
+
+    $macro->set('var', new lmbObject(array('foo' => null)));
+    $out = $macro->render();
+    $this->assertEqual($out, '');
+
+    $macro->set('var', new lmbObject(array('foo' => new lmbObject(array('bar' => null)))));
+    $out = $macro->render();
+    $this->assertEqual($out, '');
+  }
+
   function testChainedOutputForMixedArraysAndObjects()
   {
     $content = '<%$#var.foo.bar%>';
@@ -74,6 +116,19 @@
     $this->assertEqual($out, 'Hey'); 
   }
 
+  function testBrokenChainOutputForMixedArraysAndObjects()
+  {
+    $content = '<%$#var.foo.bar.baz%>';
+
+    $tpl = $this->_createTemplate($content, 'tpl.html');
+
+    $macro = $this->_createMacro($tpl);
+
+    $macro->set('var', new lmbObject(array('foo' => array('bar' => null))));
+    $out = $macro->render();
+    $this->assertEqual($out, '');
+  }
+
   protected function _createMacro($file)
   {
     $base_dir = LIMB_VAR_DIR . '/tpl';



More information about the limb-svn mailing list