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
*
)
);
}
+
+ public function testWrapperInput() {
+ $this->assertEquals(
+ '<input type=radio value=testval name=testname>',
+ Html::input( 'testname', 'testval', 'radio' ),
+ 'Input wrapper with type and value.'
+ );
+ $this->assertEquals(
+ '<input name=testname>',
+ Html::input( 'testname' ),
+ 'Input wrapper with all default values.'
+ );
+ }
+
+ public function testWrapperCheck() {
+ $this->assertEquals(
+ '<input type=checkbox value=1 name=testname>',
+ Html::check( 'testname' ),
+ 'Checkbox wrapper unchecked.'
+ );
+ $this->assertEquals(
+ '<input checked type=checkbox value=1 name=testname>',
+ Html::check( 'testname', true ),
+ 'Checkbox wrapper checked.'
+ );
+ $this->assertEquals(
+ '<input type=checkbox value=testval name=testname>',
+ Html::check( 'testname', false, array( 'value' => 'testval' ) ),
+ 'Checkbox wrapper with a value override.'
+ );
+ }
+
+ public function testWrapperRadio() {
+ $this->assertEquals(
+ '<input type=radio value=1 name=testname>',
+ Html::radio( 'testname' ),
+ 'Radio wrapper unchecked.'
+ );
+ $this->assertEquals(
+ '<input checked type=radio value=1 name=testname>',
+ Html::radio( 'testname', true ),
+ 'Radio wrapper checked.'
+ );
+ $this->assertEquals(
+ '<input type=radio value=testval name=testname>',
+ Html::radio( 'testname', false, array( 'value' => 'testval' ) ),
+ 'Radio wrapper with a value override.'
+ );
+ }
+
+ public function testWrapperLabel() {
+ $this->assertEquals(
+ '<label for=testid>testlabel</label>',
+ Html::label( 'testlabel', 'testid' ),
+ 'Label wrapper'
+ );
+ }
}
class HtmlTestValue {