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',
);
# Boolean attributes, which may have the value omitted entirely. Manually
# collected from the HTML 5 spec as of 2009-08-10.
private static $boolAttribs = array(
'async',
'autobuffer',
'autofocus',
'autoplay',
'checked',
'controls',
'defer',
'disabled',
'formnovalidate',
'hidden',
'ismap',
'loop',
'multiple',
'novalidate',
'open',
'readonly',
'required',
'reversed',
'scoped',
'seamless',
);
# A nested associative array of element => content attribute => default
# value. Attributes that have the default value will be omitted, since
# they're pointless. Currently the list hasn't been systematically
# populated.
private static $attribDefaults = array(
'input' => array(
'value' => '',
'type' => 'text',
),
);
/**
* 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::tags(), 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=.
*
* @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.
* A value of false means to omit the attribute.
* @param $contents string The raw HTML contents of the element: *not*
* escaped!
* @return string Raw HTML
*/
public static function rawElement( $element, $attribs = array(), $contents = '' ) {
global $wgHtml5, $wgWellFormedXml;
# This is not required in HTML 5, but let's do it anyway, for
# consistency and better compression.
$element = strtolower( $element );
# Element-specific hacks to slim down output and ensure validity
if ( $element == 'input' ) {
if ( !$wgHtml5 ) {
# With $wgHtml5 off we want to validate as XHTML 1, so we
# strip out any fancy HTML 5-only input types for now.
#
# Whitelist of valid types:
$validTypes = array(
'hidden',
'text',
'password',
'checkbox',
'radio',
'file',
'submit',
'image',
'reset',
'button',
);
if ( isset( $attribs['type'] )
&& !in_array( $attribs['type'], $validTypes ) ) {
# Fall back to type=text, the default
unset( $attribs['type'] );
}
# Here we're blacklisting some HTML5-only attributes...
$html5attribs = array(
'autocomplete',
'autofocus',
'max',
'min',
'multiple',
'pattern',
'placeholder',
'required',
'step',
);
foreach ( $html5attribs as $badAttr ) {
unset( $attribs[$badAttr] );
}
}
}
# Don't bother outputting the default values for attributes
foreach ( $attribs as $attrib => $value ) {
$lcattrib = strtolower( $attrib );
if ( isset( self::$attribDefaults[$element][$lcattrib] ) &&
self::$attribDefaults[$element][$lcattrib] === $value ) {
unset( $attribs[$attrib] );
}
}
$start = "<$element" . self::expandAttributes( $attribs );
if ( in_array( $element, self::$voidElements ) ) {
if ( $wgWellFormedXml ) {
return "$start />";
}
return "$start>";
} else {
return "$start>$contents$element>";
}
}
/**
* Identical to rawElement(), but HTML-escapes $contents (like
* Xml::element()).
*/
public static function element( $element, $attribs = array(), $contents = '' ) {
return self::rawElement( $element, $attribs, strtr( $contents, array(
# There's no point in escaping quotes, >, etc. in the contents of
# elements.
'&' => '&',
'<' => '<'
) ) );
}
/**
* 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,
* and will treat boolean attributes specially.
*
* @param $attribs array Associative array of attributes, e.g., array(
* 'href' => 'http://www.mediawiki.org/' ). Values will be HTML-escaped.
* A value of false means to omit the attribute.
* @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 $wgHtml5, $wgWellFormedXml;
$ret = '';
foreach ( $attribs as $key => $value ) {
if ( $value === false ) {
continue;
}
# For boolean attributes, support array( 'foo' ) instead of
# requiring array( 'foo' => 'meaningless' ).
if ( is_int( $key )
&& in_array( strtolower( $value ), self::$boolAttribs ) ) {
$key = $value;
}
# Not technically required in HTML 5, but required in XHTML 1.0,
# and we'd like consistency and better compression anyway.
$key = strtolower( $key );
# 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 = '';
}
if ( in_array( $key, self::$boolAttribs ) ) {
# In XHTML 1.0 Transitional, the value needs to be equal to the
# key. In HTML 5, we can leave the value empty instead. 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\"";
}
} else {
# 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, $wgWellFormedXml;
$attrs = array();
if ( !$wgHtml5 ) {
$attrs['type'] = $wgJsMimeType;
}
if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
$contents = "/**/";
}
return self::rawElement( '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, $wgWellFormedXml;
$attrs = array();
if ( !$wgHtml5 ) {
$attrs['type'] = 'text/css';
}
if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
$contents = "/**/";
}
if ( $media !== null ) {
$attrs['media'] = $media;
}
return self::rawElement( '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 );
}
/**
* Convenience function to produce an element. This supports the
* new HTML 5 input types and attributes, and will silently strip them if
* $wgHtml5 is false.
*
* @param $name string name attribute
* @param $value mixed value attribute (null = omit)
* @param $type string type attribute
* @param $attribs array Associative array of miscellaneous extra
* attributes, passed to Html::element()
* @return string Raw HTML
*/
public static function input( $name, $value = null, $type = 'text', $attribs = array() ) {
$attribs['type'] = $type;
if ( $value !== null ) {
$attribs['value'] = $value;
}
$attribs['name'] = $name;
return self::element( 'input', $attribs );
}
/**
* Convenience function to produce an input element with type=hidden, like
* Xml::hidden.
*
* @param $name string name attribute
* @param $value string value attribute
* @param $attribs array Associative array of miscellaneous extra
* attributes, passed to Html::element()
* @return string Raw HTML
*/
public static function hidden( $name, $value, $attribs = array() ) {
return self::input( $name, $value, 'hidden', $attribs );
}
}