elements.
*/
class Html {
# List of void elements from HTML 5, section 9.1.2 as of 2009-08-10
private static $voidElements = array(
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
'keygen', 'link', 'meta', 'param', 'source'
);
/**
* Returns an HTML element in a string. The major advantage here over
* manually typing out the HTML is that it will escape all attribute
* values. If you're hardcoding all the attributes, or there are none, you
* should probably type out the string yourself.
*
* This is quite similar to Xml::element(), but it implements some useful
* HTML-specific logic. For instance, there is no $allowShortTag
* parameter: the closing tag is magically omitted if $element has an empty
* content model. If $wgWellFormedXml is false, then a few bytes will be
* shaved off the HTML output as well. In the future, other HTML-specific
* features might be added, like allowing arrays for the values of
* attributes like class= and media=.
*
* One notable difference to Xml::element() is that $contents is *not*
* escaped. This means that Html::element() can be usefully nested, rather
* than using the rather clumsy Xml::openElement() and Xml::closeElement().
*
* @param $element string The element's name, e.g., 'a'
* @param $attribs array Associative array of attributes, e.g., array(
* 'href' => 'http://www.mediawiki.org/' ). Values will be HTML-escaped.
* @param $contents string The raw HTML contents of the element: *not*
* escaped!
* @return string Raw HTML
*/
public static function element( $element, $attribs = array(), $contents = '' ) {
global $wgWellFormedXml;
$element = strtolower( $element );
$start = "<$element" . self::expandAttributes( $attribs );
if ( in_array( $element, self::$voidElements ) ) {
if ( $wgWellFormedXml ) {
return "$start />";
}
return "$start>";
} else {
return "$start>$contents$element>";
}
}
/**
* Given an associative array of element attributes, generate a string
* to stick after the element name in HTML output. Like array( 'href' =>
* 'http://www.mediawiki.org/' ) becomes something like
* ' href="http://www.mediawiki.org"'. Again, this is like
* Xml::expandAttributes(), but it implements some HTML-specific logic.
* For instance, it will omit quotation marks if $wgWellFormedXml is false.
*
* @param $attribs array Associative array of attributes, e.g., array(
* 'href' => 'http://www.mediawiki.org/' ). Values will be HTML-escaped.
* @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 ) {
global $wgWellFormedXml;
$ret = '';
foreach ( $attribs as $key => $value ) {
# See the "Attributes" section in the HTML syntax part of HTML 5,
# 9.1.2.3 as of 2009-08-10. Most attributes can have quotation
# marks omitted, but not all. (Although a literal " is not
# permitted, we don't check for that, since it will be escaped
# anyway.)
if ( $wgWellFormedXml || $value == ''
|| preg_match( "/[ '=<>]/", $value ) ) {
$quote = '"';
} else {
$quote = '';
}
# 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(). FIXME: verify that we actually need to
# escape \n\r\t here, and explain why, exactly.
$ret .= " $key=$quote" . strtr( $value, array(
'&' => '&',
'"' => '"',
"\n" => '
',
"\r" => '
',
"\t" => ' '
) ) . $quote;
}
return $ret;
}
/**
* Output a ' or (for
* XML) literal "]]>".
*
* @param $contents string JavaScript
* @return string Raw HTML
*/
public static function inlineScript( $contents ) {
global $wgHtml5, $wgJsMimeType;
$attrs = array();
if ( !$wgHtml5 ) {
$attrs['type'] = $wgJsMimeType;
$contents = "/**/";
}
return self::element( 'script', $attrs, $contents );
}
/**
* Output a .
*
* @param $url string
* @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 );
}
/**
* Output a ' (admittedly unlikely).
*
* @param $contents string CSS
* @param $media mixed A media type string, like 'screen', or null for all
* media
* @return string Raw HTML
*/
public static function inlineStyle( $contents, $media = null ) {
global $wgHtml5;
$attrs = array();
if ( !$wgHtml5 ) {
# Technically we should probably add CDATA stuff here like with
# scripts, but in practice, stylesheets tend not to have
# problematic characters anyway.
$attrs['type'] = 'text/css';
}
if ( $media !== null ) {
$attrs['media'] = $media;
}
return self::element( 'style', $attrs, $contents );
}
/**
* 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', or null for all
* media
* @return string Raw HTML
*/
public static function linkedStyle( $url, $media = null ) {
global $wgHtml5;
$attrs = array( 'rel' => 'stylesheet', 'href' => $url );
if ( !$wgHtml5 ) {
$attrs['type'] = 'text/css';
}
if ( $media !== null ) {
$attrs['media'] = $media;
}
return self::element( 'link', $attrs );
}
}