83 'formnovalidate' =>
true,
100 'typemustmatch' =>
true,
114 if ( isset( $attrs[
'class'] ) ) {
115 if ( is_array( $attrs[
'class'] ) ) {
116 $attrs[
'class'][] =
'mw-ui-button';
117 $attrs[
'class'] = array_merge( $attrs[
'class'], $modifiers );
119 $attrs[
'class'] = implode(
' ', $attrs[
'class'] );
121 $attrs[
'class'] .=
' mw-ui-button ' . implode(
' ', $modifiers );
125 $attrs[
'class'] =
'mw-ui-button ' . implode(
' ', $modifiers );
141 if ( isset( $attrs[
'class'] ) ) {
142 if ( is_array( $attrs[
'class'] ) ) {
143 $attrs[
'class'][] =
'mw-ui-input';
145 $attrs[
'class'] .=
' mw-ui-input';
148 $attrs[
'class'] =
'mw-ui-input';
166 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
167 return self::element(
'a',
168 self::buttonAttributes( $attrs, $modifiers ),
186 public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
187 $attrs[
'type'] =
'submit';
188 $attrs[
'value'] = $contents;
189 return self::element(
'input', self::buttonAttributes( $attrs, $modifiers ) );
210 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
211 $start = self::openElement( $element, $attribs );
212 if ( isset( self::$voidElements[$element] ) ) {
214 return substr( $start, 0, -1 ) .
'/>';
216 return $start . $contents . self::closeElement( $element );
232 public static function element( $element, $attribs = [], $contents =
'' ) {
233 return self::rawElement( $element, $attribs, strtr( $contents ??
'', [
253 $attribs = (array)$attribs;
256 $element = strtolower( $element );
260 if ( strpos( $element,
' ' ) !==
false ) {
261 wfWarn( __METHOD__ .
" given element name with space '$element'" );
265 if ( $element ==
'input' ) {
280 'datetime-local' =>
true,
293 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
294 unset( $attribs[
'type'] );
301 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
302 $attribs[
'type'] =
'submit';
305 return "<$element" . self::expandAttributes(
306 self::dropDefaults( $element, $attribs ) ) .
'>';
317 $element = strtolower( $element );
319 return "</$element>";
342 static $attribDefaults = [
343 'area' => [
'shape' =>
'rect' ],
345 'formaction' =>
'GET',
346 'formenctype' =>
'application/x-www-form-urlencoded',
354 'autocomplete' =>
'on',
355 'enctype' =>
'application/x-www-form-urlencoded',
358 'formaction' =>
'GET',
361 'keygen' => [
'keytype' =>
'rsa' ],
362 'link' => [
'media' =>
'all' ],
363 'menu' => [
'type' =>
'list' ],
364 'script' => [
'type' =>
'text/javascript' ],
367 'type' =>
'text/css',
369 'textarea' => [
'wrap' =>
'soft' ],
372 foreach ( $attribs as $attrib => $value ) {
373 if ( $attrib ===
'class' ) {
374 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
375 unset( $attribs[$attrib] );
377 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
378 if ( is_array( $value ) ) {
379 $value = implode(
' ', $value );
381 $value = strval( $value );
383 if ( $attribDefaults[$element][$attrib] == $value ) {
384 unset( $attribs[$attrib] );
390 if ( $element ===
'link'
391 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
393 unset( $attribs[
'type'] );
395 if ( $element ===
'input' ) {
396 $type = $attribs[
'type'] ??
null;
397 $value = $attribs[
'value'] ??
null;
398 if (
$type ===
'checkbox' ||
$type ===
'radio' ) {
402 if ( $value ===
'on' ) {
403 unset( $attribs[
'value'] );
405 } elseif (
$type ===
'submit' ) {
413 if ( $value ===
'' ) {
414 unset( $attribs[
'value'] );
418 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
419 if ( in_array(
'multiple', $attribs )
420 || ( isset( $attribs[
'multiple'] ) && $attribs[
'multiple'] !==
false )
423 if ( strval( $attribs[
'size'] ) ==
'4' ) {
424 unset( $attribs[
'size'] );
428 if ( strval( $attribs[
'size'] ) ==
'1' ) {
429 unset( $attribs[
'size'] );
478 foreach ( $attribs as $key => $value ) {
480 if ( $value ===
false || $value ===
null ) {
486 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
492 $key = strtolower( $key );
496 $spaceSeparatedListAttributes = [
506 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
511 if ( is_array( $value ) ) {
514 foreach ( $value as $k => $v ) {
515 if ( is_string( $v ) ) {
518 if ( !isset( $value[$v] ) ) {
522 foreach ( explode(
' ', $v ) as $part ) {
525 if ( $part !==
'' && $part !==
' ' ) {
526 $arrayValue[] = $part;
537 $arrayValue = explode(
' ', $value );
540 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
544 $value = implode(
' ', array_unique( $arrayValue ) );
551 } elseif ( is_array( $value ) ) {
552 throw new MWException(
"HTML attribute $key can not contain a list of values" );
555 if ( isset( self::$boolAttribs[$key] ) ) {
556 $ret .=
" $key=\"\"";
561 $encValue = htmlspecialchars( $value, ENT_QUOTES );
565 $encValue = strtr( $encValue, [
570 $ret .=
" $key=\"$encValue\"";
591 if ( $nonce !==
null ) {
592 $attrs[
'nonce'] = $nonce;
594 wfWarn(
"no nonce set on script. CSP will break it" );
597 if ( preg_match(
'/<\/?script/i', $contents ) ) {
598 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
599 $contents =
'/* ERROR: Invalid script */';
602 return self::rawElement(
'script', $attrs, $contents );
614 $attrs = [
'src' => $url ];
615 if ( $nonce !==
null ) {
616 $attrs[
'nonce'] = $nonce;
618 wfWarn(
"no nonce set on script. CSP will break it" );
621 return self::element(
'script', $attrs );
636 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
641 $contents = strtr( $contents, [
645 ']]>' =>
'\5D\5D\3E '
648 if ( preg_match(
'/[<&]/', $contents ) ) {
649 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
652 return self::rawElement(
'style', [
654 ] + $attribs, $contents );
666 return self::element(
'link', [
667 'rel' =>
'stylesheet',
684 public static function input( $name, $value =
'',
$type =
'text', array $attribs = [] ) {
685 $attribs[
'type'] =
$type;
686 $attribs[
'value'] = $value;
687 $attribs[
'name'] = $name;
688 $textInputAttributes = [
695 if ( isset( $textInputAttributes[
$type] ) ) {
696 $attribs = self::getTextInputAttributes( $attribs );
698 $buttonAttributes = [
703 if ( isset( $buttonAttributes[
$type] ) ) {
704 $attribs = self::buttonAttributes( $attribs );
706 return self::element(
'input', $attribs );
717 public static function check( $name, $checked =
false, array $attribs = [] ) {
718 if ( isset( $attribs[
'value'] ) ) {
719 $value = $attribs[
'value'];
720 unset( $attribs[
'value'] );
726 $attribs[] =
'checked';
729 return self::input( $name, $value,
'checkbox', $attribs );
740 private static function messageBox( $html, $className, $heading =
'' ) {
741 if ( $heading !==
'' ) {
742 $html = self::element(
'h2', [], $heading ) . $html;
744 return self::rawElement(
'div', [
'class' => $className ], $html );
755 public static function warningBox( $html, $className =
'' ) {
756 return self::messageBox( $html, [
'warningbox', $className ] );
768 public static function errorBox( $html, $heading =
'', $className =
'' ) {
769 return self::messageBox( $html, [
'errorbox', $className ], $heading );
780 public static function successBox( $html, $className =
'' ) {
781 return self::messageBox( $html, [
'successbox', $className ] );
792 public static function radio( $name, $checked =
false, array $attribs = [] ) {
793 if ( isset( $attribs[
'value'] ) ) {
794 $value = $attribs[
'value'];
795 unset( $attribs[
'value'] );
801 $attribs[] =
'checked';
804 return self::input( $name, $value,
'radio', $attribs );
815 public static function label( $label, $id, array $attribs = [] ) {
819 return self::element(
'label', $attribs, $label );
831 public static function hidden( $name, $value, array $attribs = [] ) {
832 return self::input( $name, $value,
'hidden', $attribs );
847 public static function textarea( $name, $value =
'', array $attribs = [] ) {
848 $attribs[
'name'] = $name;
850 if ( substr( $value, 0, 1 ) ==
"\n" ) {
855 $spacedValue =
"\n" . $value;
857 $spacedValue = $value;
859 return self::element(
'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
868 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
869 $params[
'exclude'] = [];
872 if ( $params[
'in-user-lang'] ??
false ) {
876 $lang = MediaWikiServices::getInstance()->getContentLanguage();
880 if ( isset( $params[
'all'] ) ) {
883 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
886 $options =
$lang->getFormattedNamespaces();
888 foreach ( $options as $nsId => $nsName ) {
889 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
895 $nsName =
wfMessage(
'blanknamespace' )->text();
896 } elseif ( is_int( $nsId ) ) {
897 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
898 ->getLanguageConverter(
$lang );
899 $nsName = $converter->convertNamespace( $nsId );
901 $optionsOut[$nsId] = $nsName;
924 array $selectAttribs = []
926 ksort( $selectAttribs );
929 if ( isset( $params[
'selected'] ) ) {
932 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
933 $params[
'selected'] = (int)$params[
'selected'];
937 $params[
'selected'] =
'';
940 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
941 $params[
'disable'] = [];
945 $options = self::namespaceSelectorOptions( $params );
949 foreach ( $options as $nsId => $nsName ) {
950 $optionsHtml[] = self::element(
952 'disabled' => in_array( $nsId, $params[
'disable'] ),
954 'selected' => $nsId === $params[
'selected'],
959 if ( !array_key_exists(
'id', $selectAttribs ) ) {
960 $selectAttribs[
'id'] =
'namespace';
963 if ( !array_key_exists(
'name', $selectAttribs ) ) {
964 $selectAttribs[
'name'] =
'namespace';
968 if ( isset( $params[
'label'] ) ) {
969 $ret .= self::element(
971 'for' => $selectAttribs[
'id'] ??
null,
977 $ret .= self::openElement(
'select', $selectAttribs )
979 . implode(
"\n", $optionsHtml )
981 . self::closeElement(
'select' );
1004 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1007 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1011 $attribs[
"xmlns:$tag"] = $ns;
1014 $ret .=
"<!DOCTYPE html>\n";
1021 $ret .= self::openElement(
'html', $attribs );
1036 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1037 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1063 public static function srcSet( array $urls ) {
1065 foreach ( $urls as $density => $url ) {
1068 $density = (string)(
float)$density;
1069 $candidates[$density] = $url;
1073 ksort( $candidates, SORT_NUMERIC );
1074 $candidates = array_unique( $candidates );
1077 foreach ( $candidates as $density => $url ) {
1078 $candidates[$density] = $url .
' ' . $density .
'x';
1081 return implode(
", ", $candidates );
$wgMimeType
The default Content-Type header.
$wgUseMediaWikiUIEverywhere
Temporary variable that applies MediaWiki UI wherever it can be supported.
$wgHtml5Version
Defines the value of the version attribute in the <html> tag, if any.
$wgXhtmlNamespaces
Permit other namespaces in addition to the w3.org default.
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static isNonceRequired(Config $config)
Should we set nonce attribute.
This class is a collection of static functions that serve two purposes:
static inlineScript( $contents, $nonce=null)
Output an HTML script tag with the given contents.
static label( $label, $id, array $attribs=[])
Convenience function for generating a label for inputs.
static textarea( $name, $value='', array $attribs=[])
Convenience function to produce a <textarea> element.
static namespaceSelectorOptions(array $params=[])
Helper for Html::namespaceSelector().
static linkedScript( $url, $nonce=null)
Output a "<script>" tag linking to the given URL, e.g., "<script src=foo.js></script>".
static buttonAttributes(array $attrs, array $modifiers=[])
Modifies a set of attributes meant for button elements and apply a set of default attributes when $wg...
static linkedStyle( $url, $media='all')
Output a "<link rel=stylesheet>" linking to the given URL for the given media type (if any).
static bool[] $boolAttribs
Boolean attributes, which may have the value omitted entirely.
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
static messageBox( $html, $className, $heading='')
Return the HTML for a message box.
static isXmlMimeType( $mimetype)
Determines if the given MIME type is xml.
static bool[] $voidElements
List of void elements from HTML5, section 8.1.2 as of 2016-09-19.
static htmlHeader(array $attribs=[])
Constructs the opening html-tag with necessary doctypes depending on global variables.
static input( $name, $value='', $type='text', array $attribs=[])
Convenience function to produce an "<input>" element.
static dropDefaults( $element, array $attribs)
Given an element name and an associative array of element attributes, return an array that is functio...
static submitButton( $contents, array $attrs, array $modifiers=[])
Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enab...
static getTextInputAttributes(array $attrs)
Modifies a set of attributes meant for text input elements and apply a set of default attributes.
static radio( $name, $checked=false, array $attribs=[])
Convenience function to produce a radio button (input element with type=radio)
static expandAttributes(array $attribs)
Given an associative array of element attributes, generate a string to stick after the element name i...
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
static warningBox( $html, $className='')
Return a warning box.
static namespaceSelector(array $params=[], array $selectAttribs=[])
Build a drop-down box for selecting a namespace.
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
static linkButton( $text, array $attrs, array $modifiers=[])
Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enab...
static check( $name, $checked=false, array $attribs=[])
Convenience function to produce a checkbox (input element with type=checkbox)
static srcSet(array $urls)
Generate a srcset attribute value.
static successBox( $html, $className='')
Return a success box.
static errorBox( $html, $heading='', $className='')
Return an error box.
static closeElement( $element)
Returns "</$element>".
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
static inlineStyle( $contents, $media='all', $attribs=[])
Output a "<style>" tag with the given contents for the given media type (if any).
if(!isset( $args[0])) $lang