From 5edf25e2e69baab396e572601977d15ad7a83305 Mon Sep 17 00:00:00 2001 From: Tyler Anthony Romeo Date: Thu, 7 Mar 2013 14:03:44 -0500 Subject: [PATCH] Added Html::radio, Html::check, and Html::label. Migrated some convenience functions from the Xml class to Html so they can be used. Only added functions that acted as more than just a wrapper for another function. Change-Id: I4cc5876d4be6e04ec554444242cc049a3cff0f58 --- includes/Html.php | 58 +++++++++++++++++++++++++++++ tests/phpunit/includes/HtmlTest.php | 57 ++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/includes/Html.php b/includes/Html.php index a8dbd61552..ce439cb3ee 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -654,6 +654,64 @@ class Html { return self::element( 'input', $attribs ); } + /** + * Convenience function to produce a checkbox (input element with type=checkbox) + * + * @param string $name Name attribute + * @param bool $checked Whether the checkbox is checked or not + * @param array $attribs Array of additional attributes + */ + public static function check( $name, $checked = false, array $attribs = array() ) { + if ( isset( $attribs['value'] ) ) { + $value = $attribs['value']; + unset( $attribs['value'] ); + } else { + $value = 1; + } + + if ( $checked ) { + $attribs[] = 'checked'; + } + + return self::input( $name, $value, 'checkbox', $attribs ); + } + + /** + * Convenience function to produce a checkbox (input element with type=checkbox) + * + * @param string $name Name attribute + * @param bool $checked Whether the checkbox is checked or not + * @param array $attribs Array of additional attributes + */ + public static function radio( $name, $checked = false, array $attribs = array() ) { + if ( isset( $attribs['value'] ) ) { + $value = $attribs['value']; + unset( $attribs['value'] ); + } else { + $value = 1; + } + + if ( $checked ) { + $attribs[] = 'checked'; + } + + return self::input( $name, $value, 'radio', $attribs ); + } + + /** + * Convenience function for generating a label for inputs. + * + * @param string $label Contents of the label + * @param string $id ID of the element being labeled + * @param array $attribs Additional attributes + */ + public static function label( $label, $id, array $attribs = array() ) { + $attribs += array( + 'for' => $id + ); + return self::element( 'label', $attribs, $label ); + } + /** * Convenience function to produce an input element with type=hidden * diff --git a/tests/phpunit/includes/HtmlTest.php b/tests/phpunit/includes/HtmlTest.php index c561e70fd7..d4d955131c 100644 --- a/tests/phpunit/includes/HtmlTest.php +++ b/tests/phpunit/includes/HtmlTest.php @@ -707,6 +707,63 @@ class HtmlTest extends MediaWikiTestCase { ) ); } + + public function testWrapperInput() { + $this->assertEquals( + '', + Html::input( 'testname', 'testval', 'radio' ), + 'Input wrapper with type and value.' + ); + $this->assertEquals( + '', + Html::input( 'testname' ), + 'Input wrapper with all default values.' + ); + } + + public function testWrapperCheck() { + $this->assertEquals( + '', + Html::check( 'testname' ), + 'Checkbox wrapper unchecked.' + ); + $this->assertEquals( + '', + Html::check( 'testname', true ), + 'Checkbox wrapper checked.' + ); + $this->assertEquals( + '', + Html::check( 'testname', false, array( 'value' => 'testval' ) ), + 'Checkbox wrapper with a value override.' + ); + } + + public function testWrapperRadio() { + $this->assertEquals( + '', + Html::radio( 'testname' ), + 'Radio wrapper unchecked.' + ); + $this->assertEquals( + '', + Html::radio( 'testname', true ), + 'Radio wrapper checked.' + ); + $this->assertEquals( + '', + Html::radio( 'testname', false, array( 'value' => 'testval' ) ), + 'Radio wrapper with a value override.' + ); + } + + public function testWrapperLabel() { + $this->assertEquals( + '', + Html::label( 'testlabel', 'testid' ), + 'Label wrapper' + ); + } } class HtmlTestValue { -- 2.20.1