Browse Source

ZF-11477: Refactor form view helpers to use getClosingBracket

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24750 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan 13 năm trước cách đây
mục cha
commit
328a20e31f

+ 1 - 7
library/Zend/View/Helper/FormCheckbox.php

@@ -81,12 +81,6 @@ class Zend_View_Helper_FormCheckbox extends Zend_View_Helper_FormElement
             $disabled = ' disabled="disabled"';
         }
 
-        // XHTML or HTML end tag?
-        $endTag = ' />';
-        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
-            $endTag= '>';
-        }
-
         // build the element
         $xhtml = '';
         if (!$disable && !strstr($name, '[]')) {
@@ -99,7 +93,7 @@ class Zend_View_Helper_FormCheckbox extends Zend_View_Helper_FormElement
                 . $checkedOptions['checkedString']
                 . $disabled
                 . $this->_htmlAttribs($attribs)
-                . $endTag;
+                . $this->getClosingBracket();
 
         return $xhtml;
     }

+ 1 - 7
library/Zend/View/Helper/FormFile.php

@@ -62,19 +62,13 @@ class Zend_View_Helper_FormFile extends Zend_View_Helper_FormElement
             $disabled = ' disabled="disabled"';
         }
 
-        // XHTML or HTML end tag?
-        $endTag = ' />';
-        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
-            $endTag= '>';
-        }
-
         // build the element
         $xhtml = '<input type="file"'
                 . ' name="' . $this->view->escape($name) . '"'
                 . ' id="' . $this->view->escape($id) . '"'
                 . $disabled
                 . $this->_htmlAttribs($attribs)
-                . $endTag;
+                . $this->getClosingBracket();
 
         return $xhtml;
     }

+ 1 - 7
library/Zend/View/Helper/FormImage.php

@@ -80,12 +80,6 @@ class Zend_View_Helper_FormImage extends Zend_View_Helper_FormElement
             $disabled = ' disabled="disabled"';
         }
 
-        // XHTML or HTML end tag?
-        $endTag = ' />';
-        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
-            $endTag= '>';
-        }
-
         // build the element
         $xhtml = '<input type="image"'
                 . ' name="' . $this->view->escape($name) . '"'
@@ -94,7 +88,7 @@ class Zend_View_Helper_FormImage extends Zend_View_Helper_FormElement
                 . $value
                 . $disabled
                 . $this->_htmlAttribs($attribs)
-                . $endTag;
+                . $this->getClosingBracket();
 
         return $xhtml;
     }

+ 1 - 7
library/Zend/View/Helper/FormPassword.php

@@ -74,12 +74,6 @@ class Zend_View_Helper_FormPassword extends Zend_View_Helper_FormElement
             unset($attribs['renderPassword']);
         }
 
-        // XHTML or HTML end tag?
-        $endTag = ' />';
-        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
-            $endTag= '>';
-        }
-
         // render the element
         $xhtml = '<input type="password"'
                 . ' name="' . $this->view->escape($name) . '"'
@@ -87,7 +81,7 @@ class Zend_View_Helper_FormPassword extends Zend_View_Helper_FormElement
                 . $valueString
                 . $disabled
                 . $this->_htmlAttribs($attribs)
-                . $endTag;
+                . $this->getClosingBracket();
 
         return $xhtml;
     }

+ 1 - 7
library/Zend/View/Helper/FormRadio.php

@@ -123,12 +123,6 @@ class Zend_View_Helper_FormRadio extends Zend_View_Helper_FormElement
         // ensure value is an array to allow matching multiple times
         $value = (array) $value;
 
-        // XHTML or HTML end tag?
-        $endTag = ' />';
-        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
-            $endTag= '>';
-        }
-
         // Set up the filter - Alnum + hyphen + underscore
         require_once 'Zend/Filter/PregReplace.php';
         $pattern = @preg_match('/\pL/u', 'a') 
@@ -172,7 +166,7 @@ class Zend_View_Helper_FormRadio extends Zend_View_Helper_FormElement
                     . $checked
                     . $disabled
                     . $this->_htmlAttribs($attribs)
-                    . $endTag
+                    . $this->getClosingBracket()
                     . (('append' == $labelPlacement) ? $opt_label : '')
                     . '</label>';
 

+ 1 - 7
library/Zend/View/Helper/FormReset.php

@@ -64,12 +64,6 @@ class Zend_View_Helper_FormReset extends Zend_View_Helper_FormElement
             $disabled = ' disabled="disabled"';
         }
 
-        // get closing tag
-        $endTag = '>';
-        if ($this->view->doctype()->isXhtml()) {
-            $endTag = ' />';
-        }
-
         // Render button
         $xhtml = '<input type="reset"'
                . ' name="' . $this->view->escape($name) . '"'
@@ -82,7 +76,7 @@ class Zend_View_Helper_FormReset extends Zend_View_Helper_FormElement
         }
 
         // add attributes, close, and return
-        $xhtml .= $this->_htmlAttribs($attribs) . $endTag;
+        $xhtml .= $this->_htmlAttribs($attribs) . $this->getClosingBracket();
         return $xhtml;
     }
 }

+ 1 - 7
library/Zend/View/Helper/FormSubmit.php

@@ -67,12 +67,6 @@ class Zend_View_Helper_FormSubmit extends Zend_View_Helper_FormElement
             $id = ' id="' . $this->view->escape($id) . '"';
         }
 
-        // XHTML or HTML end tag?
-        $endTag = ' />';
-        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
-            $endTag= '>';
-        }
-
         // Render the button.
         $xhtml = '<input type="submit"'
                . ' name="' . $this->view->escape($name) . '"'
@@ -80,7 +74,7 @@ class Zend_View_Helper_FormSubmit extends Zend_View_Helper_FormElement
                . ' value="' . $this->view->escape($value) . '"'
                . $disabled
                . $this->_htmlAttribs($attribs)
-               . $endTag;
+               . $this->getClosingBracket();
 
         return $xhtml;
     }

+ 1 - 7
library/Zend/View/Helper/FormText.php

@@ -65,19 +65,13 @@ class Zend_View_Helper_FormText extends Zend_View_Helper_FormElement
             $disabled = ' disabled="disabled"';
         }
 
-        // XHTML or HTML end tag?
-        $endTag = ' />';
-        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
-            $endTag= '>';
-        }
-
         $xhtml = '<input type="text"'
                 . ' name="' . $this->view->escape($name) . '"'
                 . ' id="' . $this->view->escape($id) . '"'
                 . ' value="' . $this->view->escape($value) . '"'
                 . $disabled
                 . $this->_htmlAttribs($attribs)
-                . $endTag;
+                . $this->getClosingBracket();
 
         return $xhtml;
     }

+ 25 - 0
tests/Zend/View/Helper/FormImageTest.php

@@ -62,6 +62,8 @@ class Zend_View_Helper_FormImageTest extends PHPUnit_Framework_TestCase
     protected function setUp()
     {
         $this->view = new Zend_View();
+        $this->view->doctype('HTML4_LOOSE');  // Reset doctype to default
+        
         $this->helper = new Zend_View_Helper_FormImage();
         $this->helper->setView($this->view);
     }
@@ -92,6 +94,29 @@ class Zend_View_Helper_FormImageTest extends PHPUnit_Framework_TestCase
         $this->assertRegexp('/<input[^>]*?name="foo"/', $button);
         $this->assertRegexp('/<input[^>]*?type="image"/', $button);
     }
+    
+    /**
+     * @group ZF-11477
+     */
+    public function testRendersAsHtmlByDefault()
+    {
+        $test = $this->helper->formImage(array(
+            'name' => 'foo',
+        ));
+        $this->assertNotContains(' />', $test);
+    }
+
+    /**
+     * @group ZF-11477
+     */
+    public function testCanRendersAsXHtml()
+    {
+        $this->view->doctype('XHTML1_STRICT');
+        $test = $this->helper->formImage(array(
+            'name' => 'foo',
+        ));
+        $this->assertContains(' />', $test);
+    }
 }
 
 // Call Zend_View_Helper_FormImageTest::main() if this source file is executed directly.

+ 40 - 0
tests/Zend/View/Helper/FormRadioTest.php

@@ -59,6 +59,7 @@ class Zend_View_Helper_FormRadioTest extends PHPUnit_Framework_TestCase
     public function setUp()
     {
         $this->view   = new Zend_View();
+        $this->view->doctype('HTML4_LOOSE'); // Set default doctype
         $this->helper = new Zend_View_Helper_FormRadio();
         $this->helper->setView($this->view);
     }
@@ -429,6 +430,45 @@ class Zend_View_Helper_FormRadioTest extends PHPUnit_Framework_TestCase
         $this->assertRegExp('/<input([^>]*)(id="'.$name.'--1")([^>]*)(checked="checked")/', $html);
     }
     
+    /**
+     * @group ZF-11477
+     */
+    public function testRendersAsHtmlByDefault()
+    {
+        $options = array(
+            'foo' => 'Foo',
+            'bar' => 'Bar',
+            'baz' => 'Baz'
+        );
+        $html = $this->helper->formRadio(array(
+            'name'    => 'foo',
+            'options' => $options,
+        ));
+
+        $this->assertContains('value="foo">', $html);
+        $this->assertContains('value="bar">', $html);
+        $this->assertContains('value="baz">', $html);
+    }
+
+    /**
+     * @group ZF-11477
+     */
+    public function testCanRendersAsXHtml()
+    {
+        $this->view->doctype('XHTML1_STRICT');
+        $options = array(
+            'foo' => 'Foo',
+            'bar' => 'Bar',
+            'baz' => 'Baz'
+        );
+        $html = $this->helper->formRadio(array(
+            'name'    => 'foo',
+            'options' => $options,
+        ));
+        $this->assertContains('value="foo" />', $html);
+        $this->assertContains('value="bar" />', $html);
+        $this->assertContains('value="baz" />', $html);
+    }
 }
 
 // Call Zend_View_Helper_FormRadioTest::main() if this source file is executed directly.