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

svn at limb-project.com svn at limb-project.com
Mon Dec 3 12:08:51 MSK 2007


Author: serega
Date: 2007-12-03 12:08:51 +0300 (Mon, 03 Dec 2007)
New Revision: 6556
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6556

Added:
   3.x/trunk/limb/macro/src/tags/form/input.tag.php
   3.x/trunk/limb/macro/src/tags/form/lmbMacroFormElementTag.class.php
   3.x/trunk/limb/macro/src/tags/form/lmbMacroInputWidget.class.php
   3.x/trunk/limb/macro/tests/cases/tags/form/lmbMacroInputTagTest.class.php
Modified:
   3.x/trunk/limb/macro/src/tags/form/form.tag.php
   3.x/trunk/limb/macro/src/tags/form/lmbMacroFormFieldWidget.class.php
   3.x/trunk/limb/macro/src/tags/form/lmbMacroRuntimeWidgetTag.class.php
   3.x/trunk/limb/macro/tests/cases/tags/form/lmbMacroFormTagTest.class.php
Log:
-- initial revision of {{input}}
-- lmbMacroFormElementTag added - a base class for all form element tags
-- better tests for {{form}} tag

Modified: 3.x/trunk/limb/macro/src/tags/form/form.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/form/form.tag.php	2007-12-01 10:31:03 UTC (rev 6555)
+++ 3.x/trunk/limb/macro/src/tags/form/form.tag.php	2007-12-03 09:08:51 UTC (rev 6556)
@@ -17,14 +17,10 @@
  */
 class lmbMacroFormTag extends lmbMacroRuntimeWidgetTag
 {
+  protected $html_tag = 'form';
   protected $widget_class_name = 'lmbMacroFormWidget';
   protected $widget_include_file = 'limb/macro/src/tags/form/lmbMacroFormWidget.class.php';
   
-  function __construct($location, $tag, $tag_info)
-  {
-    parent :: __construct($location, $tag, $tag_info, 'form');
-  }
-  
   protected function _generateBeforeOpeningTag($code)
   {
     $form = $this->getWidgetVar();
@@ -32,8 +28,9 @@
     // передача указанного контейнера с данными в виджет формы
     if($this->has('from'))
     {
-      $from = $this->getEscaped('from');       
+      $from = $this->get('from');       
       $code->writePHP("{$form}->setDatasource({$from});\n");
+      $this->remove('from');
     }
     
     $error_list_id = $form . '_error_list';

Added: 3.x/trunk/limb/macro/src/tags/form/input.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/form/input.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/form/input.tag.php	2007-12-03 09:08:51 UTC (rev 6556)
@@ -0,0 +1,46 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/macro/src/tags/form/lmbMacroFormElementTag.class.php');
+
+/**
+ * Macro analog for html <input> tag
+ * @tag input
+ * @forbid_end_tag  
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroInputTag extends lmbMacroFormElementTag
+{
+  protected $html_tag = 'input';
+  protected $widget_include_file = 'limb/macro/src/tags/form/lmbMacroInputWidget.class.php';
+  
+  function preParse()
+  {
+    $type = strtolower($this->get('type'));
+    switch ($type)
+    {
+      case 'text': 
+      case 'hidden':
+      case 'image':
+      case 'button':
+        $this->widget_class_name = 'lmbMacroInputWidget';
+        break;
+      case 'password':
+      case 'submit':
+      case 'reset':
+      case 'file':
+        $this->widget_class_name = 'lmbMacroFormFieldWidget';
+        break;
+      default:
+        $this->raiseCompilerError('Unrecognized type attribute for input tag');
+    }
+  }
+}
+

Added: 3.x/trunk/limb/macro/src/tags/form/lmbMacroFormElementTag.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/form/lmbMacroFormElementTag.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/form/lmbMacroFormElementTag.class.php	2007-12-03 09:08:51 UTC (rev 6556)
@@ -0,0 +1,24 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/macro/src/tags/form/lmbMacroRuntimeWidgetTag.class.php');
+
+/**
+ * Base class for any form element tag
+ */
+class lmbMacroFormElementTag extends lmbMacroRuntimeWidgetTag
+{
+  function _generateWidget($code_writer)
+  {
+    parent :: _generateWidget($code_writer);
+    if($form_tag = $this->findParentByClass('lmbMacroFormTag'))
+      $code_writer->writePHP("{$this->getWidgetVar()}->setForm({$form_tag->getWidgetVar()});\n");
+  }  
+}
+

Modified: 3.x/trunk/limb/macro/src/tags/form/lmbMacroFormFieldWidget.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/form/lmbMacroFormFieldWidget.class.php	2007-12-01 10:31:03 UTC (rev 6555)
+++ 3.x/trunk/limb/macro/src/tags/form/lmbMacroFormFieldWidget.class.php	2007-12-03 09:08:51 UTC (rev 6556)
@@ -19,12 +19,18 @@
 class lmbMacroFormFieldWidget extends lmbMacroHtmlTagWidget
 {
   protected $has_errors = false;
+  protected $form;
   
   function getDisplayName()
   {
     return $this->id;
   }
   
+  function setForm($form)
+  {
+    $this->form = $form;
+  }
+  
   function setErrorState($has_errors = true)
   {
     $this->has_errors = $has_errors;
@@ -34,5 +40,19 @@
   {
     return $this->has_errors;
   }
+  
+  function getValue()
+  {
+    if($this->hasAttribute('value'))
+      return $this->getAttribute('value');
+    
+    if(is_object($this->form))
+    {
+      $ds = $this->form->getDatasource();
+      $id = $this->getId();
+      if(isset($ds[$id]))
+        return $ds[$id];
+    }
+  }   
 }
 

Added: 3.x/trunk/limb/macro/src/tags/form/lmbMacroInputWidget.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/form/lmbMacroInputWidget.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/form/lmbMacroInputWidget.class.php	2007-12-03 09:08:51 UTC (rev 6556)
@@ -0,0 +1,33 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com 
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
+ */
+lmb_require('limb/macro/src/tags/form/lmbMacroFormFieldWidget.class.php');
+
+/**
+ * class lmbMacroInputWidget
+ * A runtime widget for input tag of "text" and "hidden" types
+ *
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroInputWidget extends lmbMacroFormFieldWidget
+{
+  function renderAttributes()
+  {
+    if(!$this->hasAttribute('value'))
+    {
+      if($value = $this->getValue())
+        $this->setAttribute('value', $value);
+      else
+        $this->setAttribute('value', "");
+    }
+
+   parent :: renderAttributes();
+  }
+}
+

Modified: 3.x/trunk/limb/macro/src/tags/form/lmbMacroRuntimeWidgetTag.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/form/lmbMacroRuntimeWidgetTag.class.php	2007-12-01 10:31:03 UTC (rev 6555)
+++ 3.x/trunk/limb/macro/src/tags/form/lmbMacroRuntimeWidgetTag.class.php	2007-12-03 09:08:51 UTC (rev 6556)
@@ -19,14 +19,13 @@
   protected $widget_class_name;
   protected $html_tag;
   
-  function __construct($location, $tag, $tag_info, $html_tag)
+  function preParse()
   {
-    parent :: __construct($location, $tag, $tag_info);
-
-    $this->html_tag = $html_tag;
-    
     if(!$this->widget_class_name)
       $this->raise('Please specify "$widget_class_name" property of the tag class "' . get_class($this) .'"');
+
+    if(!$this->html_tag)
+      $this->raise('Please specify "$html_tag" property of the tag class "' . get_class($this) .'"');
   }
 
   protected function _generateBeforeContent($code_writer)
@@ -106,7 +105,7 @@
       {
         $value = $this->attributes[$key]->getValue();
         $code->writePHP("{$widget}->setAttribute('{$key}',");
-        $code->writePHP($this->attributes[$key]->getEscaped());
+        $code->writePHP($this->getEscaped($key));
         $code->writePHP(");\n");
       }
     } 

Modified: 3.x/trunk/limb/macro/tests/cases/tags/form/lmbMacroFormTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/form/lmbMacroFormTagTest.class.php	2007-12-01 10:31:03 UTC (rev 6555)
+++ 3.x/trunk/limb/macro/tests/cases/tags/form/lmbMacroFormTagTest.class.php	2007-12-03 09:08:51 UTC (rev 6556)
@@ -51,4 +51,20 @@
     $out = $page->render();
     $this->assertEqual($out, '<form name="my_form">1111</form>');
   }  
+  
+  function testFormTakesDatasourceByFromAttribute()
+  {
+    $template = '{{form name="my_form" from="$#form_data"}}'.
+                '<?php $ds = $this->form_my_form->getDatasource(); '.
+                ' echo $ds["value"];'.
+                '?>'.
+                '{{/form}}';
+
+    $page = $this->_createMacroTemplate($template, 'tpl.html');
+    
+    $page->set('form_data', array('value' => 1111));
+ 
+    $out = $page->render();
+    $this->assertEqual($out, '<form name="my_form">1111</form>');
+  }
 }

Added: 3.x/trunk/limb/macro/tests/cases/tags/form/lmbMacroInputTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/form/lmbMacroInputTagTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/tags/form/lmbMacroInputTagTest.class.php	2007-12-03 09:08:51 UTC (rev 6556)
@@ -0,0 +1,84 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+require_once('limb/macro/src/tags/form/lmbMacroFormErrorList.class.php'); 
+ 
+
+class lmbMacroInputTagTest extends lmbBaseMacroTest
+{
+  function testTypeText()
+  {
+    $template = '{{input type="text" name="my_input" value="$#var"}}';
+
+    $page = $this->_createMacroTemplate($template, 'tpl.html');
+    $page->set('var', 100);
+ 
+    $out = $page->render();
+    $this->assertEqual($out, '<input type="text" name="my_input" value="100" />');
+  }
+
+  function testTypeTextRendersValueAttributeInAnyCase()
+  {
+    $template = '{{input type="text" name="my_input"}}';
+
+    $page = $this->_createMacroTemplate($template, 'tpl.html');
+ 
+    $out = $page->render();
+    $this->assertEqual($out, '<input type="text" name="my_input" value="" />');
+  }
+
+  function testTypeTextTakesValueFromFormIfPossible()
+  {
+    $template = '{{form name="my_form" from="$#form_data"}}{{input type="text" name="my_input"}}{{/form}}';
+
+    $page = $this->_createMacroTemplate($template, 'tpl.html');
+    $page->set('form_data', array('my_input' => 100));
+ 
+    $out = $page->render();
+    $this->assertEqual($out, '<form name="my_form"><input type="text" name="my_input" value="100" /></form>');
+  }   
+  
+  function testTypesHiddenAndButtonAndImage()
+  {
+    $template = '{{input type="hidden" name="my_hidden" value="$#for_hidden"}}'.
+                '{{input type="button" name="my_button" value="$#for_button"}}'.
+                '{{input type="image" src="some_path" name="my_image" value="$#for_image"}}';
+
+    $page = $this->_createMacroTemplate($template, 'tpl.html');
+    $page->set('for_hidden', 10);
+    $page->set('for_button', 20);
+    $page->set('for_image', 30);
+ 
+    $out = $page->render();
+    $expected = '<input type="hidden" name="my_hidden" value="10" />'.
+                '<input type="button" name="my_button" value="20" />'.
+                '<input type="image" src="some_path" name="my_image" value="30" />';
+    $this->assertEqual($out, $expected);
+  }
+  
+  function testTypesFileAndSubmitAndPasswordAndResetAndFile_DontTakeValueFromFormDatasource()
+  {
+    $template = '{{form name="my_form" from="$#form_data"}}'.
+                '{{input type="file" name="my_file"}}'.
+                '{{input type="submit" name="my_submit"}}'.
+                '{{input type="password" name="my_password"}}'.
+                '{{input type="reset" name="my_reset"}}'.
+                '{{/form}}';
+
+    $page = $this->_createMacroTemplate($template, 'tpl.html');
+    $page->set('form_data', array('my_file' => 10, 'my_submit' => 20, 'my_password' => 30, 'my_reset' => 40));
+ 
+    $out = $page->render();
+    $expected = '<form name="my_form"><input type="file" name="my_file" />'.
+                '<input type="submit" name="my_submit" />'.
+                '<input type="password" name="my_password" />'.
+                '<input type="reset" name="my_reset" /></form>';
+    $this->assertEqual($out, $expected);
+  }
+}



More information about the limb-svn mailing list