From: Chuck Hagenbuch Date: Sun, 20 Dec 2009 08:21:47 +0000 (-0500) Subject: First pass at making form and tag helpers HTML 4.01 Strict X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=cb85520865b6b76b3398f188709b02db3442612c;p=horde.git First pass at making form and tag helpers HTML 4.01 Strict --- diff --git a/framework/View/lib/Horde/View/Helper/Form/InstanceTag/Form.php b/framework/View/lib/Horde/View/Helper/Form/InstanceTag/Form.php index 6fbb2909c..b6d5f4f3e 100644 --- a/framework/View/lib/Horde/View/Helper/Form/InstanceTag/Form.php +++ b/framework/View/lib/Horde/View/Helper/Form/InstanceTag/Form.php @@ -62,9 +62,7 @@ class Horde_View_Helper_Form_InstanceTag_Form extends Horde_View_Helper_Form_Ins } else { $checked = $this->isRadioButtonChecked($this->value($this->object()), $tagValue); } - if ($checked) { - $options['checked'] = 'checked'; - } + $options['checked'] = (boolean)$checked; $prettyTagValue = strval($tagValue); $prettyTagValue = preg_replace('/\s/', '_', $prettyTagValue); @@ -116,9 +114,7 @@ class Horde_View_Helper_Form_InstanceTag_Form extends Horde_View_Helper_Form_Ins } else { $checked = $this->isCheckBoxChecked($this->value($this->object()), $checkedValue); } - if ($checked) { - $options['checked'] = 'checked'; - } + $options['checked'] = (boolean)$checked; $options = $this->addDefaultNameAndId($options); // hidden must output first in PHP to not overwrite checkbox value diff --git a/framework/View/lib/Horde/View/Helper/FormTag.php b/framework/View/lib/Horde/View/Helper/FormTag.php index 18e2a91ef..ba01e4ff8 100644 --- a/framework/View/lib/Horde/View/Helper/FormTag.php +++ b/framework/View/lib/Horde/View/Helper/FormTag.php @@ -80,13 +80,11 @@ class Horde_View_Helper_FormTag extends Horde_View_Helper_Base public function checkBoxTag($name, $value = '1', $checked = false, $options = array()) { - $htmlOptions = array_merge(array('type' => 'checkbox', - 'name' => $name, - 'id' => $name, - 'value' => $value), $options); - if ($checked) { - $htmlOptions['checked'] = 'checked'; - } + $htmlOptions = array_merge(array('type' => 'checkbox', + 'name' => $name, + 'id' => $name, + 'value' => $value, + 'checked' => $checked), $options); return $this->tag('input', $htmlOptions); } @@ -99,10 +97,8 @@ class Horde_View_Helper_FormTag extends Horde_View_Helper_Base $htmlOptions = array_merge(array('type' => 'radio', 'name' => $name, 'id' => "{$name}_{$prettyTagValue}", - 'value' => $value), $options); - if ($checked) { - $htmlOptions['checked'] = 'checked'; - } + 'value' => $value, + 'checked' => $checked), $options); return $this->tag('input', $htmlOptions); } diff --git a/framework/View/lib/Horde/View/Helper/Tag.php b/framework/View/lib/Horde/View/Helper/Tag.php index 34de23862..d70f0b3b3 100644 --- a/framework/View/lib/Horde/View/Helper/Tag.php +++ b/framework/View/lib/Horde/View/Helper/Tag.php @@ -14,7 +14,7 @@ /** * Use these methods to generate HTML tags programmatically. - * By default, they output XHTML compliant tags. + * By default, they output HTML 4.01 Strict compliant tags. * * @author Mike Naberezny * @author Derek DeVries @@ -27,37 +27,32 @@ class Horde_View_Helper_Tag extends Horde_View_Helper_Base { /** - * HTML attributes that get converted from boolean to the attribute name: - * array('disabled' => true) becomes array('disabled' => 'disabled') + * Boolean HTML attributes: + * array('disabled' => true) is displayed as just "disabled". * * @var array */ - private $_booleanAttributes = array('disabled', 'readonly', 'multiple'); + private $_booleanAttributes = array('checked', 'disabled', 'multiple', 'readonly', 'selected'); /** - * Returns an empty HTML tag of type $name which by default is XHTML - * compliant. Setting $open to true will create an open tag compatible - * with HTML 4.0 and below. Add HTML attributes by passing an attributes - * hash to $options. For attributes with no value (like disabled and - * readonly), give it a value of TRUE in the $options array. + * Returns an empty HTML tag of type $name. Add HTML attributes by passing + * an attributes hash to $options. For attributes with no value (like + * disabled and readonly), give it a value of TRUE in the $options array. * * $this->tag("br") - * # =>
- * $this->tag("br", null, true) - * # =>
+ * # =>
* $this->tag("input", array('type' => 'text', 'disabled' => true)) - * # => + * # => * * @param string $name Tag name * @param string $options Tag attributes - * @param boolean $open Leave tag open for HTML 4.0 and below? - * @param string Generated HTML tag + * @return string Generated HTML tag */ - public function tag($name, $options = null, $open = false) + public function tag($name, $options = null) { return "<$name" - . ($options ? $this->tagOptions($options) : '') - . ($open ? '>' : ' />'); + . ($options ? $this->tagOptions($options) : '') + . '>'; } /** @@ -139,19 +134,19 @@ class Horde_View_Helper_Tag extends Horde_View_Helper_Base */ public function tagOptions($options) { - foreach ($options as $k => &$v) { + foreach ($options as $k => $v) { if ($v === null || $v === false) { unset($options[$k]); - } else { - if (in_array($k, $this->_booleanAttributes)) { - $v = $k; - } } } if (! empty($options)) { foreach ($options as $k => &$v) { - $v = $k . '="' . $this->escapeOnce($v) . '"'; + if (in_array($k, $this->_booleanAttributes)) { + $v = $k; + } else { + $v = $k . '="' . $this->escapeOnce($v) . '"'; + } } sort($options); return ' ' . implode(' ', $options); diff --git a/framework/View/test/Horde/View/Helper/FormTagTest.php b/framework/View/test/Horde/View/Helper/FormTagTest.php index 3398abd1b..06db8f02f 100644 --- a/framework/View/test/Horde/View/Helper/FormTagTest.php +++ b/framework/View/test/Horde/View/Helper/FormTagTest.php @@ -56,116 +56,116 @@ class Horde_View_Helper_FormTagTest extends Horde_Test_Functional public function testFormTagWithMethod() { $actual = $this->view->formTag(array(), array('method' => 'put')); - $expected = '
'; + $expected = '
'; $this->assertEquals($expected, $actual); } public function testCheckBoxTag() { $actual = $this->view->checkBoxTag('admin'); - $expected = ''; - $this->assertDomEquals($expected, $actual); + $expected = ''; + $this->assertHtmlDomEquals($expected, $actual); } public function testHiddenFieldTag() { $actual = $this->view->hiddenFieldTag('id', 3); - $expected = ''; - $this->assertDomEquals($expected, $actual); + $expected = ''; + $this->assertHtmlDomEquals($expected, $actual); } public function testFileFieldTag() { $actual = $this->view->fileFieldTag('id'); - $expected = ''; - $this->assertDomEquals($expected, $actual); + $expected = ''; + $this->assertHtmlDomEquals($expected, $actual); } public function testPasswordFieldTag() { $actual = $this->view->passwordFieldTag(); - $expected = ''; - $this->assertDomEquals($expected, $actual); + $expected = ''; + $this->assertHtmlDomEquals($expected, $actual); } public function testRadioButtonTag() { $actual = $this->view->radioButtonTag('people', 'david'); - $expected = ''; - $this->assertDomEquals($expected, $actual); + $expected = ''; + $this->assertHtmlDomEquals($expected, $actual); $actual = $this->view->radioButtonTag('num_people', 5); - $expected = ''; - $this->assertDomEquals($expected, $actual); + $expected = ''; + $this->assertHtmlDomEquals($expected, $actual); $actual = $this->view->radioButtonTag('gender', 'm') . $this->view->radioButtonTag('gender', 'f'); - $expected = '' - . ''; - $this->assertEquals($expected, $actual); // @todo assertDomEquals + $expected = '' + . ''; + $this->assertEquals($expected, $actual); // @todo assertHtmlDomEquals $actual = $this->view->radioButtonTag('opinion', '-1') . $this->view->radioButtonTag('opinion', '1'); - $expected = '' - . ''; - $this->assertEquals($expected, $actual); // @todo assertDomEquals + $expected = '' + . ''; + $this->assertEquals($expected, $actual); // @todo assertHtmlDomEquals } public function testSelectTag() { $actual = $this->view->selectTag('people', ''); $expected = ''; - $this->assertDomEquals($expected, $actual); + $this->assertHtmlDomEquals($expected, $actual); } public function testTextAreaTagSizeString() { $actual = $this->view->textAreaTag('body', 'hello world', array('size' => '20x40')); $expected = ''; - $this->assertDomEquals($expected, $actual); + $this->assertHtmlDomEquals($expected, $actual); } public function testTextAreaTagShouldDisregardSizeIfGivenAsAnInteger() { $actual = $this->view->textAreaTag('body', 'hello world', array('size' => 20)); $expected = ''; - $this->assertDomEquals($expected, $actual); + $this->assertHtmlDomEquals($expected, $actual); } public function testTextFieldTag() { $actual = $this->view->textFieldTag('title', 'Hello!'); - $expected = ''; - $this->assertDomEquals($expected, $actual); + $expected = ''; + $this->assertHtmlDomEquals($expected, $actual); } public function testTextFieldTagClassString() { $actual = $this->view->textFieldTag('title', 'Hello!', array('class' => 'admin')); - $expected = ''; - $this->assertDomEquals($expected, $actual); + $expected = ''; + $this->assertHtmlDomEquals($expected, $actual); } public function testBooleanOptions() { - $this->assertDomEquals('', + $this->assertHtmlDomEquals('', $this->view->checkBoxTag("admin", 1, true, array('disabled' => true, 'readonly' => "yes"))); - $this->assertDomEquals('', + $this->assertHtmlDomEquals('', $this->view->checkBoxTag('admin', 1, true, array('disabled' => false, 'readonly' => null))); - $this->assertDomEquals('', + $this->assertHtmlDomEquals('', $this->view->selectTag('people', '', array('multiple' => true))); - $this->assertDomEquals('', + $this->assertHtmlDomEquals('', $this->view->selectTag('people', '', array('multiple' => null))); } public function testSubmitTag() { - $expected = ''; + $expected = ''; $actual = $this->view->submitTag('Save', array('disableWith' => 'Saving...', 'onclick' => "alert('hello!')")); - $this->assertDomEquals($expected, $actual); + $this->assertHtmlDomEquals($expected, $actual); } } diff --git a/framework/View/test/Horde/View/Helper/FormTest.php b/framework/View/test/Horde/View/Helper/FormTest.php index 7e5ccdd36..47cea7868 100644 --- a/framework/View/test/Horde/View/Helper/FormTest.php +++ b/framework/View/test/Horde/View/Helper/FormTest.php @@ -48,15 +48,15 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case public function testTextField() { $this->assertEquals( - '', + '', $this->view->textField('post', 'title')); $this->assertEquals( - '', + '', $this->view->passwordField('post', 'title')); $this->assertEquals( - '', + '', $this->view->passwordField("person", "name")); } @@ -64,26 +64,26 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case { $this->post->title = 'Hello World'; $this->assertEquals( - '', + '', $this->view->textField('post', 'title')); } public function testTextFieldWithOptions() { - $expected = ''; + $expected = ''; $this->assertEquals($expected, $this->view->textField('post', 'title', array('size' => 35))); } public function testTextFieldAssumingSize() { - $expected = ''; + $expected = ''; $this->assertEquals($expected, $this->view->textField('post', 'title', array('maxlength' => 35))); } public function testTextFieldDoesntChangeParamValues() { $objectName = 'post[]'; - $expected = ''; + $expected = ''; $this->assertEquals($expected, $this->view->textField($objectName, 'title')); $this->assertEquals($objectName, 'post[]'); } @@ -91,23 +91,23 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case public function testCheckBox() { $this->assertEquals( - '', + '', $this->view->checkBox('post', 'secret')); $this->post->secret = 0; $this->assertEquals( - '', + '', $this->view->checkBox('post', 'secret')); $this->assertEquals( - '', - $this->view->checkBox('post', 'secret', array('checked' => 'checked'))); + '', + $this->view->checkBox('post', 'secret', array('checked' => true))); $this->post->secret = true; $this->assertEquals( - '', + '', $this->view->checkBox('post', 'secret')); } @@ -116,32 +116,32 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $this->post->secret = 'on'; $this->assertEquals( - '', + '', $this->view->checkBox('post', 'secret', array(), 'on', 'off')); } public function testRadioButton() { $this->assertEquals( - '', + '', $this->view->radioButton('post', 'title', 'Hello World')); $this->assertEquals( - '', + '', $this->view->radioButton('post', 'title', 'Goodbye World')); } public function testRadioButtonIsCheckedWithIntegers() { $this->assertEquals( - '', + '', $this->view->radioButton('post', 'secret', '1')); } public function testRadioButtonRespectsPassedInId() { $this->assertEquals( - '', + '', $this->view->radioButton('post', 'secret', '1', array('id' => 'foo'))); } @@ -177,7 +177,7 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case public function testExplicitName() { $this->assertEquals( - '', + '', $this->view->textField("post", "title", array("name" => "dont guess"))); $this->assertEquals( @@ -185,14 +185,14 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $this->view->textArea("post", "body", array("name" => "really!"))); $this->assertEquals( - '', + '', $this->view->checkBox("post", "secret", array("name" => "i mean it"))); } public function testExplicitId() { $this->assertEquals( - '', + '', $this->view->textField("post", "title", array("id" => "dont guess"))); $this->assertEquals( @@ -200,7 +200,7 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $this->view->textArea("post", "body", array("id" => "really!"))); $this->assertEquals( - '', + '', $this->view->checkBox("post", "secret", array("id" => "i mean it"))); } @@ -209,7 +209,7 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $pid = $this->post->id; $this->assertEquals( - "", + "", $this->view->textField("post[]", "title")); $this->assertEquals( @@ -217,15 +217,15 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $this->view->textArea("post[]", "body")); $this->assertEquals( - "", + "", $this->view->checkBox('post[]', 'secret')); $this->assertEquals( - "", + "", $this->view->radioButton('post[]', 'title', 'Hello World')); $this->assertEquals( - "", + "", $this->view->radioButton('post[]', 'title', 'Goodbye World')); } @@ -241,11 +241,11 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '' . - '' . + '' . '' . - '' . - '' . - '' . + '' . + '' . + '' . "
"; $this->assertEquals($expected, ob_get_clean()); @@ -263,11 +263,11 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '
' . - '' . + '
' . + '' . '' . - '' . - '' . + '' . + '' . "
"; $this->assertEquals($expected, ob_get_clean()); @@ -284,10 +284,10 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '' . + '' . '' . - '' . - '' . + '' . + '' . "
"; $this->assertEquals($expected, ob_get_clean()); @@ -304,10 +304,10 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '' . + '' . '' . - '' . - '' . + '' . + '' . '
'; $this->assertEquals($expected, ob_get_clean()); @@ -323,10 +323,10 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $fields->end(); $expected = - '' . + '' . '' . - '' . - ''; + '' . + ''; $this->assertEquals($expected, ob_get_clean()); } @@ -342,7 +342,7 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '' . + '' . '
'; $this->assertEquals($expected, ob_get_clean()); @@ -358,10 +358,10 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $fields->end(); $expected = - '' . + '' . '' . - '' . - ''; + '' . + ''; $this->assertEquals($expected, ob_get_clean()); } @@ -374,7 +374,7 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $fields->end(); $this->assertEquals( - '', + '', ob_get_clean()); } @@ -398,10 +398,10 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '' . + '' . '' . - '' . - '' . + '' . + '' . '
'; $this->assertEquals($expected, ob_get_clean()); @@ -417,8 +417,8 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '' . - ''; + '' . + ''; $this->assertEquals($expected, ob_get_clean()); } @@ -437,8 +437,8 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '' . - ''; + '' . + ''; $this->assertEquals($expected, ob_get_clean()); } catch (Exception $e) {} @@ -458,7 +458,7 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $fields->end(); $this->assertEquals( - '', + '', ob_get_clean()); } @@ -483,7 +483,7 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '' . + '' . '
'; $this->assertEquals($expected, ob_get_clean()); @@ -498,7 +498,7 @@ class Horde_View_Helper_FormTest extends Horde_Test_Case $expected = '
' . - '' . + '' . '
'; $this->assertEquals($expected, ob_get_clean()); @@ -521,6 +521,6 @@ class Horde_View_Helper_FormTest_BuilderMock extends Horde_View_Helper_Form_Buil { public function foo() { - return ''; + return ''; } } diff --git a/framework/View/test/Horde/View/Helper/TagTest.php b/framework/View/test/Horde/View/Helper/TagTest.php index 098f44e00..e2fe0ff4e 100644 --- a/framework/View/test/Horde/View/Helper/TagTest.php +++ b/framework/View/test/Horde/View/Helper/TagTest.php @@ -32,35 +32,33 @@ class Horde_View_Helper_TagTest extends Horde_Test_Case public function testTag() { - $this->assertEquals('
', $this->view->tag('br')); - $this->assertEquals('
', + $this->assertEquals('
', $this->view->tag('br')); + $this->assertEquals('
', $this->view->tag('br', array('clear' => 'left'))); - $this->assertEquals('
', - $this->view->tag('br', null, true)); } public function testTagOptions() { - $this->assertRegExp('/\A

\z/', + $this->assertRegExp('/\A

\z/', $this->view->tag('p', array('class' => 'show', 'class' => 'elsewhere'))); } public function testTagOptionsRejectsNullOption() { - $this->assertEquals('

', + $this->assertEquals('

', $this->view->tag('p', array('ignored' => null))); } public function testTagOptionsAcceptsBlankOption() { - $this->assertEquals('

', + $this->assertEquals('

', $this->view->tag('p', array('included' => ''))); } public function testTagOptionsConvertsBooleanOption() { - $this->assertEquals('

', + $this->assertEquals('

', $this->view->tag('p', array('disabled' => true, 'multiple' => true, 'readonly' => true))); @@ -86,7 +84,7 @@ class Horde_View_Helper_TagTest extends Horde_Test_Case { $attributes = array('1&2', '1 < 2', '“test“'); foreach ($attributes as $escaped) { - $this->assertEquals("", + $this->assertEquals("", $this->view->tag('a', array('href' => $escaped))); } } @@ -95,7 +93,7 @@ class Horde_View_Helper_TagTest extends Horde_Test_Case { $attributes = array('&1;', 'dfa3;', '& #123;'); foreach ($attributes as $escaped) { - $this->assertEquals('', + $this->assertEquals('', $this->view->tag('a', array('href' => $escaped))); } } diff --git a/framework/View/test/Horde/View/Helper/UrlTest.php b/framework/View/test/Horde/View/Helper/UrlTest.php index b1d948972..3d2ff0a90 100644 --- a/framework/View/test/Horde/View/Helper/UrlTest.php +++ b/framework/View/test/Horde/View/Helper/UrlTest.php @@ -53,8 +53,8 @@ class Horde_View_Helper_UrlTest extends Horde_Test_Case public function testLinkTagWithImg() { - $this->assertEquals("", - $this->view->linkTo("", "http://www.example.com")); + $this->assertEquals("", + $this->view->linkTo("", "http://www.example.com")); } public function testLinkToUnless() @@ -102,8 +102,8 @@ class Horde_View_Helper_UrlTest extends Horde_Test_Case public function testMailToWithImg() { - $this->assertEquals('', - $this->view->mailTo('feedback@example.com', '')); + $this->assertEquals('', + $this->view->mailTo('feedback@example.com', '')); } public function testMailToWithHex()