*/
class Html {
// List of void elements from HTML5, section 8.1.2 as of 2011-08-12
- private static $voidElements = array(
+ private static $voidElements = [
'area',
'base',
'br',
'source',
'track',
'wbr',
- );
+ ];
// Boolean attributes, which may have the value omitted entirely. Manually
// collected from the HTML5 spec as of 2011-08-12.
- private static $boolAttribs = array(
+ private static $boolAttribs = [
'async',
'autofocus',
'autoplay',
'typemustmatch',
// HTML5 Microdata
'itemscope',
- );
+ ];
/**
* Modifies a set of attributes meant for button elements
* @see https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
* @return array $attrs A modified attribute array
*/
- public static function buttonAttributes( array $attrs, array $modifiers = array() ) {
+ public static function buttonAttributes( array $attrs, array $modifiers = [] ) {
global $wgUseMediaWikiUIEverywhere;
if ( $wgUseMediaWikiUIEverywhere ) {
if ( isset( $attrs['class'] ) ) {
* @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
* @return string Raw HTML
*/
- public static function linkButton( $contents, array $attrs, array $modifiers = array() ) {
+ public static function linkButton( $contents, array $attrs, array $modifiers = [] ) {
return self::element( 'a',
self::buttonAttributes( $attrs, $modifiers ),
$contents
* @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
* @return string Raw HTML
*/
- public static function submitButton( $contents, array $attrs, array $modifiers = array() ) {
+ public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
$attrs['type'] = 'submit';
$attrs['value'] = $contents;
return self::element( 'input', self::buttonAttributes( $attrs, $modifiers ) );
* escaped!
* @return string Raw HTML
*/
- public static function rawElement( $element, $attribs = array(), $contents = '' ) {
+ public static function rawElement( $element, $attribs = [], $contents = '' ) {
global $wgWellFormedXml;
$start = self::openElement( $element, $attribs );
if ( in_array( $element, self::$voidElements ) ) {
*
* @return string
*/
- public static function element( $element, $attribs = array(), $contents = '' ) {
- return self::rawElement( $element, $attribs, strtr( $contents, array(
+ public static function element( $element, $attribs = [], $contents = '' ) {
+ return self::rawElement( $element, $attribs, strtr( $contents, [
// There's no point in escaping quotes, >, etc. in the contents of
// elements.
'&' => '&',
'<' => '<'
- ) ) );
+ ] ) );
}
/**
*
* @return string
*/
- public static function openElement( $element, $attribs = array() ) {
+ public static function openElement( $element, $attribs = [] ) {
$attribs = (array)$attribs;
// This is not required in HTML5, but let's do it anyway, for
// consistency and better compression.
// Remove invalid input types
if ( $element == 'input' ) {
- $validTypes = array(
+ $validTypes = [
'hidden',
'text',
'password',
'search',
'tel',
'color',
- );
+ ];
if ( isset( $attribs['type'] ) && !in_array( $attribs['type'], $validTypes ) ) {
unset( $attribs['type'] );
}
private static function dropDefaults( $element, array $attribs ) {
// Whenever altering this array, please provide a covering test case
// in HtmlTest::provideElementsWithAttributesHavingDefaultValues
- static $attribDefaults = array(
- 'area' => array( 'shape' => 'rect' ),
- 'button' => array(
+ static $attribDefaults = [
+ 'area' => [ 'shape' => 'rect' ],
+ 'button' => [
'formaction' => 'GET',
'formenctype' => 'application/x-www-form-urlencoded',
- ),
- 'canvas' => array(
+ ],
+ 'canvas' => [
'height' => '150',
'width' => '300',
- ),
- 'command' => array( 'type' => 'command' ),
- 'form' => array(
+ ],
+ 'command' => [ 'type' => 'command' ],
+ 'form' => [
'action' => 'GET',
'autocomplete' => 'on',
'enctype' => 'application/x-www-form-urlencoded',
- ),
- 'input' => array(
+ ],
+ 'input' => [
'formaction' => 'GET',
'type' => 'text',
- ),
- 'keygen' => array( 'keytype' => 'rsa' ),
- 'link' => array( 'media' => 'all' ),
- 'menu' => array( 'type' => 'list' ),
- 'script' => array( 'type' => 'text/javascript' ),
- 'style' => array(
+ ],
+ 'keygen' => [ 'keytype' => 'rsa' ],
+ 'link' => [ 'media' => 'all' ],
+ 'menu' => [ 'type' => 'list' ],
+ 'script' => [ 'type' => 'text/javascript' ],
+ 'style' => [
'media' => 'all',
'type' => 'text/css',
- ),
- 'textarea' => array( 'wrap' => 'soft' ),
- );
+ ],
+ 'textarea' => [ 'wrap' => 'soft' ],
+ ];
$element = strtolower( $element );
// numbers to be entered in 'type="number"' fields, allow
// the special case 'step="any"'.
- if ( in_array( $key, array( 'max', 'min', 'pattern', 'required' ) )
+ if ( in_array( $key, [ 'max', 'min', 'pattern', 'required' ] )
|| $key === 'step' && $value !== 'any' ) {
continue;
}
// http://www.w3.org/TR/html401/index/attributes.html ("space-separated")
// http://www.w3.org/TR/html5/index.html#attributes-1 ("space-separated")
- $spaceSeparatedListAttributes = array(
+ $spaceSeparatedListAttributes = [
'class', // html4, html5
'accesskey', // as of html5, multiple space-separated values allowed
// html4-spec doesn't document rel= as space-separated
// but has been used like that and is now documented as such
// in the html5-spec.
'rel',
- );
+ ];
// Specific features for attributes that allow a list of space-separated values
if ( in_array( $key, $spaceSeparatedListAttributes ) ) {
// values. Implode/explode to get those into the main array as well.
if ( is_array( $value ) ) {
// If input wasn't an array, we can skip this step
- $newValue = array();
+ $newValue = [];
foreach ( $value as $k => $v ) {
if ( is_string( $v ) ) {
// String values should be normal `array( 'foo' )`
// Normalize spacing by fixing up cases where people used
// more than 1 space and/or a trailing/leading space
- $value = array_diff( $value, array( '', ' ' ) );
+ $value = array_diff( $value, [ '', ' ' ] );
// Remove duplicates and create the string
$value = implode( ' ', array_unique( $value ) );
// The only difference between this transform and the one by
// Sanitizer::encodeAttribute() is '<' is only encoded here if
// $wgWellFormedXml is set, and ' is not encoded.
- $map = array(
+ $map = [
'&' => '&',
'"' => '"',
'>' => '>',
"\n" => ' ',
"\r" => ' ',
"\t" => '	'
- );
+ ];
if ( $wgWellFormedXml ) {
// This is allowed per spec: <http://www.w3.org/TR/xml/#NT-AttValue>
// But reportedly it breaks some XML tools?
public static function inlineScript( $contents ) {
global $wgWellFormedXml;
- $attrs = array();
+ $attrs = [];
if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
$contents = "/*<![CDATA[*/$contents/*]]>*/";
* @return string Raw HTML
*/
public static function linkedScript( $url ) {
- $attrs = array( 'src' => $url );
+ $attrs = [ 'src' => $url ];
return self::element( 'script', $attrs );
}
$contents = "/*<![CDATA[*/$contents/*]]>*/";
}
- return self::rawElement( 'style', array(
+ return self::rawElement( 'style', [
'media' => $media,
- ), $contents );
+ ], $contents );
}
/**
* @return string Raw HTML
*/
public static function linkedStyle( $url, $media = 'all' ) {
- return self::element( 'link', array(
+ return self::element( 'link', [
'rel' => 'stylesheet',
'href' => $url,
'media' => $media,
- ) );
+ ] );
}
/**
* attributes, passed to Html::element()
* @return string Raw HTML
*/
- public static function input( $name, $value = '', $type = 'text', array $attribs = array() ) {
+ public static function input( $name, $value = '', $type = 'text', array $attribs = [] ) {
$attribs['type'] = $type;
$attribs['value'] = $value;
$attribs['name'] = $name;
- if ( in_array( $type, array( 'text', 'search', 'email', 'password', 'number' ) ) ) {
+ if ( in_array( $type, [ 'text', 'search', 'email', 'password', 'number' ] ) ) {
$attribs = self::getTextInputAttributes( $attribs );
}
- if ( in_array( $type, array( 'button', 'reset', 'submit' ) ) ) {
+ if ( in_array( $type, [ 'button', 'reset', 'submit' ] ) ) {
$attribs = self::buttonAttributes( $attribs );
}
return self::element( 'input', $attribs );
* @param array $attribs Array of additional attributes
* @return string Raw HTML
*/
- public static function check( $name, $checked = false, array $attribs = array() ) {
+ public static function check( $name, $checked = false, array $attribs = [] ) {
if ( isset( $attribs['value'] ) ) {
$value = $attribs['value'];
unset( $attribs['value'] );
* @param array $attribs Array of additional attributes
* @return string Raw HTML
*/
- public static function radio( $name, $checked = false, array $attribs = array() ) {
+ public static function radio( $name, $checked = false, array $attribs = [] ) {
if ( isset( $attribs['value'] ) ) {
$value = $attribs['value'];
unset( $attribs['value'] );
* @param array $attribs Additional attributes
* @return string Raw HTML
*/
- public static function label( $label, $id, array $attribs = array() ) {
- $attribs += array(
+ public static function label( $label, $id, array $attribs = [] ) {
+ $attribs += [
'for' => $id
- );
+ ];
return self::element( 'label', $attribs, $label );
}
* attributes, passed to Html::element()
* @return string Raw HTML
*/
- public static function hidden( $name, $value, array $attribs = array() ) {
+ public static function hidden( $name, $value, array $attribs = [] ) {
return self::input( $name, $value, 'hidden', $attribs );
}
* attributes, passed to Html::element()
* @return string Raw HTML
*/
- public static function textarea( $name, $value = '', array $attribs = array() ) {
+ public static function textarea( $name, $value = '', array $attribs = [] ) {
$attribs['name'] = $name;
if ( substr( $value, 0, 1 ) == "\n" ) {
* @param array $params See Html::namespaceSelector()
* @return array
*/
- public static function namespaceSelectorOptions( array $params = array() ) {
+ public static function namespaceSelectorOptions( array $params = [] ) {
global $wgContLang;
- $options = array();
+ $options = [];
if ( !isset( $params['exclude'] ) || !is_array( $params['exclude'] ) ) {
- $params['exclude'] = array();
+ $params['exclude'] = [];
}
if ( isset( $params['all'] ) ) {
// Add all namespaces as options (in the content language)
$options += $wgContLang->getFormattedNamespaces();
- $optionsOut = array();
+ $optionsOut = [];
// Filter out namespaces below 0 and massage labels
foreach ( $options as $nsId => $nsName ) {
if ( $nsId < NS_MAIN || in_array( $nsId, $params['exclude'] ) ) {
* - name: [optional], default: 'namespace'.
* @return string HTML code to select a namespace.
*/
- public static function namespaceSelector( array $params = array(),
- array $selectAttribs = array()
+ public static function namespaceSelector( array $params = [],
+ array $selectAttribs = []
) {
ksort( $selectAttribs );
}
if ( !isset( $params['disable'] ) || !is_array( $params['disable'] ) ) {
- $params['disable'] = array();
+ $params['disable'] = [];
}
// Associative array between option-values and option-labels
$options = self::namespaceSelectorOptions( $params );
// Convert $options to HTML
- $optionsHtml = array();
+ $optionsHtml = [];
foreach ( $options as $nsId => $nsName ) {
$optionsHtml[] = self::element(
- 'option', array(
+ 'option', [
'disabled' => in_array( $nsId, $params['disable'] ),
'value' => $nsId,
'selected' => $nsId === $params['selected'],
- ), $nsName
+ ], $nsName
);
}
$ret = '';
if ( isset( $params['label'] ) ) {
$ret .= self::element(
- 'label', array(
+ 'label', [
'for' => isset( $selectAttribs['id'] ) ? $selectAttribs['id'] : null,
- ), $params['label']
+ ], $params['label']
) . ' ';
}
* attributes, passed to Html::element() of html tag.
* @return string Raw HTML
*/
- public static function htmlHeader( array $attribs = array() ) {
+ public static function htmlHeader( array $attribs = [] ) {
$ret = '';
global $wgHtml5Version, $wgMimeType, $wgXhtmlNamespaces;
* @return string
*/
static function infoBox( $text, $icon, $alt, $class = '' ) {
- $s = self::openElement( 'div', array( 'class' => "mw-infobox $class" ) );
+ $s = self::openElement( 'div', [ 'class' => "mw-infobox $class" ] );
- $s .= self::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ) .
+ $s .= self::openElement( 'div', [ 'class' => 'mw-infobox-left' ] ) .
self::element( 'img',
- array(
+ [
'src' => $icon,
'alt' => $alt,
- )
+ ]
) .
self::closeElement( 'div' );
- $s .= self::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ) .
+ $s .= self::openElement( 'div', [ 'class' => 'mw-infobox-right' ] ) .
$text .
self::closeElement( 'div' );
- $s .= self::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
+ $s .= self::element( 'div', [ 'style' => 'clear: left;' ], ' ' );
$s .= self::closeElement( 'div' );
- $s .= self::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
+ $s .= self::element( 'div', [ 'style' => 'clear: left;' ], ' ' );
return $s;
}
* @return string
*/
static function srcSet( array $urls ) {
- $candidates = array();
+ $candidates = [];
foreach ( $urls as $density => $url ) {
// Cast density to float to strip 'x'.
$candidates[] = $url . ' ' . (float)$density . 'x';