*
* 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.).
*
'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
* @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.
// 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 '';
'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".
$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',
* @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
* (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;
$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
}
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
* @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/*]]>*/";
}
* @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 );
}
/**
* 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
* 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
* @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
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 );
* @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 );
}
/**
$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
+ // http://www.whatwg.org/html/embedded-content-1.html#attr-img-srcset
$candidates[] = "{$url} {$density}x";
}
return implode( ", ", $candidates );