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] ) ) {
240 public static function element( $element, $attribs = [], $contents =
'' ) {
244 strtr( $contents ??
'', [
265 $attribs = (array)$attribs;
268 $element = strtolower( $element );
272 if ( strpos( $element,
' ' ) !==
false ) {
273 wfWarn( __METHOD__ .
" given element name with space '$element'" );
277 if ( $element ==
'input' ) {
292 'datetime-local' =>
true,
305 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
306 unset( $attribs[
'type'] );
313 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
314 $attribs[
'type'] =
'submit';
318 self::dropDefaults( $element, $attribs ) ) .
'>';
329 $element = strtolower( $element );
331 return "</$element>";
351 private static function dropDefaults( $element, array $attribs ) {
354 static $attribDefaults = [
355 'area' => [
'shape' =>
'rect' ],
357 'formaction' =>
'GET',
358 'formenctype' =>
'application/x-www-form-urlencoded',
366 'autocomplete' =>
'on',
367 'enctype' =>
'application/x-www-form-urlencoded',
370 'formaction' =>
'GET',
373 'keygen' => [
'keytype' =>
'rsa' ],
374 'link' => [
'media' =>
'all' ],
375 'menu' => [
'type' =>
'list' ],
376 'script' => [
'type' =>
'text/javascript' ],
379 'type' =>
'text/css',
381 'textarea' => [
'wrap' =>
'soft' ],
384 foreach ( $attribs as $attrib => $value ) {
385 if ( $attrib ===
'class' ) {
386 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
387 unset( $attribs[$attrib] );
389 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
390 if ( is_array( $value ) ) {
391 $value = implode(
' ', $value );
393 $value = strval( $value );
395 if ( $attribDefaults[$element][$attrib] == $value ) {
396 unset( $attribs[$attrib] );
402 if ( $element ===
'link'
403 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
405 unset( $attribs[
'type'] );
407 if ( $element ===
'input' ) {
408 $type = $attribs[
'type'] ??
null;
409 $value = $attribs[
'value'] ??
null;
410 if (
$type ===
'checkbox' ||
$type ===
'radio' ) {
414 if ( $value ===
'on' ) {
415 unset( $attribs[
'value'] );
417 } elseif (
$type ===
'submit' ) {
425 if ( $value ===
'' ) {
426 unset( $attribs[
'value'] );
430 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
431 if ( in_array(
'multiple', $attribs )
432 || ( isset( $attribs[
'multiple'] ) && $attribs[
'multiple'] !==
false )
435 if ( strval( $attribs[
'size'] ) ==
'4' ) {
436 unset( $attribs[
'size'] );
440 if ( strval( $attribs[
'size'] ) ==
'1' ) {
441 unset( $attribs[
'size'] );
490 foreach ( $attribs as $key => $value ) {
492 if ( $value ===
false || $value ===
null ) {
498 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
504 $key = strtolower( $key );
508 $spaceSeparatedListAttributes = [
518 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
523 if ( is_array( $value ) ) {
526 foreach ( $value as $k => $v ) {
527 if ( is_string( $v ) ) {
530 if ( !isset( $value[$v] ) ) {
534 foreach ( explode(
' ', $v ) as $part ) {
537 if ( $part !==
'' && $part !==
' ' ) {
538 $arrayValue[] = $part;
549 $arrayValue = explode(
' ', $value );
552 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
556 $value = implode(
' ', array_unique( $arrayValue ) );
563 } elseif ( is_array( $value ) ) {
564 throw new MWException(
"HTML attribute $key can not contain a list of values" );
567 if ( isset( self::$boolAttribs[$key] ) ) {
568 $ret .=
" $key=\"\"";
573 $encValue = htmlspecialchars( $value, ENT_QUOTES );
577 $encValue = strtr( $encValue, [
582 $ret .=
" $key=\"$encValue\"";
603 if ( $nonce !==
null ) {
604 $attrs[
'nonce'] = $nonce;
606 wfWarn(
"no nonce set on script. CSP will break it" );
609 if ( preg_match(
'/<\/?script/i', $contents ) ) {
610 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
611 $contents =
'/* ERROR: Invalid script */';
626 $attrs = [
'src' => $url ];
627 if ( $nonce !==
null ) {
628 $attrs[
'nonce'] = $nonce;
630 wfWarn(
"no nonce set on script. CSP will break it" );
648 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
653 $contents = strtr( $contents, [
657 ']]>' =>
'\5D\5D\3E '
660 if ( preg_match(
'/[<&]/', $contents ) ) {
661 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
666 ] + $attribs, $contents );
679 'rel' =>
'stylesheet',
696 public static function input( $name, $value =
'',
$type =
'text', array $attribs = [] ) {
697 $attribs[
'type'] =
$type;
698 $attribs[
'value'] = $value;
699 $attribs[
'name'] = $name;
700 $textInputAttributes = [
707 if ( isset( $textInputAttributes[
$type] ) ) {
710 $buttonAttributes = [
715 if ( isset( $buttonAttributes[
$type] ) ) {
729 public static function check( $name, $checked =
false, array $attribs = [] ) {
730 if ( isset( $attribs[
'value'] ) ) {
731 $value = $attribs[
'value'];
732 unset( $attribs[
'value'] );
738 $attribs[] =
'checked';
741 return self::input( $name, $value,
'checkbox', $attribs );
752 private static function messageBox( $html, $className, $heading =
'' ) {
753 if ( $heading !==
'' ) {
756 if ( is_array( $className ) ) {
757 $className[] =
'mw-message-box';
759 $className .=
' mw-message-box';
772 return self::messageBox( $html, [
'mw-message-box-notice', $className ] );
783 public static function warningBox( $html, $className =
'' ) {
784 return self::messageBox( $html, [
'mw-message-box-warning', $className ] );
796 public static function errorBox( $html, $heading =
'', $className =
'' ) {
797 return self::messageBox( $html, [
'mw-message-box-error', $className ], $heading );
808 public static function successBox( $html, $className =
'' ) {
809 return self::messageBox( $html, [
'mw-message-box-success', $className ] );
820 public static function radio( $name, $checked =
false, array $attribs = [] ) {
821 if ( isset( $attribs[
'value'] ) ) {
822 $value = $attribs[
'value'];
823 unset( $attribs[
'value'] );
829 $attribs[] =
'checked';
832 return self::input( $name, $value,
'radio', $attribs );
843 public static function label( $label, $id, array $attribs = [] ) {
859 public static function hidden( $name, $value, array $attribs = [] ) {
860 return self::input( $name, $value,
'hidden', $attribs );
875 public static function textarea( $name, $value =
'', array $attribs = [] ) {
876 $attribs[
'name'] = $name;
878 if ( substr( $value, 0, 1 ) ==
"\n" ) {
883 $spacedValue =
"\n" . $value;
885 $spacedValue = $value;
887 return self::element(
'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
896 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
897 $params[
'exclude'] = [];
900 if ( $params[
'in-user-lang'] ??
false ) {
908 if ( isset( $params[
'all'] ) ) {
911 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
914 $options =
$lang->getFormattedNamespaces();
916 foreach ( $options as $nsId => $nsName ) {
917 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
923 $nsName =
wfMessage(
'blanknamespace' )->text();
924 } elseif ( is_int( $nsId ) ) {
926 ->getLanguageConverter(
$lang );
927 $nsName = $converter->convertNamespace( $nsId );
929 $optionsOut[$nsId] = $nsName;
953 array $selectAttribs = []
955 ksort( $selectAttribs );
958 if ( isset( $params[
'selected'] ) ) {
961 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
962 $params[
'selected'] = (int)$params[
'selected'];
966 $params[
'selected'] =
'';
969 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
970 $params[
'disable'] = [];
978 foreach ( $options as $nsId => $nsName ) {
982 'disabled' => in_array( $nsId, $params[
'disable'] ),
984 'selected' => $nsId === $params[
'selected'],
990 if ( !array_key_exists(
'id', $selectAttribs ) ) {
991 $selectAttribs[
'id'] =
'namespace';
994 if ( !array_key_exists(
'name', $selectAttribs ) ) {
995 $selectAttribs[
'name'] =
'namespace';
999 if ( isset( $params[
'label'] ) ) {
1002 'for' => $selectAttribs[
'id'] ??
null,
1010 . implode(
"\n", $optionsHtml )
1037 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1040 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1043 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1044 $attribs[
"xmlns:$tag"] = $ns;
1047 $ret .=
"<!DOCTYPE html>\n";
1050 if ( $html5Version ) {
1051 $attribs[
'version'] = $html5Version;
1069 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1070 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1096 public static function srcSet( array $urls ) {
1098 foreach ( $urls as $density => $url ) {
1101 $density = (string)(
float)$density;
1102 $candidates[$density] = $url;
1106 ksort( $candidates, SORT_NUMERIC );
1107 $candidates = array_unique( $candidates );
1110 foreach ( $candidates as $density => $url ) {
1111 $candidates[$density] = $url .
' ' . $density .
'x';
1114 return implode(
", ", $candidates );
1118 class_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()
static isNonceRequired(Config $config)
Should we set nonce attribute.
if(!isset( $args[0])) $lang