Fix undefined variable $header in HttpError
[lhc/web/wiklou.git] / includes / Html.php
index 6bd661f..6907245 100644 (file)
@@ -109,7 +109,7 @@ class Html {
         * @see https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
         * @return array $attrs A modified attribute array
         */
-       public static function buttonAttributes( $attrs, $modifiers = array() ) {
+       public static function buttonAttributes( array $attrs, array $modifiers = array() ) {
                global $wgUseMediaWikiUIEverywhere;
                if ( $wgUseMediaWikiUIEverywhere ) {
                        if ( isset( $attrs['class'] ) ) {
@@ -137,11 +137,8 @@ class Html {
         * @param array $attrs An attribute array.
         * @return array $attrs A modified attribute array
         */
-       public static function getTextInputAttributes( $attrs ) {
+       public static function getTextInputAttributes( array $attrs ) {
                global $wgUseMediaWikiUIEverywhere;
-               if ( !$attrs ) {
-                       $attrs = array();
-               }
                if ( $wgUseMediaWikiUIEverywhere ) {
                        if ( isset( $attrs['class'] ) ) {
                                if ( is_array( $attrs['class'] ) ) {
@@ -169,8 +166,8 @@ class Html {
         * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
         * @return string Raw HTML
         */
-       public static function linkButton( $contents, $attrs, $modifiers = array() ) {
-               return Html::element( 'a',
+       public static function linkButton( $contents, array $attrs, array $modifiers = array() ) {
+               return self::element( 'a',
                        self::buttonAttributes( $attrs, $modifiers ),
                        $contents
                );
@@ -189,10 +186,10 @@ class Html {
         * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
         * @return string Raw HTML
         */
-       public static function submitButton( $contents, $attrs, $modifiers = array() ) {
+       public static function submitButton( $contents, array $attrs, array $modifiers = array() ) {
                $attrs['type'] = 'submit';
                $attrs['value'] = $contents;
-               return Html::element( 'input', self::buttonAttributes( $attrs, $modifiers ) );
+               return self::element( 'input', self::buttonAttributes( $attrs, $modifiers ) );
        }
 
        /**
@@ -337,8 +334,7 @@ class Html {
         *   further documentation.
         * @return array An array of attributes functionally identical to $attribs
         */
-       private static function dropDefaults( $element, $attribs ) {
-
+       private static function dropDefaults( $element, array $attribs ) {
                // Whenever altering this array, please provide a covering test case
                // in HtmlTest::provideElementsWithAttributesHavingDefaultValues
                static $attribDefaults = array(
@@ -485,11 +481,10 @@ class Html {
         * @return string HTML fragment that goes between element name and '>'
         *   (starting with a space if at least one attribute is output)
         */
-       public static function expandAttributes( $attribs ) {
+       public static function expandAttributes( array $attribs ) {
                global $wgWellFormedXml;
 
                $ret = '';
-               $attribs = (array)$attribs;
                foreach ( $attribs as $key => $value ) {
                        // Support intuitive array( 'checked' => true/false ) form
                        if ( $value === false || is_null( $value ) ) {
@@ -600,17 +595,20 @@ class Html {
                        } else {
                                // Apparently we need to entity-encode \n, \r, \t, although the
                                // spec doesn't mention that.  Since we're doing strtr() anyway,
-                               // and we don't need <> escaped here, we may as well not call
-                               // htmlspecialchars().
+                               // we may as well not call htmlspecialchars().
                                // @todo FIXME: Verify that we actually need to
                                // escape \n\r\t here, and explain why, exactly.
                                #
                                // We could call Sanitizer::encodeAttribute() for this, but we
                                // don't because we're stubborn and like our marginal savings on
                                // byte size from not having to encode unnecessary quotes.
+                               // The only difference between this transform and the one by
+                               // Sanitizer::encodeAttribute() is '<' is only encoded here if
+                               // $wgWellFormedXml is set, and ' is not encoded.
                                $map = array(
                                        '&' => '&amp;',
                                        '"' => '&quot;',
+                                       '>' => '&gt;',
                                        "\n" => '&#10;',
                                        "\r" => '&#13;',
                                        "\t" => '&#9;'
@@ -711,12 +709,12 @@ class Html {
         *   attributes, passed to Html::element()
         * @return string Raw HTML
         */
-       public static function input( $name, $value = '', $type = 'text', $attribs = array() ) {
+       public static function input( $name, $value = '', $type = 'text', array $attribs = array() ) {
                $attribs['type'] = $type;
                $attribs['value'] = $value;
                $attribs['name'] = $name;
                if ( in_array( $type, array( 'text', 'search', 'email', 'password', 'number' ) ) ) {
-                       $attribs = Html::getTextInputAttributes( $attribs );
+                       $attribs = self::getTextInputAttributes( $attribs );
                }
                return self::element( 'input', $attribs );
        }
@@ -791,7 +789,7 @@ class Html {
         *   attributes, passed to Html::element()
         * @return string Raw HTML
         */
-       public static function hidden( $name, $value, $attribs = array() ) {
+       public static function hidden( $name, $value, array $attribs = array() ) {
                return self::input( $name, $value, 'hidden', $attribs );
        }
 
@@ -807,7 +805,7 @@ class Html {
         *   attributes, passed to Html::element()
         * @return string Raw HTML
         */
-       public static function textarea( $name, $value = '', $attribs = array() ) {
+       public static function textarea( $name, $value = '', array $attribs = array() ) {
                $attribs['name'] = $name;
 
                if ( substr( $value, 0, 1 ) == "\n" ) {
@@ -819,7 +817,7 @@ class Html {
                } else {
                        $spacedValue = $value;
                }
-               return self::element( 'textarea', Html::getTextInputAttributes( $attribs ), $spacedValue );
+               return self::element( 'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
        }
 
        /**
@@ -890,7 +888,7 @@ class Html {
                        } elseif ( is_int( $nsId ) ) {
                                $nsName = $wgContLang->convertNamespace( $nsId );
                        }
-                       $optionsHtml[] = Html::element(
+                       $optionsHtml[] = self::element(
                                'option', array(
                                        'disabled' => in_array( $nsId, $params['disable'] ),
                                        'value' => $nsId,
@@ -909,7 +907,7 @@ class Html {
 
                $ret = '';
                if ( isset( $params['label'] ) ) {
-                       $ret .= Html::element(
+                       $ret .= self::element(
                                'label', array(
                                        'for' => isset( $selectAttribs['id'] ) ? $selectAttribs['id'] : null,
                                ), $params['label']
@@ -917,11 +915,11 @@ class Html {
                }
 
                // Wrap options in a <select>
-               $ret .= Html::openElement( 'select', $selectAttribs )
+               $ret .= self::openElement( 'select', $selectAttribs )
                        . "\n"
                        . implode( "\n", $optionsHtml )
                        . "\n"
-                       . Html::closeElement( 'select' );
+                       . self::closeElement( 'select' );
 
                return $ret;
        }
@@ -934,7 +932,7 @@ class Html {
         *   attributes, passed to Html::element() of html tag.
         * @return string Raw HTML
         */
-       public static function htmlHeader( $attribs = array() ) {
+       public static function htmlHeader( array $attribs = array() ) {
                $ret = '';
 
                global $wgHtml5Version, $wgMimeType, $wgXhtmlNamespaces;
@@ -962,7 +960,7 @@ class Html {
                        $attribs['version'] = $wgHtml5Version;
                }
 
-               $html = Html::openElement( 'html', $attribs );
+               $html = self::openElement( 'html', $attribs );
 
                if ( $html ) {
                        $html .= "\n";
@@ -998,43 +996,57 @@ class Html {
         * @return string
         */
        static function infoBox( $text, $icon, $alt, $class = '' ) {
-               $s = Html::openElement( 'div', array( 'class' => "mw-infobox $class" ) );
+               $s = self::openElement( 'div', array( 'class' => "mw-infobox $class" ) );
 
-               $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ) .
-                               Html::element( 'img',
+               $s .= self::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ) .
+                               self::element( 'img',
                                        array(
                                                'src' => $icon,
                                                'alt' => $alt,
                                        )
                                ) .
-                               Html::closeElement( 'div' );
+                               self::closeElement( 'div' );
 
-               $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ) .
+               $s .= self::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ) .
                                $text .
-                               Html::closeElement( 'div' );
-               $s .= Html::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
+                               self::closeElement( 'div' );
+               $s .= self::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
 
-               $s .= Html::closeElement( 'div' );
+               $s .= self::closeElement( 'div' );
 
-               $s .= Html::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
+               $s .= self::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
 
                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.
+        * Generate a srcset attribute value.
+        *
+        * Generates a srcset attribute value from an array mapping pixel densities
+        * to URLs. A trailing 'x' in pixel density values is optional.
+        *
+        * @note srcset width and height values are not supported.
+        *
+        * @see http://www.whatwg.org/html/embedded-content-1.html#attr-img-srcset
+        *
+        * @par Example:
+        * @code
+        *     Html::srcSet( array(
+        *         '1x'   => 'standard.jpeg',
+        *         '1.5x' => 'large.jpeg',
+        *         '3x'   => 'extra-large.jpeg',
+        *     ) );
+        *     // gives 'standard.jpeg 1x, large.jpeg 1.5x, extra-large.jpeg 2x'
+        * @endcode
         *
         * @param string[] $urls
         * @return string
         */
-       static function srcSet( $urls ) {
+       static function srcSet( array $urls ) {
                $candidates = array();
                foreach ( $urls as $density => $url ) {
-                       // Image candidate syntax per current whatwg live spec, 2012-09-23:
-                       // http://www.whatwg.org/html/embedded-content-1.html#attr-img-srcset
-                       $candidates[] = "{$url} {$density}x";
+                       // Cast density to float to strip 'x'.
+                       $candidates[] = $url . ' ' . (float)$density . 'x';
                }
                return implode( ", ", $candidates );
        }