29 use 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 ( strpos( $element,
' ' ) !==
false ) {
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 );
769 private static function messageBox( $html, $className, $heading =
'' ) {
770 if ( $heading !==
'' ) {
778 if ( is_array( $className ) ) {
779 $className = array_merge(
784 $className .=
' ' . implode(
' ', $coreClasses );
787 self::element(
'span', [
'class' =>
'cdx-message__icon' ] ) .
788 self::rawElement(
'div', [
789 'class' =>
'cdx-message__content'
802 return self::messageBox( $html, [
803 'mw-message-box-notice',
804 'cdx-message--notice', $className ] );
815 public static function warningBox( $html, $className =
'' ) {
816 return self::messageBox( $html, [
817 'mw-message-box-warning',
818 'cdx-message--warning', $className ] );
830 public static function errorBox( $html, $heading =
'', $className =
'' ) {
831 return self::messageBox( $html, [
832 'mw-message-box-error',
833 'cdx-message--error', $className ], $heading );
844 public static function successBox( $html, $className =
'' ) {
845 return self::messageBox( $html, [
846 'mw-message-box-success',
847 'cdx-message--success', $className ] );
858 public static function radio( $name, $checked =
false, array $attribs = [] ) {
859 if ( isset( $attribs[
'value'] ) ) {
860 $value = $attribs[
'value'];
861 unset( $attribs[
'value'] );
867 $attribs[] =
'checked';
870 return self::input( $name, $value,
'radio', $attribs );
881 public static function label( $label, $id, array $attribs = [] ) {
897 public static function hidden( $name, $value, array $attribs = [] ) {
898 return self::input( $name, $value,
'hidden', $attribs );
913 public static function textarea( $name, $value =
'', array $attribs = [] ) {
914 $attribs[
'name'] = $name;
916 if ( substr( $value, 0, 1 ) ==
"\n" ) {
921 $spacedValue =
"\n" . $value;
923 $spacedValue = $value;
925 return self::element(
'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
934 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
935 $params[
'exclude'] = [];
938 if ( $params[
'in-user-lang'] ??
false ) {
946 if ( isset( $params[
'all'] ) ) {
949 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
952 $options = $lang->getFormattedNamespaces();
954 foreach ( $options as $nsId => $nsName ) {
955 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
961 $nsName =
wfMessage(
'blanknamespace' )->text();
962 } elseif ( is_int( $nsId ) ) {
964 ->getLanguageConverter( $lang );
965 $nsName = $converter->convertNamespace( $nsId );
967 $optionsOut[$nsId] = $nsName;
991 array $selectAttribs = []
993 ksort( $selectAttribs );
996 if ( isset( $params[
'selected'] ) ) {
999 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
1000 $params[
'selected'] = (int)$params[
'selected'];
1004 $params[
'selected'] =
'';
1007 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
1008 $params[
'disable'] = [];
1016 foreach ( $options as $nsId => $nsName ) {
1020 'disabled' => in_array( $nsId, $params[
'disable'] ),
1022 'selected' => $nsId === $params[
'selected'],
1028 if ( !array_key_exists(
'id', $selectAttribs ) ) {
1029 $selectAttribs[
'id'] =
'namespace';
1032 if ( !array_key_exists(
'name', $selectAttribs ) ) {
1033 $selectAttribs[
'name'] =
'namespace';
1037 if ( isset( $params[
'label'] ) ) {
1040 'for' => $selectAttribs[
'id'] ??
null,
1048 . implode(
"\n", $optionsHtml )
1075 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1078 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1081 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1082 $attribs[
"xmlns:$tag"] = $ns;
1085 $ret .=
"<!DOCTYPE html>\n";
1088 if ( $html5Version ) {
1089 $attribs[
'version'] = $html5Version;
1107 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1108 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1134 public static function srcSet( array $urls ) {
1136 foreach ( $urls as $density => $url ) {
1139 $density = (string)(
float)$density;
1140 $candidates[$density] = $url;
1144 ksort( $candidates, SORT_NUMERIC );
1145 $candidates = array_unique( $candidates );
1148 foreach ( $candidates as $density => $url ) {
1149 $candidates[$density] = $url .
' ' . $density .
'x';
1152 return implode(
", ", $candidates );
1171 return $value->value;
1192 if ( $encodedArgs ===
false ) {
1195 return "$name($encodedArgs);";
1208 foreach ( $args as &$arg ) {
1210 if ( $arg ===
false ) {
1215 return ' ' . implode(
', ', $args ) .
' ';
1217 return implode(
',', $args );
1225 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()
Handle sending Content-Security-Policy headers.
static isNonceRequired(Config $config)
Should we set nonce attribute.