From c1f9b1f7b1b77776192048005dcc66dcf3df2bfb Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sat, 27 Dec 2014 15:41:37 +0100 Subject: Update to MediaWiki 1.24.1 --- includes/Html.php | 226 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 151 insertions(+), 75 deletions(-) (limited to 'includes/Html.php') diff --git a/includes/Html.php b/includes/Html.php index 932f753e..1e16e394 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -36,7 +36,7 @@ * * There are two important configuration options this class uses: * - * $wgMimeType: If this is set to an xml mimetype then output should be + * $wgMimeType: If this is set to an xml MIME type then output should be * valid XHTML5. * $wgWellFormedXml: If this is set to true, then all output should be * well-formed XML (quotes on attributes, self-closing tags, etc.). @@ -101,6 +101,35 @@ class Html { 'itemscope', ); + /** + * Modifies a set of attributes meant for text input elements + * and apply a set of default attributes. + * Removes size attribute when $wgUseMediaWikiUIEverywhere enabled. + * @param array $attrs An attribute array. + * @return array $attrs A modified attribute array + */ + public static function getTextInputAttributes( $attrs ) { + global $wgUseMediaWikiUIEverywhere; + if ( !$attrs ) { + $attrs = array(); + } + if ( isset( $attrs['class'] ) ) { + if ( is_array( $attrs['class'] ) ) { + $attrs['class'][] = 'mw-ui-input'; + } else { + $attrs['class'] .= ' mw-ui-input'; + } + } else { + $attrs['class'] = 'mw-ui-input'; + } + if ( $wgUseMediaWikiUIEverywhere ) { + // Note that size can effect the desired width rendering of mw-ui-input elements + // so it is removed. Left intact when mediawiki ui not enabled. + unset( $attrs['size'] ); + } + return $attrs; + } + /** * Returns an HTML element in a string. The major advantage here over * manually typing out the HTML is that it will escape all attribute @@ -114,7 +143,7 @@ class Html { * shaved off the HTML output as well. * * @param string $element The element's name, e.g., 'a' - * @param array $attribs Associative array of attributes, e.g., array( + * @param array $attribs Associative array of attributes, e.g., array( * 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for * further documentation. * @param string $contents The raw HTML contents of the element: *not* @@ -139,9 +168,9 @@ class Html { * Identical to rawElement(), but HTML-escapes $contents (like * Xml::element()). * - * @param $element string - * @param $attribs array - * @param $contents string + * @param string $element + * @param array $attribs + * @param string $contents * * @return string */ @@ -158,8 +187,8 @@ class Html { * Identical to rawElement(), but has no third parameter and omits the end * tag (and the self-closing '/' in XML mode for empty elements). * - * @param $element string - * @param $attribs array + * @param string $element + * @param array $attribs * * @return string */ @@ -225,33 +254,15 @@ class Html { } /** - * Returns "", except if $wgWellFormedXml is off, in which case - * it returns the empty string when that's guaranteed to be safe. + * Returns "" * * @since 1.17 * @param string $element Name of the element, e.g., 'a' - * @return string A closing tag, if required + * @return string A closing tag */ public static function closeElement( $element ) { - global $wgWellFormedXml; - $element = strtolower( $element ); - // Reference: - // http://www.whatwg.org/html/syntax.html#optional-tags - if ( !$wgWellFormedXml && in_array( $element, array( - 'html', - 'head', - 'body', - 'li', - 'dt', - 'dd', - 'tr', - 'td', - 'th', - ) ) ) { - return ''; - } return ""; } @@ -267,7 +278,7 @@ class Html { * to the input array (currently per the HTML 5 draft as of 2009-09-06). * * @param string $element Name of the element, e.g., 'a' - * @param array $attribs Associative array of attributes, e.g., array( + * @param array $attribs Associative array of attributes, e.g., array( * 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for * further documentation. * @return array An array of attributes functionally identical to $attribs @@ -386,7 +397,7 @@ class Html { * For instance, it will omit quotation marks if $wgWellFormedXml is false, * and will treat boolean attributes specially. * - * Attributes that should contain space-separated lists (such as 'class') array + * Attributes that can contain space-separated lists ('class', 'accesskey' and 'rel') array * values are allowed as well, which will automagically be normalized * and converted to a space-separated string. In addition to a numerical * array, the attribute value may also be an associative array. See the @@ -413,6 +424,8 @@ class Html { * A value of false means to omit the attribute. For boolean attributes, * you can omit the key, e.g., array( 'checked' ) instead of * array( 'checked' => 'checked' ) or such. + * + * @throws MWException If an attribute that doesn't allow lists is set to an array * @return string HTML fragment that goes between element name and '>' * (starting with a space if at least one attribute is output) */ @@ -449,8 +462,8 @@ class Html { // numbers to be entered in 'type="number"' fields, allow // the special case 'step="any"'. - if ( in_array( $key, array( 'max', 'min', 'pattern', 'required' ) ) || - $key === 'step' && $value !== 'any' ) { + if ( in_array( $key, array( 'max', 'min', 'pattern', 'required' ) ) + || $key === 'step' && $value !== 'any' ) { continue; } @@ -500,6 +513,8 @@ class Html { // Remove duplicates and create the string $value = implode( ' ', array_unique( $value ) ); + } elseif ( is_array( $value ) ) { + throw new MWException( "HTML attribute $key can not contain a list of values" ); } // See the "Attributes" section in the HTML syntax part of HTML5, @@ -583,7 +598,7 @@ class Html { * Output a "". * - * @param $url string + * @param string $url * @return string Raw HTML */ public static function linkedScript( $url ) { @@ -598,7 +613,7 @@ class Html { * contains literal "" (admittedly unlikely). * * @param string $contents CSS - * @param $media mixed A media type string, like 'screen' + * @param string $media A media type string, like 'screen' * @return string Raw HTML */ public static function inlineStyle( $contents, $media = 'all' ) { @@ -618,8 +633,8 @@ class Html { * Output a "" linking to the given URL for the given * media type (if any). * - * @param $url string - * @param $media mixed A media type string, like 'screen' + * @param string $url + * @param string $media A media type string, like 'screen' * @return string Raw HTML */ public static function linkedStyle( $url, $media = 'all' ) { @@ -635,10 +650,10 @@ class Html { * Convenience function to produce an "" element. This supports the * new HTML5 input types and attributes. * - * @param $name string name attribute - * @param $value mixed value attribute - * @param $type string type attribute - * @param array $attribs Associative array of miscellaneous extra + * @param string $name Name attribute + * @param array $value Value attribute + * @param string $type Type attribute + * @param array $attribs Associative array of miscellaneous extra * attributes, passed to Html::element() * @return string Raw HTML */ @@ -646,16 +661,79 @@ class Html { $attribs['type'] = $type; $attribs['value'] = $value; $attribs['name'] = $name; - + if ( in_array( $type, array( 'text', 'search', 'email', 'password', 'number' ) ) ) { + $attribs = Html::getTextInputAttributes( $attribs ); + } 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 + * @return string + */ + 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 + * @return string + */ + 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 + * @return string + */ + 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 * - * @param $name string name attribute - * @param $value string value attribute - * @param array $attribs Associative array of miscellaneous extra + * @param string $name Name attribute + * @param string $value Value attribute + * @param array $attribs Associative array of miscellaneous extra * attributes, passed to Html::element() * @return string Raw HTML */ @@ -664,14 +742,14 @@ class Html { } /** - * Convenience function to produce an "" element. + * Convenience function to produce a