12namespace MediaWiki\Html;
14use MediaWiki\Context\RequestContext;
15use MediaWiki\Json\FormatJson;
16use MediaWiki\MainConfigNames;
17use MediaWiki\MediaWikiServices;
18use MediaWiki\Parser\Sanitizer;
19use MediaWiki\Request\ContentSecurityPolicy;
20use UnexpectedValueException;
46 private const VOID_ELEMENTS = [
68 private const BOOL_ATTRIBS = [
77 'formnovalidate' =>
true,
94 'typemustmatch' =>
true,
101 private const ATTRIBS_DEFAULTS = [
102 'area' => [
'shape' =>
'rect' ],
104 'formaction' =>
'GET',
105 'formenctype' =>
'application/x-www-form-urlencoded',
113 'autocomplete' =>
'on',
114 'enctype' =>
'application/x-www-form-urlencoded',
117 'formaction' =>
'GET',
120 'keygen' => [
'keytype' =>
'rsa' ],
123 'type' =>
'text/css',
125 'menu' => [
'type' =>
'list' ],
126 'script' => [
'type' =>
'text/javascript' ],
129 'type' =>
'text/css',
131 'textarea' => [
'wrap' =>
'soft' ],
138 private const SPACE_SEPARATED_LIST_ATTRIBUTES = [
147 private const INPUT_ELEMENT_VALID_TYPES = [
161 'datetime-local' =>
true,
185 public static function addClass( &$classes,
string $class ): void {
186 $classes = (array)$classes;
188 foreach ( $classes as $key => $val ) {
190 ( is_int( $key ) && is_string( $val ) ) ||
191 ( is_string( $key ) && is_bool( $val ) )
196 wfWarn( __METHOD__ .
": Argument doesn't look like a class array: " . var_export( $classes,
true ) );
212 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
230 public static function submitButton( $contents, array $attrs = [], array $modifiers = [] ) {
231 $attrs[
'type'] =
'submit';
232 $attrs[
'value'] = $contents;
258 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
259 $start = self::openElement( $element, $attribs );
260 if ( isset( self::VOID_ELEMENTS[$element] ) ) {
263 $contents = Sanitizer::escapeCombiningChar( $contents ??
'' );
264 return $start . $contents . self::closeElement( $element );
284 public static function element( $element, $attribs = [], $contents =
'' ) {
285 return self::rawElement(
288 strtr( $contents ??
'', [
309 $attribs = (array)$attribs;
312 $element = strtolower( $element );
316 if ( str_contains( $element,
' ' ) ) {
317 wfWarn( __METHOD__ .
" given element name with space '$element'" );
321 if ( $element ==
'input' ) {
322 if ( isset( $attribs[
'type'] ) && !isset( self::INPUT_ELEMENT_VALID_TYPES[$attribs[
'type']] ) ) {
323 unset( $attribs[
'type'] );
330 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
331 $attribs[
'type'] =
'submit';
334 return "<$element" . self::expandAttributes(
335 self::dropDefaults( $element, $attribs ) ) .
'>';
346 $element = strtolower( $element );
348 return "</$element>";
368 private static function dropDefaults( $element, array $attribs ) {
369 foreach ( $attribs as $attrib => $value ) {
370 if ( $attrib ===
'class' ) {
371 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
372 unset( $attribs[$attrib] );
374 } elseif ( isset( self::ATTRIBS_DEFAULTS[$element][$attrib] ) ) {
375 if ( is_array( $value ) ) {
376 $value = implode(
' ', $value );
380 if ( self::ATTRIBS_DEFAULTS[$element][$attrib] ==
$value ) {
381 unset( $attribs[$attrib] );
387 if ( $element ===
'input' ) {
388 $type = $attribs[
'type'] ??
null;
389 $value = $attribs[
'value'] ??
null;
390 if (
$type ===
'checkbox' ||
$type ===
'radio' ) {
395 unset( $attribs[
'value'] );
397 } elseif (
$type ===
'submit' ) {
406 unset( $attribs[
'value'] );
410 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
411 $multiple = ( $attribs[
'multiple'] ?? false ) !==
false ||
412 in_array(
'multiple', $attribs );
413 $default = $multiple ? 4 : 1;
414 if ( (
int)$attribs[
'size'] === $default ) {
415 unset( $attribs[
'size'] );
435 if ( is_array( $classes ) ) {
438 foreach ( $classes as $k => $v ) {
439 if ( is_string( $v ) ) {
442 if ( !isset( $classes[$v] ) ) {
446 foreach ( explode(
' ', $v ) as $part ) {
449 if ( $part !==
'' && $part !==
' ' ) {
450 $arrayValue[] = $part;
461 $arrayValue = explode(
' ', $classes );
464 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
468 return implode(
' ', array_unique( $arrayValue ) );
511 foreach ( $attribs as $key => $value ) {
513 if ( $value ===
false || $value ===
null ) {
519 if ( is_int( $key ) && isset( self::BOOL_ATTRIBS[strtolower( $value )] ) ) {
525 $key = strtolower( $key );
528 if ( isset( self::SPACE_SEPARATED_LIST_ATTRIBUTES[$key] ) ) {
530 $value = self::expandClassList( $value );
537 } elseif ( is_array( $value ) ) {
538 throw new UnexpectedValueException(
"HTML attribute $key can not contain a list of values" );
541 if ( isset( self::BOOL_ATTRIBS[$key] ) ) {
542 $ret .=
" $key=\"\"";
547 $encValue = htmlspecialchars( $value, ENT_QUOTES );
551 $encValue = strtr( $encValue, [
556 $ret .=
" $key=\"$encValue\"";
576 if ( preg_match(
'/<\/?script/i', $contents ) ) {
577 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
578 $contents =
'/* ERROR: Invalid script */';
581 return self::rawElement(
'script', [], $contents );
593 $attrs = [
'src' =>
$url ];
594 if ( $nonce !==
null ) {
595 $attrs[
'nonce'] = $nonce;
597 wfWarn(
"no nonce set on script. CSP will break it" );
600 return self::element(
'script', $attrs );
615 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
620 $contents = strtr( $contents, [
624 ']]>' =>
'\5D\5D\3E '
627 if ( preg_match(
'/[<&]/', $contents ) ) {
628 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
631 return self::rawElement(
'style', [
633 ] + $attribs, $contents );
645 return self::element(
'link', [
646 'rel' =>
'stylesheet',
663 public static function input( $name, $value =
'', $type =
'text', array $attribs = [] ) {
664 $attribs[
'type'] = $type;
665 $attribs[
'value'] = $value;
666 $attribs[
'name'] = $name;
667 return self::element(
'input', $attribs );
678 public static function check( $name, $checked =
false, array $attribs = [] ) {
679 $value = $attribs[
'value'] ?? 1;
680 unset( $attribs[
'value'] );
681 return self::element(
'input', [
683 'checked' => (
bool)$checked,
684 'type' =>
'checkbox',
701 private static function messageBox( $html, $className, $heading =
'', $iconClassName =
'', array $attribs = [] ) {
702 if ( $heading !==
'' ) {
703 $html = self::element(
'h2', [], $heading ) . $html;
705 self::addClass( $className,
'cdx-message' );
706 self::addClass( $className,
'cdx-message--block' );
707 return self::rawElement(
'div', array_merge( [
'class' => $className ], $attribs ),
708 self::element(
'span', [
'class' => [
712 self::rawElement(
'div', [
713 'class' =>
'cdx-message__content'
733 public static function noticeBox( $html, $className =
'', $heading =
'', $iconClassName =
'' ) {
734 return self::messageBox( $html, [
735 'cdx-message--notice',
737 ], $heading, $iconClassName );
754 public static function warningBox( $html, $className =
'' ) {
755 return self::messageBox( $html, [
756 'cdx-message--warning', $className ],
'',
'', [
'aria-live' =>
'polite' ] );
774 public static function errorBox( $html, $heading =
'', $className =
'' ) {
775 return self::messageBox( $html, [
776 'cdx-message--error', $className ], $heading,
'', [
'role' =>
'alert' ] );
793 public static function successBox( $html, $className =
'' ) {
794 return self::messageBox( $html, [
795 'cdx-message--success', $className ] );
806 public static function radio( $name, $checked =
false, array $attribs = [] ) {
807 $value = $attribs[
'value'] ?? 1;
808 unset( $attribs[
'value'] );
809 return self::element(
'input', [
811 'checked' => (
bool)$checked,
826 public static function label( $label, $id, array $attribs = [] ) {
830 return self::element(
'label', $attribs, $label );
842 public static function hidden( $name, $value, array $attribs = [] ) {
843 return self::element(
'input', [
863 public static function textarea( $name, $value =
'', array $attribs = [] ) {
864 $attribs[
'name'] = $name;
866 if ( str_starts_with( $value ??
'',
"\n" ) ) {
871 $spacedValue =
"\n" . $value;
873 $spacedValue = $value;
875 return self::element(
'textarea', $attribs, $spacedValue );
884 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
885 $params[
'exclude'] = [];
888 if ( $params[
'in-user-lang'] ??
false ) {
889 $lang = RequestContext::getMain()->getLanguage();
895 if ( isset( $params[
'all'] ) ) {
898 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
901 $options = $lang->getFormattedNamespaces();
903 foreach ( $options as $nsId => $nsName ) {
904 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
908 isset( $params[
'include'] ) &&
909 is_array( $params[
'include'] ) &&
910 !in_array( $nsId, $params[
'include'] )
918 $nsName =
wfMessage(
'blanknamespace' )->text();
919 } elseif ( is_int( $nsId ) ) {
921 ->getLanguageConverter( $lang );
922 $nsName = $converter->convertNamespace( $nsId );
924 $optionsOut[$nsId] = $nsName;
948 array $selectAttribs = []
950 ksort( $selectAttribs );
953 if ( isset( $params[
'selected'] ) ) {
956 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
957 $params[
'selected'] = (int)$params[
'selected'];
961 $params[
'selected'] =
'';
964 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
965 $params[
'disable'] = [];
969 $options = self::namespaceSelectorOptions( $params );
973 foreach ( $options as $nsId => $nsName ) {
974 $optionsHtml[] = self::element(
977 'disabled' => in_array( $nsId, $params[
'disable'] ),
979 'selected' => $nsId === $params[
'selected'],
985 $selectAttribs[
'id'] ??=
'namespace';
986 $selectAttribs[
'name'] ??=
'namespace';
989 if ( isset( $params[
'label'] ) ) {
990 $label = self::element(
'label', [
'for' => $selectAttribs[
'id'] ],
996 return $label . self::rawElement(
'select', $selectAttribs,
997 "\n" . implode(
"\n", $optionsHtml ) .
"\n"
1016 $isXHTML = self::isXmlMimeType( $mimeType );
1021 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1024 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1027 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1028 $attribs[
"xmlns:$tag"] = $ns;
1031 $ret .=
"<!DOCTYPE html>\n";
1034 if ( $html5Version ) {
1035 $attribs[
'version'] = $html5Version;
1038 $ret .= self::openElement(
'html', $attribs );
1053 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1054 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1079 public static function srcSet( array $urls ) {
1081 foreach ( $urls as $density =>
$url ) {
1084 $density = (string)(
float)$density;
1085 $candidates[$density] =
$url;
1089 ksort( $candidates, SORT_NUMERIC );
1090 $candidates = array_unique( $candidates );
1093 foreach ( $candidates as $density =>
$url ) {
1094 $candidates[$density] =
$url .
' ' . $density .
'x';
1097 return implode(
", ", $candidates );
1116 return $value->value;
1118 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
1136 $encodedArgs = self::encodeJsList( $args, $pretty );
1137 if ( $encodedArgs ===
false ) {
1140 return "$name($encodedArgs);";
1153 foreach ( $args as &$arg ) {
1154 $arg = self::encodeJsVar( $arg, $pretty );
1155 if ( $arg ===
false ) {
1160 return ' ' . implode(
', ', $args ) .
' ';
1162 return implode(
',', $args );
1182 if ( isset( $params[
'other'] ) ) {
1183 $options[ $params[
'other'] ] =
'other';
1187 foreach ( explode(
"\n", $list ) as $option ) {
1188 $value = trim( $option );
1189 if ( $value ==
'' ) {
1192 if ( str_starts_with( $value,
'*' ) && !str_starts_with( $value,
'**' ) ) {
1193 # A new group is starting...
1194 $value = trim( substr( $value, 1 ) );
1195 if ( $value !==
'' &&
1197 ( !isset( $params[
'other'] ) || $value !== $params[
'other'] )
1203 } elseif ( str_starts_with( $value,
'**' ) ) {
1205 $opt = trim( substr( $value, 2 ) );
1206 if ( $optgroup ===
false ) {
1207 $options[$opt] = $opt;
1209 $options[$optgroup][$opt] = $opt;
1212 # groupless reason list
1214 $options[$option] = $option;
1232 foreach ( $options as $text => $value ) {
1233 if ( is_array( $value ) ) {
1234 $optionsOoui[] = [
'optgroup' => (string)$text ];
1235 foreach ( $value as $text2 => $value2 ) {
1236 $optionsOoui[] = [
'data' => (string)$value2,
'label' => (
string)$text2 ];
1239 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
1243 return $optionsOoui;
1257 foreach ( $options as $text => $value ) {
1258 if ( is_array( $value ) ) {
1260 'label' => (string)$text,
1261 'items' => array_map(
static function ( $text2, $value2 ) {
1262 return [
'label' => (string)$text2,
'value' => (
string)$value2 ];
1263 }, array_keys( $value ), $value )
1266 $optionsCodex[] = [
'label' => (string)$text,
'value' => (
string)$value ];
1269 return $optionsCodex;
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_SETUP_CALLBACK'))
const MimeType
Name constant for the MimeType 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().