Move more <input> logic from input() to element()
authorAryeh Gregor <simetrical@users.mediawiki.org>
Sun, 6 Sep 2009 15:07:52 +0000 (15:07 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Sun, 6 Sep 2009 15:07:52 +0000 (15:07 +0000)
includes/Html.php

index 8d97bc4..a58459d 100644 (file)
@@ -83,6 +83,17 @@ class Html {
                'seamless',
        );
 
+       # A nested associative array of element => content attribute => default
+       # value.  Attributes that have the default value will be omitted, since
+       # they're pointless.  Currently the list hasn't been systematically
+       # populated.
+       private static $attribDefaults = array(
+               'input' => array(
+                       'value' => '',
+                       'type' => 'text',
+               ),
+       );
+
        /**
         * Returns an HTML element in a string.  The major advantage here over
         * manually typing out the HTML is that it will escape all attribute
@@ -153,6 +164,15 @@ class Html {
                        }
                }
 
+               # Don't bother outputting the default values for attributes
+               foreach ( $attribs as $attrib => $value ) {
+                       $lcattrib = strtolower( $attrib );
+                       if ( isset( self::$attribDefaults[$element][$lcattrib] ) &&
+                       self::$attribDefaults[$element][$lcattrib] === $value ) {
+                               unset( $attribs[$attrib] );
+                       }
+               }
+
                $start = "<$element" . self::expandAttributes( $attribs );
                if ( in_array( $element, self::$voidElements ) ) {
                        if ( $wgWellFormedXml ) {
@@ -352,10 +372,8 @@ class Html {
         * @return string Raw HTML
         */
        public static function input( $name, $value = null, $type = 'text', $attribs = array() ) {
-               if ( $type != 'text' ) {
-                       $attribs['type'] = $type;
-               }
-               if ( $value !== null && $value !== '' ) {
+               $attribs['type'] = $type;
+               if ( $value !== null ) {
                        $attribs['value'] = $value;
                }
                $attribs['name'] = $name;