From: Aryeh Gregor Date: Sun, 21 Mar 2010 05:12:02 +0000 (+0000) Subject: Omit some start/end tags if not well-formed XML X-Git-Tag: 1.31.0-rc.0~37400 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=eefe1b13a382a7d11c2137bbf900b783ee445323;p=lhc%2Fweb%2Fwiklou.git Omit some start/end tags if not well-formed XML --- diff --git a/includes/Html.php b/includes/Html.php index 84acf31b63..b56a6d0c95 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -117,7 +117,7 @@ class Html { } return $start; } else { - return "$start$contents"; + return "$start$contents" . self::closeElement( $element ); } } @@ -136,15 +136,23 @@ 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). + * tag (and the self-closing '/' in XML mode for empty elements). */ public static function openElement( $element, $attribs = array() ) { - global $wgHtml5; + global $wgHtml5, $wgWellFormedXml; $attribs = (array)$attribs; # This is not required in HTML5, but let's do it anyway, for # consistency and better compression. $element = strtolower( $element ); + # In text/html, initial and tags can be omitted under + # pretty much any sane circumstances, if they have no attributes. See: + # + if ( !$wgWellFormedXml && !$attribs + && in_array( $element, array( 'html', 'head' ) ) ) { + return ''; + } + # Remove HTML5-only attributes if we aren't doing HTML5 if ( !$wgHtml5 ) { if ( $element == 'input' ) { @@ -192,6 +200,36 @@ class Html { self::dropDefaults( $element, $attribs ) ) . '>'; } + /** + * Returns "", except if $wgWellFormedXml is off, in which case + * it returns the empty string when that's guaranteed to be safe. + * + * @param $element string Name of the element, e.g., 'a' + * @return string A closing tag, if required + */ + public static function closeElement( $element ) { + global $wgWellFormedXml; + + $element = strtolower( $element ); + + # Reference: + # http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#optional-tags + if ( !$wgWellFormedXml && in_array( $element, array( + 'html', + 'head', + 'body', + 'li', + 'dt', + 'dd', + 'tr', + 'td', + 'th', + ) ) ) { + return ''; + } + return ""; + } + /** * Given an element name and an associative array of element attributes, * return an array that is functionally identical to the input array, but diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 8a80cc30ae..facd93fa7e 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2121,7 +2121,11 @@ class OutputPage { $ret .= "lang=\"$wgContLanguageCode\" dir=\"$dir\">\n"; } - $ret .= "\n"; + $openHead = Html::openElement( 'head' ); + if ( $openHead ) { + # Don't bother with the newline if $head == '' + $ret .= "$openHead\n"; + } $ret .= "" . htmlspecialchars( $this->getHTMLTitle() ) . "\n"; $ret .= implode( "\n", array( $this->getHeadLinks(), @@ -2137,7 +2141,10 @@ class OutputPage { $ret .= $this->getTitle()->trackbackRDF(); } - $ret .= "\n"; + $closeHead = Html::closeElement( 'head' ); + if ( $closeHead ) { + $ret .= "$closeHead\n"; + } $bodyAttrs = array(); diff --git a/skins/MonoBook.php b/skins/MonoBook.php index fdc1684de4..68aa2f9b28 100644 --- a/skins/MonoBook.php +++ b/skins/MonoBook.php @@ -204,10 +204,11 @@ if($this->data['copyrightico']) { ?> text( 'debug' ); ?> --> - - -