32use UnexpectedValueException;
58 private static $voidElements = [
81 private static $boolAttribs = [
90 'formnovalidate' =>
true,
107 'typemustmatch' =>
true,
145 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
163 public static function submitButton( $contents, array $attrs = [], array $modifiers = [] ) {
164 $attrs[
'type'] =
'submit';
165 $attrs[
'value'] = $contents;
191 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
193 if ( isset( self::$voidElements[$element] ) ) {
216 public static function element( $element, $attribs = [], $contents =
'' ) {
220 strtr( $contents ??
'', [
241 $attribs = (array)$attribs;
244 $element = strtolower( $element );
248 if ( str_contains( $element,
' ' ) ) {
249 wfWarn( __METHOD__ .
" given element name with space '$element'" );
253 if ( $element ==
'input' ) {
268 'datetime-local' =>
true,
281 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
282 unset( $attribs[
'type'] );
289 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
290 $attribs[
'type'] =
'submit';
294 self::dropDefaults( $element, $attribs ) ) .
'>';
305 $element = strtolower( $element );
307 return "</$element>";
327 private static function dropDefaults( $element, array $attribs ) {
330 static $attribDefaults = [
331 'area' => [
'shape' =>
'rect' ],
333 'formaction' =>
'GET',
334 'formenctype' =>
'application/x-www-form-urlencoded',
342 'autocomplete' =>
'on',
343 'enctype' =>
'application/x-www-form-urlencoded',
346 'formaction' =>
'GET',
349 'keygen' => [
'keytype' =>
'rsa' ],
350 'link' => [
'media' =>
'all' ],
351 'menu' => [
'type' =>
'list' ],
352 'script' => [
'type' =>
'text/javascript' ],
355 'type' =>
'text/css',
357 'textarea' => [
'wrap' =>
'soft' ],
360 foreach ( $attribs as $attrib => $value ) {
361 if ( $attrib ===
'class' ) {
362 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
363 unset( $attribs[$attrib] );
365 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
366 if ( is_array( $value ) ) {
367 $value = implode(
' ', $value );
369 $value = strval( $value );
371 if ( $attribDefaults[$element][$attrib] == $value ) {
372 unset( $attribs[$attrib] );
378 if ( $element ===
'link'
379 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
381 unset( $attribs[
'type'] );
383 if ( $element ===
'input' ) {
384 $type = $attribs[
'type'] ??
null;
385 $value = $attribs[
'value'] ??
null;
386 if ( $type ===
'checkbox' || $type ===
'radio' ) {
390 if ( $value ===
'on' ) {
391 unset( $attribs[
'value'] );
393 } elseif ( $type ===
'submit' ) {
401 if ( $value ===
'' ) {
402 unset( $attribs[
'value'] );
406 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
407 $multiple = ( $attribs[
'multiple'] ?? false ) !==
false ||
408 in_array(
'multiple', $attribs );
409 $default = $multiple ? 4 : 1;
410 if ( (
int)$attribs[
'size'] === $default ) {
411 unset( $attribs[
'size'] );
458 foreach ( $attribs as $key => $value ) {
460 if ( $value ===
false || $value ===
null ) {
466 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
472 $key = strtolower( $key );
476 $spaceSeparatedListAttributes = [
486 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
491 if ( is_array( $value ) ) {
494 foreach ( $value as $k => $v ) {
495 if ( is_string( $v ) ) {
498 if ( !isset( $value[$v] ) ) {
502 foreach ( explode(
' ', $v ) as $part ) {
505 if ( $part !==
'' && $part !==
' ' ) {
506 $arrayValue[] = $part;
517 $arrayValue = explode(
' ', $value );
520 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
524 $value = implode(
' ', array_unique( $arrayValue ) );
531 } elseif ( is_array( $value ) ) {
532 throw new UnexpectedValueException(
"HTML attribute $key can not contain a list of values" );
535 if ( isset( self::$boolAttribs[$key] ) ) {
536 $ret .=
" $key=\"\"";
541 $encValue = htmlspecialchars( $value, ENT_QUOTES );
545 $encValue = strtr( $encValue, [
550 $ret .=
" $key=\"$encValue\"";
570 if ( preg_match(
'/<\/?script/i', $contents ) ) {
571 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
572 $contents =
'/* ERROR: Invalid script */';
587 $attrs = [
'src' =>
$url ];
588 if ( $nonce !==
null ) {
589 $attrs[
'nonce'] = $nonce;
591 wfWarn(
"no nonce set on script. CSP will break it" );
609 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
614 $contents = strtr( $contents, [
618 ']]>' =>
'\5D\5D\3E '
621 if ( preg_match(
'/[<&]/', $contents ) ) {
622 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
627 ] + $attribs, $contents );
640 'rel' =>
'stylesheet',
657 public static function input( $name, $value =
'', $type =
'text', array $attribs = [] ) {
658 $attribs[
'type'] = $type;
659 $attribs[
'value'] = $value;
660 $attribs[
'name'] = $name;
672 public static function check( $name, $checked =
false, array $attribs = [] ) {
673 if ( isset( $attribs[
'value'] ) ) {
674 $value = $attribs[
'value'];
675 unset( $attribs[
'value'] );
681 $attribs[] =
'checked';
684 return self::input( $name, $value,
'checkbox', $attribs );
697 private static function messageBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
698 if ( $heading !==
'' ) {
705 if ( is_array( $className ) ) {
706 $className = array_merge(
711 $className .=
' ' . implode(
' ', $coreClasses );
714 self::element(
'span', [
'class' => [
718 self::rawElement(
'div', [
719 'class' =>
'cdx-message__content'
734 public static function noticeBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
735 return self::messageBox( $html, [
736 'cdx-message--notice',
738 ], $heading, $iconClassName );
750 public static function warningBox( $html, $className =
'' ) {
751 return self::messageBox( $html, [
752 'cdx-message--warning', $className ] );
765 public static function errorBox( $html, $heading =
'', $className =
'' ) {
766 return self::messageBox( $html, [
767 'cdx-message--error', $className ], $heading );
779 public static function successBox( $html, $className =
'' ) {
780 return self::messageBox( $html, [
781 'cdx-message--success', $className ] );
792 public static function radio( $name, $checked =
false, array $attribs = [] ) {
793 if ( isset( $attribs[
'value'] ) ) {
794 $value = $attribs[
'value'];
795 unset( $attribs[
'value'] );
801 $attribs[] =
'checked';
804 return self::input( $name, $value,
'radio', $attribs );
815 public static function label( $label, $id, array $attribs = [] ) {
831 public static function hidden( $name, $value, array $attribs = [] ) {
832 return self::input( $name, $value,
'hidden', $attribs );
847 public static function textarea( $name, $value =
'', array $attribs = [] ) {
848 $attribs[
'name'] = $name;
850 if ( substr( $value, 0, 1 ) ==
"\n" ) {
855 $spacedValue =
"\n" . $value;
857 $spacedValue = $value;
868 if ( !isset(
$params[
'exclude'] ) || !is_array(
$params[
'exclude'] ) ) {
872 if (
$params[
'in-user-lang'] ??
false ) {
880 if ( isset(
$params[
'all'] ) ) {
886 $options = $lang->getFormattedNamespaces();
888 foreach ( $options as $nsId => $nsName ) {
894 is_array(
$params[
'include'] ) &&
895 !in_array( $nsId,
$params[
'include'] )
903 $nsName =
wfMessage(
'blanknamespace' )->text();
904 } elseif ( is_int( $nsId ) ) {
906 ->getLanguageConverter( $lang );
907 $nsName = $converter->convertNamespace( $nsId );
909 $optionsOut[$nsId] = $nsName;
933 array $selectAttribs = []
935 ksort( $selectAttribs );
938 if ( isset(
$params[
'selected'] ) ) {
941 if ( !is_int(
$params[
'selected'] ) && ctype_digit( (
string)
$params[
'selected'] ) ) {
949 if ( !isset(
$params[
'disable'] ) || !is_array(
$params[
'disable'] ) ) {
958 foreach ( $options as $nsId => $nsName ) {
962 'disabled' => in_array( $nsId,
$params[
'disable'] ),
964 'selected' => $nsId ===
$params[
'selected'],
970 $selectAttribs[
'id'] ??=
'namespace';
971 $selectAttribs[
'name'] ??=
'namespace';
974 if ( isset(
$params[
'label'] ) ) {
977 'for' => $selectAttribs[
'id'],
985 . implode(
"\n", $optionsHtml )
1012 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1015 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1018 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1019 $attribs[
"xmlns:$tag"] = $ns;
1022 $ret .=
"<!DOCTYPE html>\n";
1025 if ( $html5Version ) {
1026 $attribs[
'version'] = $html5Version;
1044 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1045 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1071 public static function srcSet( array $urls ) {
1073 foreach ( $urls as $density =>
$url ) {
1076 $density = (string)(
float)$density;
1077 $candidates[$density] =
$url;
1081 ksort( $candidates, SORT_NUMERIC );
1082 $candidates = array_unique( $candidates );
1085 foreach ( $candidates as $density =>
$url ) {
1086 $candidates[$density] =
$url .
' ' . $density .
'x';
1089 return implode(
", ", $candidates );
1108 return $value->value;
1110 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
1129 if ( $encodedArgs ===
false ) {
1132 return "$name($encodedArgs);";
1145 foreach ( $args as &$arg ) {
1147 if ( $arg ===
false ) {
1152 return ' ' . implode(
', ', $args ) .
' ';
1154 return implode(
',', $args );
1174 if ( isset(
$params[
'other'] ) ) {
1175 $options[
$params[
'other'] ] =
'other';
1179 foreach ( explode(
"\n", $list ) as $option ) {
1180 $value = trim( $option );
1181 if ( $value ==
'' ) {
1184 if ( substr( $value, 0, 1 ) ==
'*' && substr( $value, 1, 1 ) !=
'*' ) {
1185 # A new group is starting...
1186 $value = trim( substr( $value, 1 ) );
1187 if ( $value !==
'' &&
1195 } elseif ( substr( $value, 0, 2 ) ==
'**' ) {
1197 $opt = trim( substr( $value, 2 ) );
1198 if ( $optgroup ===
false ) {
1199 $options[$opt] = $opt;
1201 $options[$optgroup][$opt] = $opt;
1204 # groupless reason list
1206 $options[$option] = $option;
1224 foreach ( $options as $text => $value ) {
1225 if ( is_array( $value ) ) {
1226 $optionsOoui[] = [
'optgroup' => (string)$text ];
1227 foreach ( $value as $text2 => $value2 ) {
1228 $optionsOoui[] = [
'data' => (string)$value2,
'label' => (
string)$text2 ];
1231 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
1235 return $optionsOoui;
1249 foreach ( $options as $text => $value ) {
1250 if ( is_array( $value ) ) {
1252 $optionsCodex[] = [
'label' => (string)$text,
'value' =>
'',
'disabled' =>
true ];
1253 foreach ( $value as $text2 => $value2 ) {
1254 $optionsCodex[] = [
'label' => (string)$text2,
'value' => (
string)$value2 ];
1257 $optionsCodex[] = [
'label' => (string)$text,
'value' => (
string)$value ];
1260 return $optionsCodex;
1265class_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.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
array $params
The job parameters.
A class containing constants representing the names of configuration variables.
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()
Handle sending Content-Security-Policy headers.