CologneBlue rewrite: output the sidebar items as <ul> lists
[lhc/web/wiklou.git] / includes / Html.php
index d4d0203..5be67ab 100644 (file)
@@ -191,12 +191,8 @@ class Html {
                        return '';
                }
 
-               # Remove HTML5-only attributes if we aren't doing HTML5, and disable
-               # form validation regardless (see bug 23769 and the more detailed
-               # comment in expandAttributes())
+               # Remove invalid input types
                if ( $element == 'input' ) {
-                       # Whitelist of types that don't cause validation.  All except
-                       # 'search' are valid in XHTML1.
                        $validTypes = array(
                                'hidden',
                                'text',
@@ -208,18 +204,30 @@ class Html {
                                'image',
                                'reset',
                                'button',
-                               'search',
                        );
 
+                       # Allow more input types in HTML5 mode
+                       if( $wgHtml5 ) {
+                               $validTypes = array_merge( $validTypes, array(
+                                       'datetime',
+                                       'datetime-local',
+                                       'date',
+                                       'month',
+                                       'time',
+                                       'week',
+                                       'number',
+                                       'range',
+                                       'email',
+                                       'url',
+                                       'search',
+                                       'tel',
+                                       'color',
+                               ) );
+                       }
                        if ( isset( $attribs['type'] )
                        && !in_array( $attribs['type'], $validTypes ) ) {
                                unset( $attribs['type'] );
                        }
-
-                       if ( isset( $attribs['type'] ) && $attribs['type'] == 'search'
-                       && !$wgHtml5 ) {
-                               unset( $attribs['type'] );
-                       }
                }
 
                if ( !$wgHtml5 && $element == 'textarea' && isset( $attribs['maxlength'] ) ) {
@@ -286,6 +294,8 @@ class Html {
                        return $attribs;
                }
 
+               # Whenever altering this array, please provide a covering test case
+               # in HtmlTest::provideElementsWithAttributesHavingDefaultValues
                static $attribDefaults = array(
                        'area' => array( 'shape' => 'rect' ),
                        'button' => array(
@@ -306,7 +316,6 @@ class Html {
                        'input' => array(
                                'formaction' => 'GET',
                                'type' => 'text',
-                               'value' => '',
                        ),
                        'keygen' => array( 'keytype' => 'rsa' ),
                        'link' => array( 'media' => 'all' ),
@@ -347,6 +356,29 @@ class Html {
                && strval( $attribs['type'] ) == 'text/css' ) {
                        unset( $attribs['type'] );
                }
+               if ( $element === 'input' ) {
+                       $type = isset( $attribs['type'] ) ? $attribs['type'] : null;
+                       $value = isset( $attribs['value'] ) ? $attribs['value'] : null;
+                       if ( $type === 'checkbox' || $type === 'radio' ) {
+                               // The default value for checkboxes and radio buttons is 'on'
+                               // not ''. By stripping value="" we break radio boxes that
+                               // actually wants empty values.
+                               if ( $value === 'on' ) {
+                                       unset( $attribs['value'] );
+                               }
+                       } elseif ( $type === 'submit' ) {
+                               // The default value for submit appears to be "Submit" but
+                               // let's not bother stripping out localized text that matches
+                               // that.
+                       } else {
+                               // The default value for nearly every other field type is ''
+                               // The 'range' and 'color' types use different defaults but
+                               // stripping a value="" does not hurt them.
+                               if ( $value === '' ) {
+                                       unset( $attribs['value'] );
+                               }
+                       }
+               }
                if ( $element === 'select' && isset( $attribs['size'] ) ) {
                        if ( in_array( 'multiple', $attribs )
                                || ( isset( $attribs['multiple'] ) && $attribs['multiple'] !== false )
@@ -448,7 +480,7 @@ class Html {
                                'class', // html4, html5
                                'accesskey', // as of html5, multiple space-separated values allowed
                                // html4-spec doesn't document rel= as space-separated
-                               // but has been used like that and is now documented as such 
+                               // but has been used like that and is now documented as such
                                // in the html5-spec.
                                'rel',
                        );
@@ -461,7 +493,6 @@ class Html {
                                // values. Implode/explode to get those into the main array as well.
                                if ( is_array( $value ) ) {
                                        // If input wasn't an array, we can skip this step
-                                       
                                        $newValue = array();
                                        foreach ( $value as $k => $v ) {
                                                if ( is_string( $v ) ) {
@@ -544,7 +575,6 @@ class Html {
                                        # @todo FIXME: Is this really true?
                                        $map['<'] = '&lt;';
                                }
-                               
                                $ret .= " $key=$quote" . strtr( $value, $map ) . $quote;
                        }
                }
@@ -721,7 +751,7 @@ class Html {
         * - name: [optional], default: 'namespace'
         * @return string HTML code to select a namespace.
         */
-       public static function namespaceSelector( Array $params = array(), Array $selectAttribs = array() ) {
+       public static function namespaceSelector( array $params = array(), array $selectAttribs = array() ) {
                global $wgContLang;
 
                ksort( $selectAttribs );
@@ -764,10 +794,12 @@ class Html {
                        if ( $nsId < NS_MAIN || in_array( $nsId, $params['exclude'] ) ) {
                                continue;
                        }
-                       if ( $nsId === 0 ) {
+                       if ( $nsId === NS_MAIN ) {
                                // For other namespaces use use the namespace prefix as label, but for
                                // main we don't use "" but the user message descripting it (e.g. "(Main)" or "(Article)")
                                $nsName = wfMessage( 'blanknamespace' )->text();
+                       } elseif ( is_int( $nsId ) ) {
+                               $nsName = $wgContLang->convertNamespace( $nsId );
                        }
                        $optionsHtml[] = Html::element(
                                'option', array(
@@ -778,6 +810,14 @@ class Html {
                        );
                }
 
+               if ( !array_key_exists( 'id', $selectAttribs ) ) {
+                       $selectAttribs['id'] = 'namespace';
+               }
+
+               if ( !array_key_exists( 'name', $selectAttribs ) ) {
+                       $selectAttribs['name'] = 'namespace';
+               }
+
                $ret = '';
                if ( isset( $params['label'] ) ) {
                        $ret .= Html::element(
@@ -900,4 +940,22 @@ class Html {
 
                return $s;
        }
+
+       /**
+        * Generate a srcset attribute value from an array mapping pixel densities
+        * to URLs. Note that srcset supports width and height values as well, which
+        * are not used here.
+        *
+        * @param array $urls
+        * @return string
+        */
+       static function srcSet( $urls ) {
+               $candidates = array();
+               foreach( $urls as $density => $url ) {
+                       // Image candidate syntax per current whatwg live spec, 2012-09-23:
+                       // http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content-1.html#attr-img-srcset
+                       $candidates[] = "{$url} {$density}x";
+               }
+               return implode( ", ", $candidates );
+       }
 }