diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2013-12-08 09:55:49 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2013-12-08 09:55:49 +0100 |
commit | 4ac9fa081a7c045f6a9f1cfc529d82423f485b2e (patch) | |
tree | af68743f2f4a47d13f2b0eb05f5c4aaf86d8ea37 /includes/Html.php | |
parent | af4da56f1ad4d3ef7b06557bae365da2ea27a897 (diff) |
Update to MediaWiki 1.22.0
Diffstat (limited to 'includes/Html.php')
-rw-r--r-- | includes/Html.php | 179 |
1 files changed, 59 insertions, 120 deletions
diff --git a/includes/Html.php b/includes/Html.php index af4b4bbf..932f753e 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -36,8 +36,8 @@ * * There are two important configuration options this class uses: * - * $wgHtml5: If this is set to false, then all output should be valid XHTML 1.0 - * Transitional. + * $wgMimeType: If this is set to an xml mimetype 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,19 +101,6 @@ class Html { 'itemscope', ); - private static $HTMLFiveOnlyAttribs = array( - 'autocomplete', - 'autofocus', - 'max', - 'min', - 'multiple', - 'pattern', - 'placeholder', - 'required', - 'step', - 'spellcheck', - ); - /** * Returns an HTML element in a string. The major advantage here over * manually typing out the HTML is that it will escape all attribute @@ -177,7 +164,7 @@ class Html { * @return string */ public static function openElement( $element, $attribs = array() ) { - global $wgHtml5, $wgWellFormedXml; + global $wgWellFormedXml; $attribs = (array)$attribs; // This is not required in HTML5, but let's do it anyway, for // consistency and better compression. @@ -185,7 +172,7 @@ class Html { // In text/html, initial <html> and <head> tags can be omitted under // pretty much any sane circumstances, if they have no attributes. See: - // <http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#optional-tags> + // <http://www.whatwg.org/html/syntax.html#optional-tags> if ( !$wgWellFormedXml && !$attribs && in_array( $element, array( 'html', 'head' ) ) ) { return ''; @@ -204,36 +191,28 @@ class Html { 'image', 'reset', 'button', - ); - // 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', - ) ); - } + // HTML input types + '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 ( !$wgHtml5 && $element == 'textarea' && isset( $attribs['maxlength'] ) ) { - unset( $attribs['maxlength'] ); - } - // According to standard the default type for <button> elements is "submit". // Depending on compatibility mode IE might use "button", instead. // We enforce the standard "submit". @@ -259,7 +238,7 @@ class Html { $element = strtolower( $element ); // Reference: - // http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#optional-tags + // http://www.whatwg.org/html/syntax.html#optional-tags if ( !$wgWellFormedXml && in_array( $element, array( 'html', 'head', @@ -294,12 +273,6 @@ class Html { * @return array An array of attributes functionally identical to $attribs */ private static function dropDefaults( $element, $attribs ) { - // Don't bother doing anything if we aren't outputting HTML5; it's too - // much of a pain to maintain two sets of defaults. - global $wgHtml5; - if ( !$wgHtml5 ) { - return $attribs; - } // Whenever altering this array, please provide a covering test case // in HtmlTest::provideElementsWithAttributesHavingDefaultValues @@ -340,7 +313,7 @@ class Html { foreach ( $attribs as $attrib => $value ) { $lcattrib = strtolower( $attrib ); - if( is_array( $value ) ) { + if ( is_array( $value ) ) { $value = implode( ' ', $value ); } else { $value = strval( $value ); @@ -444,11 +417,12 @@ class Html { * (starting with a space if at least one attribute is output) */ public static function expandAttributes( $attribs ) { - global $wgHtml5, $wgWellFormedXml; + global $wgWellFormedXml; $ret = ''; $attribs = (array)$attribs; foreach ( $attribs as $key => $value ) { + // Support intuitive array( 'checked' => true/false ) form if ( $value === false || is_null( $value ) ) { continue; } @@ -460,15 +434,10 @@ class Html { $key = $value; } - // Not technically required in HTML5, but required in XHTML 1.0, - // and we'd like consistency and better compression anyway. + // Not technically required in HTML5 but we'd like consistency + // and better compression anyway. $key = strtolower( $key ); - // Here we're blacklisting some HTML5-only attributes... - if ( !$wgHtml5 && in_array( $key, self::$HTMLFiveOnlyAttribs ) ) { - continue; - } - // Bug 23769: Blacklist all form validation attributes for now. Current // (June 2010) WebKit has no UI, so the form just refuses to submit // without telling the user why, which is much worse than failing @@ -552,15 +521,12 @@ class Html { } if ( in_array( $key, self::$boolAttribs ) ) { - // In XHTML 1.0 Transitional, the value needs to be equal to the - // key. In HTML5, we can leave the value empty instead. If we - // don't need well-formed XML, we can omit the = entirely. + // In HTML5, we can leave the value empty. If we don't need + // well-formed XML, we can omit the = entirely. if ( !$wgWellFormedXml ) { $ret .= " $key"; - } elseif ( $wgHtml5 ) { - $ret .= " $key=\"\""; } else { - $ret .= " $key=\"$key\""; + $ret .= " $key=\"\""; } } else { // Apparently we need to entity-encode \n, \r, \t, although the @@ -602,14 +568,10 @@ class Html { * @return string Raw HTML */ public static function inlineScript( $contents ) { - global $wgHtml5, $wgJsMimeType, $wgWellFormedXml; + global $wgWellFormedXml; $attrs = array(); - if ( !$wgHtml5 ) { - $attrs['type'] = $wgJsMimeType; - } - if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) { $contents = "/*<![CDATA[*/$contents/*]]>*/"; } @@ -625,14 +587,8 @@ class Html { * @return string Raw HTML */ public static function linkedScript( $url ) { - global $wgHtml5, $wgJsMimeType; - $attrs = array( 'src' => $url ); - if ( !$wgHtml5 ) { - $attrs['type'] = $wgJsMimeType; - } - return self::element( 'script', $attrs ); } @@ -677,8 +633,7 @@ class Html { /** * Convenience function to produce an "<input>" element. This supports the - * new HTML5 input types and attributes, and will silently strip them if - * $wgHtml5 is false. + * new HTML5 input types and attributes. * * @param $name string name attribute * @param $value mixed value attribute @@ -712,9 +667,7 @@ class Html { * Convenience function to produce an "<input>" element. * * This supports leaving out the cols= and rows= which Xml requires and are - * required by HTML4/XHTML but not required by HTML5 and will silently set - * cols="" and rows="" if $wgHtml5 is false and cols and rows are omitted - * (HTML4 validates present but empty cols="" and rows="" as valid). + * required by HTML4/XHTML but not required by HTML5. * * @param $name string name attribute * @param $value string value attribute @@ -723,20 +676,8 @@ class Html { * @return string Raw HTML */ public static function textarea( $name, $value = '', $attribs = array() ) { - global $wgHtml5; - $attribs['name'] = $name; - if ( !$wgHtml5 ) { - if ( !isset( $attribs['cols'] ) ) { - $attribs['cols'] = ""; - } - - if ( !isset( $attribs['rows'] ) ) { - $attribs['rows'] = ""; - } - } - if ( substr( $value, 0, 1 ) == "\n" ) { // Workaround for bug 12130: browsers eat the initial newline // assuming that it's just for show, but they do keep the later @@ -859,28 +800,29 @@ class Html { public static function htmlHeader( $attribs = array() ) { $ret = ''; - global $wgMimeType; - - if ( self::isXmlMimeType( $wgMimeType ) ) { - $ret .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n"; - } + global $wgHtml5Version, $wgMimeType, $wgXhtmlNamespaces; - global $wgHtml5, $wgHtml5Version, $wgDocType, $wgDTD; - global $wgXhtmlNamespaces, $wgXhtmlDefaultNamespace; + $isXHTML = self::isXmlMimeType( $wgMimeType ); - if ( $wgHtml5 ) { - $ret .= "<!DOCTYPE html>\n"; + if ( $isXHTML ) { // XHTML5 + // XML mimetyped markup should have an xml header. + // However a DOCTYPE is not needed. + $ret .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n"; - if ( $wgHtml5Version ) { - $attribs['version'] = $wgHtml5Version; - } - } else { - $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\" \"$wgDTD\">\n"; - $attribs['xmlns'] = $wgXhtmlDefaultNamespace; + // Add the standard xmlns + $attribs['xmlns'] = 'http://www.w3.org/1999/xhtml'; + // And support custom namespaces foreach ( $wgXhtmlNamespaces as $tag => $ns ) { $attribs["xmlns:$tag"] = $ns; } + } else { // HTML5 + // DOCTYPE + $ret .= "<!DOCTYPE html>\n"; + } + + if ( $wgHtml5Version ) { + $attribs['version'] = $wgHtml5Version; } $html = Html::openElement( 'html', $attribs ); @@ -901,14 +843,11 @@ class Html { * @return Boolean */ public static function isXmlMimeType( $mimetype ) { - switch ( $mimetype ) { - case 'text/xml': - case 'application/xhtml+xml': - case 'application/xml': - return true; - default: - return false; - } + # http://www.whatwg.org/html/infrastructure.html#xml-mime-type + # * text/xml + # * application/xml + # * Any mimetype with a subtype ending in +xml (this implicitly includes application/xhtml+xml) + return (bool)preg_match( '!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype ); } /** @@ -926,22 +865,22 @@ class Html { global $wgStylePath; if ( $useStylePath ) { - $icon = $wgStylePath.'/common/images/'.$icon; + $icon = $wgStylePath . '/common/images/' . $icon; } $s = Html::openElement( 'div', array( 'class' => "mw-infobox $class" ) ); - $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ). + $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ) . Html::element( 'img', array( 'src' => $icon, 'alt' => $alt, ) - ). + ) . Html::closeElement( 'div' ); - $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ). - $text. + $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ) . + $text . Html::closeElement( 'div' ); $s .= Html::element( 'div', array( 'style' => 'clear: left;' ), ' ' ); @@ -962,9 +901,9 @@ class Html { */ static function srcSet( $urls ) { $candidates = array(); - foreach( $urls as $density => $url ) { + 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 + // http://www.whatwg.org/html/embedded-content-1.html#attr-img-srcset $candidates[] = "{$url} {$density}x"; } return implode( ", ", $candidates ); |