29use InvalidArgumentException;
59 private static $voidElements = [
82 private static $boolAttribs = [
91 'formnovalidate' =>
true,
108 'typemustmatch' =>
true,
120 $useMediaWikiUIEverywhere =
122 if ( $useMediaWikiUIEverywhere ) {
123 if ( isset( $attrs[
'class'] ) ) {
124 if ( is_array( $attrs[
'class'] ) ) {
125 $attrs[
'class'][] =
'mw-ui-button';
126 $attrs[
'class'] = array_merge( $attrs[
'class'], $modifiers );
128 $attrs[
'class'] = implode(
' ', $attrs[
'class'] );
130 $attrs[
'class'] .=
' mw-ui-button ' . implode(
' ', $modifiers );
134 $attrs[
'class'] =
'mw-ui-button ' . implode(
' ', $modifiers );
150 if ( $useMediaWikiUIEverywhere ) {
151 $cdxInputClass =
'cdx-text-input__input';
154 if ( isset( $attrs[
'class'] ) ) {
155 if ( is_array( $attrs[
'class'] ) ) {
157 !in_array( $cdxInputClass, $attrs[
'class'],
true ) &&
158 !( $attrs[
'class'][$cdxInputClass] ??
false )
160 $attrs[
'class'][
'mw-ui-input'] =
true;
162 } elseif ( is_string( $attrs[
'class'] ) ) {
163 if ( !preg_match(
"/(^| )$cdxInputClass($| )/", $attrs[
'class'] ) ) {
164 $attrs[
'class'] .=
' mw-ui-input';
167 throw new InvalidArgumentException(
168 'Unexpected class attr of type ' . gettype( $attrs[
'class'] )
172 $attrs[
'class'] =
'mw-ui-input';
190 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
193 self::buttonAttributes( $attrs, $modifiers ),
211 public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
212 $attrs[
'type'] =
'submit';
213 $attrs[
'value'] = $contents;
214 return self::element(
'input', self::buttonAttributes( $attrs, $modifiers ) );
239 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
241 if ( isset( self::$voidElements[$element] ) ) {
264 public static function element( $element, $attribs = [], $contents =
'' ) {
268 strtr( $contents ??
'', [
289 $attribs = (array)$attribs;
292 $element = strtolower( $element );
296 if ( str_contains( $element,
' ' ) ) {
297 wfWarn( __METHOD__ .
" given element name with space '$element'" );
301 if ( $element ==
'input' ) {
316 'datetime-local' =>
true,
329 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
330 unset( $attribs[
'type'] );
337 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
338 $attribs[
'type'] =
'submit';
342 self::dropDefaults( $element, $attribs ) ) .
'>';
353 $element = strtolower( $element );
355 return "</$element>";
375 private static function dropDefaults( $element, array $attribs ) {
378 static $attribDefaults = [
379 'area' => [
'shape' =>
'rect' ],
381 'formaction' =>
'GET',
382 'formenctype' =>
'application/x-www-form-urlencoded',
390 'autocomplete' =>
'on',
391 'enctype' =>
'application/x-www-form-urlencoded',
394 'formaction' =>
'GET',
397 'keygen' => [
'keytype' =>
'rsa' ],
398 'link' => [
'media' =>
'all' ],
399 'menu' => [
'type' =>
'list' ],
400 'script' => [
'type' =>
'text/javascript' ],
403 'type' =>
'text/css',
405 'textarea' => [
'wrap' =>
'soft' ],
408 foreach ( $attribs as $attrib => $value ) {
409 if ( $attrib ===
'class' ) {
410 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
411 unset( $attribs[$attrib] );
413 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
414 if ( is_array( $value ) ) {
415 $value = implode(
' ', $value );
417 $value = strval( $value );
419 if ( $attribDefaults[$element][$attrib] == $value ) {
420 unset( $attribs[$attrib] );
426 if ( $element ===
'link'
427 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
429 unset( $attribs[
'type'] );
431 if ( $element ===
'input' ) {
432 $type = $attribs[
'type'] ??
null;
433 $value = $attribs[
'value'] ??
null;
434 if ( $type ===
'checkbox' || $type ===
'radio' ) {
438 if ( $value ===
'on' ) {
439 unset( $attribs[
'value'] );
441 } elseif ( $type ===
'submit' ) {
449 if ( $value ===
'' ) {
450 unset( $attribs[
'value'] );
454 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
455 if ( in_array(
'multiple', $attribs )
456 || ( isset( $attribs[
'multiple'] ) && $attribs[
'multiple'] !==
false )
459 if ( strval( $attribs[
'size'] ) ==
'4' ) {
460 unset( $attribs[
'size'] );
464 if ( strval( $attribs[
'size'] ) ==
'1' ) {
465 unset( $attribs[
'size'] );
514 foreach ( $attribs as $key => $value ) {
516 if ( $value ===
false || $value ===
null ) {
522 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
528 $key = strtolower( $key );
532 $spaceSeparatedListAttributes = [
542 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
547 if ( is_array( $value ) ) {
550 foreach ( $value as $k => $v ) {
551 if ( is_string( $v ) ) {
554 if ( !isset( $value[$v] ) ) {
558 foreach ( explode(
' ', $v ) as $part ) {
561 if ( $part !==
'' && $part !==
' ' ) {
562 $arrayValue[] = $part;
573 $arrayValue = explode(
' ', $value );
576 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
580 $value = implode(
' ', array_unique( $arrayValue ) );
587 } elseif ( is_array( $value ) ) {
588 throw new MWException(
"HTML attribute $key can not contain a list of values" );
591 if ( isset( self::$boolAttribs[$key] ) ) {
592 $ret .=
" $key=\"\"";
597 $encValue = htmlspecialchars( $value, ENT_QUOTES );
601 $encValue = strtr( $encValue, [
606 $ret .=
" $key=\"$encValue\"";
626 if ( preg_match(
'/<\/?script/i', $contents ) ) {
627 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
628 $contents =
'/* ERROR: Invalid script */';
643 $attrs = [
'src' => $url ];
644 if ( $nonce !==
null ) {
645 $attrs[
'nonce'] = $nonce;
647 wfWarn(
"no nonce set on script. CSP will break it" );
665 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
670 $contents = strtr( $contents, [
674 ']]>' =>
'\5D\5D\3E '
677 if ( preg_match(
'/[<&]/', $contents ) ) {
678 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
683 ] + $attribs, $contents );
696 'rel' =>
'stylesheet',
713 public static function input( $name, $value =
'', $type =
'text', array $attribs = [] ) {
714 $attribs[
'type'] = $type;
715 $attribs[
'value'] = $value;
716 $attribs[
'name'] = $name;
717 $textInputAttributes = [
724 if ( isset( $textInputAttributes[$type] ) ) {
727 $buttonAttributes = [
732 if ( isset( $buttonAttributes[$type] ) ) {
746 public static function check( $name, $checked =
false, array $attribs = [] ) {
747 if ( isset( $attribs[
'value'] ) ) {
748 $value = $attribs[
'value'];
749 unset( $attribs[
'value'] );
755 $attribs[] =
'checked';
758 return self::input( $name, $value,
'checkbox', $attribs );
770 private static function messageBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
771 if ( $heading !==
'' ) {
779 if ( is_array( $className ) ) {
780 $className = array_merge(
785 $className .=
' ' . implode(
' ', $coreClasses );
788 self::element(
'span', [
'class' => [
792 self::rawElement(
'div', [
793 'class' =>
'cdx-message__content'
807 public static function noticeBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
808 return self::messageBox( $html, [
809 'mw-message-box-notice',
810 'cdx-message--notice',
812 ], $heading, $iconClassName );
823 public static function warningBox( $html, $className =
'' ) {
824 return self::messageBox( $html, [
825 'mw-message-box-warning',
826 'cdx-message--warning', $className ] );
838 public static function errorBox( $html, $heading =
'', $className =
'' ) {
839 return self::messageBox( $html, [
840 'mw-message-box-error',
841 'cdx-message--error', $className ], $heading );
852 public static function successBox( $html, $className =
'' ) {
853 return self::messageBox( $html, [
854 'mw-message-box-success',
855 'cdx-message--success', $className ] );
866 public static function radio( $name, $checked =
false, array $attribs = [] ) {
867 if ( isset( $attribs[
'value'] ) ) {
868 $value = $attribs[
'value'];
869 unset( $attribs[
'value'] );
875 $attribs[] =
'checked';
878 return self::input( $name, $value,
'radio', $attribs );
889 public static function label( $label, $id, array $attribs = [] ) {
905 public static function hidden( $name, $value, array $attribs = [] ) {
906 return self::input( $name, $value,
'hidden', $attribs );
921 public static function textarea( $name, $value =
'', array $attribs = [] ) {
922 $attribs[
'name'] = $name;
924 if ( substr( $value, 0, 1 ) ==
"\n" ) {
929 $spacedValue =
"\n" . $value;
931 $spacedValue = $value;
933 return self::element(
'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
942 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
943 $params[
'exclude'] = [];
946 if ( $params[
'in-user-lang'] ??
false ) {
954 if ( isset( $params[
'all'] ) ) {
957 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
960 $options = $lang->getFormattedNamespaces();
962 foreach ( $options as $nsId => $nsName ) {
963 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
969 $nsName =
wfMessage(
'blanknamespace' )->text();
970 } elseif ( is_int( $nsId ) ) {
972 ->getLanguageConverter( $lang );
973 $nsName = $converter->convertNamespace( $nsId );
975 $optionsOut[$nsId] = $nsName;
999 array $selectAttribs = []
1001 ksort( $selectAttribs );
1004 if ( isset( $params[
'selected'] ) ) {
1007 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
1008 $params[
'selected'] = (int)$params[
'selected'];
1012 $params[
'selected'] =
'';
1015 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
1016 $params[
'disable'] = [];
1024 foreach ( $options as $nsId => $nsName ) {
1028 'disabled' => in_array( $nsId, $params[
'disable'] ),
1030 'selected' => $nsId === $params[
'selected'],
1036 if ( !array_key_exists(
'id', $selectAttribs ) ) {
1037 $selectAttribs[
'id'] =
'namespace';
1040 if ( !array_key_exists(
'name', $selectAttribs ) ) {
1041 $selectAttribs[
'name'] =
'namespace';
1045 if ( isset( $params[
'label'] ) ) {
1048 'for' => $selectAttribs[
'id'] ??
null,
1056 . implode(
"\n", $optionsHtml )
1083 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1086 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1089 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1090 $attribs[
"xmlns:$tag"] = $ns;
1093 $ret .=
"<!DOCTYPE html>\n";
1096 if ( $html5Version ) {
1097 $attribs[
'version'] = $html5Version;
1115 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1116 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1142 public static function srcSet( array $urls ) {
1144 foreach ( $urls as $density => $url ) {
1147 $density = (string)(
float)$density;
1148 $candidates[$density] = $url;
1152 ksort( $candidates, SORT_NUMERIC );
1153 $candidates = array_unique( $candidates );
1156 foreach ( $candidates as $density => $url ) {
1157 $candidates[$density] = $url .
' ' . $density .
'x';
1160 return implode(
", ", $candidates );
1179 return $value->value;
1181 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
1200 if ( $encodedArgs ===
false ) {
1203 return "$name($encodedArgs);";
1216 foreach ( $args as &$arg ) {
1218 if ( $arg ===
false ) {
1223 return ' ' . implode(
', ', $args ) .
' ';
1225 return implode(
',', $args );
1233class_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()
Handle sending Content-Security-Policy headers.