57 private static $voidElements = [
80 private static $boolAttribs = [
89 'formnovalidate' =>
true,
106 'typemustmatch' =>
true,
118 $useMediaWikiUIEverywhere =
120 if ( $useMediaWikiUIEverywhere ) {
121 if ( isset( $attrs[
'class'] ) ) {
122 if ( is_array( $attrs[
'class'] ) ) {
123 $attrs[
'class'][] =
'mw-ui-button';
124 $attrs[
'class'] = array_merge( $attrs[
'class'], $modifiers );
126 $attrs[
'class'] = implode(
' ', $attrs[
'class'] );
128 $attrs[
'class'] .=
' mw-ui-button ' . implode(
' ', $modifiers );
132 $attrs[
'class'] =
'mw-ui-button ' . implode(
' ', $modifiers );
148 if ( $useMediaWikiUIEverywhere ) {
149 if ( isset( $attrs[
'class'] ) ) {
150 if ( is_array( $attrs[
'class'] ) ) {
151 $attrs[
'class'][] =
'mw-ui-input';
153 $attrs[
'class'] .=
' mw-ui-input';
156 $attrs[
'class'] =
'mw-ui-input';
174 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
177 self::buttonAttributes( $attrs, $modifiers ),
195 public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
196 $attrs[
'type'] =
'submit';
197 $attrs[
'value'] = $contents;
198 return self::element(
'input', self::buttonAttributes( $attrs, $modifiers ) );
219 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
221 if ( isset( self::$voidElements[$element] ) ) {
223 return substr( $start, 0, -1 ) .
'/>';
241 public static function element( $element, $attribs = [], $contents =
'' ) {
245 strtr( $contents ??
'', [
266 $attribs = (array)$attribs;
269 $element = strtolower( $element );
273 if ( strpos( $element,
' ' ) !==
false ) {
274 wfWarn( __METHOD__ .
" given element name with space '$element'" );
278 if ( $element ==
'input' ) {
293 'datetime-local' =>
true,
306 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
307 unset( $attribs[
'type'] );
314 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
315 $attribs[
'type'] =
'submit';
319 self::dropDefaults( $element, $attribs ) ) .
'>';
330 $element = strtolower( $element );
332 return "</$element>";
352 private static function dropDefaults( $element, array $attribs ) {
355 static $attribDefaults = [
356 'area' => [
'shape' =>
'rect' ],
358 'formaction' =>
'GET',
359 'formenctype' =>
'application/x-www-form-urlencoded',
367 'autocomplete' =>
'on',
368 'enctype' =>
'application/x-www-form-urlencoded',
371 'formaction' =>
'GET',
374 'keygen' => [
'keytype' =>
'rsa' ],
375 'link' => [
'media' =>
'all' ],
376 'menu' => [
'type' =>
'list' ],
377 'script' => [
'type' =>
'text/javascript' ],
380 'type' =>
'text/css',
382 'textarea' => [
'wrap' =>
'soft' ],
385 foreach ( $attribs as $attrib => $value ) {
386 if ( $attrib ===
'class' ) {
387 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
388 unset( $attribs[$attrib] );
390 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
391 if ( is_array( $value ) ) {
392 $value = implode(
' ', $value );
394 $value = strval( $value );
396 if ( $attribDefaults[$element][$attrib] == $value ) {
397 unset( $attribs[$attrib] );
403 if ( $element ===
'link'
404 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
406 unset( $attribs[
'type'] );
408 if ( $element ===
'input' ) {
409 $type = $attribs[
'type'] ??
null;
410 $value = $attribs[
'value'] ??
null;
411 if (
$type ===
'checkbox' ||
$type ===
'radio' ) {
415 if ( $value ===
'on' ) {
416 unset( $attribs[
'value'] );
418 } elseif (
$type ===
'submit' ) {
426 if ( $value ===
'' ) {
427 unset( $attribs[
'value'] );
431 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
432 if ( in_array(
'multiple', $attribs )
433 || ( isset( $attribs[
'multiple'] ) && $attribs[
'multiple'] !==
false )
436 if ( strval( $attribs[
'size'] ) ==
'4' ) {
437 unset( $attribs[
'size'] );
441 if ( strval( $attribs[
'size'] ) ==
'1' ) {
442 unset( $attribs[
'size'] );
491 foreach ( $attribs as $key => $value ) {
493 if ( $value ===
false || $value ===
null ) {
499 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
505 $key = strtolower( $key );
509 $spaceSeparatedListAttributes = [
519 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
524 if ( is_array( $value ) ) {
527 foreach ( $value as $k => $v ) {
528 if ( is_string( $v ) ) {
531 if ( !isset( $value[$v] ) ) {
535 foreach ( explode(
' ', $v ) as $part ) {
538 if ( $part !==
'' && $part !==
' ' ) {
539 $arrayValue[] = $part;
550 $arrayValue = explode(
' ', $value );
553 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
557 $value = implode(
' ', array_unique( $arrayValue ) );
564 } elseif ( is_array( $value ) ) {
565 throw new MWException(
"HTML attribute $key can not contain a list of values" );
568 if ( isset( self::$boolAttribs[$key] ) ) {
569 $ret .=
" $key=\"\"";
574 $encValue = htmlspecialchars( $value, ENT_QUOTES );
578 $encValue = strtr( $encValue, [
583 $ret .=
" $key=\"$encValue\"";
604 if ( $nonce !==
null ) {
605 $attrs[
'nonce'] = $nonce;
607 wfWarn(
"no nonce set on script. CSP will break it" );
610 if ( preg_match(
'/<\/?script/i', $contents ) ) {
611 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
612 $contents =
'/* ERROR: Invalid script */';
627 $attrs = [
'src' => $url ];
628 if ( $nonce !==
null ) {
629 $attrs[
'nonce'] = $nonce;
631 wfWarn(
"no nonce set on script. CSP will break it" );
649 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
654 $contents = strtr( $contents, [
658 ']]>' =>
'\5D\5D\3E '
661 if ( preg_match(
'/[<&]/', $contents ) ) {
662 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
667 ] + $attribs, $contents );
680 'rel' =>
'stylesheet',
697 public static function input( $name, $value =
'',
$type =
'text', array $attribs = [] ) {
698 $attribs[
'type'] =
$type;
699 $attribs[
'value'] = $value;
700 $attribs[
'name'] = $name;
701 $textInputAttributes = [
708 if ( isset( $textInputAttributes[
$type] ) ) {
711 $buttonAttributes = [
716 if ( isset( $buttonAttributes[
$type] ) ) {
730 public static function check( $name, $checked =
false, array $attribs = [] ) {
731 if ( isset( $attribs[
'value'] ) ) {
732 $value = $attribs[
'value'];
733 unset( $attribs[
'value'] );
739 $attribs[] =
'checked';
742 return self::input( $name, $value,
'checkbox', $attribs );
753 private static function messageBox( $html, $className, $heading =
'' ) {
754 if ( $heading !==
'' ) {
757 if ( is_array( $className ) ) {
758 $className[] =
'mw-message-box';
760 $className .=
' mw-message-box';
773 return self::messageBox( $html, [
'mw-message-box-notice', $className ] );
784 public static function warningBox( $html, $className =
'' ) {
785 return self::messageBox( $html, [
'mw-message-box-warning', $className ] );
797 public static function errorBox( $html, $heading =
'', $className =
'' ) {
798 return self::messageBox( $html, [
'mw-message-box-error', $className ], $heading );
809 public static function successBox( $html, $className =
'' ) {
810 return self::messageBox( $html, [
'mw-message-box-success', $className ] );
821 public static function radio( $name, $checked =
false, array $attribs = [] ) {
822 if ( isset( $attribs[
'value'] ) ) {
823 $value = $attribs[
'value'];
824 unset( $attribs[
'value'] );
830 $attribs[] =
'checked';
833 return self::input( $name, $value,
'radio', $attribs );
844 public static function label( $label, $id, array $attribs = [] ) {
860 public static function hidden( $name, $value, array $attribs = [] ) {
861 return self::input( $name, $value,
'hidden', $attribs );
876 public static function textarea( $name, $value =
'', array $attribs = [] ) {
877 $attribs[
'name'] = $name;
879 if ( substr( $value, 0, 1 ) ==
"\n" ) {
884 $spacedValue =
"\n" . $value;
886 $spacedValue = $value;
888 return self::element(
'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
897 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
898 $params[
'exclude'] = [];
901 if ( $params[
'in-user-lang'] ??
false ) {
909 if ( isset( $params[
'all'] ) ) {
912 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
915 $options =
$lang->getFormattedNamespaces();
917 foreach ( $options as $nsId => $nsName ) {
918 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
924 $nsName =
wfMessage(
'blanknamespace' )->text();
925 } elseif ( is_int( $nsId ) ) {
927 ->getLanguageConverter(
$lang );
928 $nsName = $converter->convertNamespace( $nsId );
930 $optionsOut[$nsId] = $nsName;
954 array $selectAttribs = []
956 ksort( $selectAttribs );
959 if ( isset( $params[
'selected'] ) ) {
962 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
963 $params[
'selected'] = (int)$params[
'selected'];
967 $params[
'selected'] =
'';
970 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
971 $params[
'disable'] = [];
979 foreach ( $options as $nsId => $nsName ) {
983 'disabled' => in_array( $nsId, $params[
'disable'] ),
985 'selected' => $nsId === $params[
'selected'],
991 if ( !array_key_exists(
'id', $selectAttribs ) ) {
992 $selectAttribs[
'id'] =
'namespace';
995 if ( !array_key_exists(
'name', $selectAttribs ) ) {
996 $selectAttribs[
'name'] =
'namespace';
1000 if ( isset( $params[
'label'] ) ) {
1003 'for' => $selectAttribs[
'id'] ??
null,
1011 . implode(
"\n", $optionsHtml )
1038 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1041 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1044 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1045 $attribs[
"xmlns:$tag"] = $ns;
1048 $ret .=
"<!DOCTYPE html>\n";
1051 if ( $html5Version ) {
1052 $attribs[
'version'] = $html5Version;
1070 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1071 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1097 public static function srcSet( array $urls ) {
1099 foreach ( $urls as $density => $url ) {
1102 $density = (string)(
float)$density;
1103 $candidates[$density] = $url;
1107 ksort( $candidates, SORT_NUMERIC );
1108 $candidates = array_unique( $candidates );
1111 foreach ( $candidates as $density => $url ) {
1112 $candidates[$density] = $url .
' ' . $density .
'x';
1115 return implode(
", ", $candidates );
1119class_alias( Html::class,
'Html' );
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.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
A class containing constants representing the names of configuration variables.
const MimeType
Name constant for the MimeType setting, for use with Config::get()
const UseMediaWikiUIEverywhere
Name constant for the UseMediaWikiUIEverywhere setting, for use with Config::get()
const XhtmlNamespaces
Name constant for the XhtmlNamespaces setting, for use with Config::get()
const Html5Version
Name constant for the Html5Version setting, for use with Config::get()
if(!isset( $args[0])) $lang