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'
739 public static function noticeBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
740 return self::messageBox( $html, [
741 'cdx-message--notice',
743 ], $heading, $iconClassName );
760 public static function warningBox( $html, $className =
'' ) {
761 return self::messageBox( $html, [
762 'cdx-message--warning', $className ] );
780 public static function errorBox( $html, $heading =
'', $className =
'' ) {
781 return self::messageBox( $html, [
782 'cdx-message--error', $className ], $heading );
799 public static function successBox( $html, $className =
'' ) {
800 return self::messageBox( $html, [
801 'cdx-message--success', $className ] );
812 public static function radio( $name, $checked =
false, array $attribs = [] ) {
813 if ( isset( $attribs[
'value'] ) ) {
814 $value = $attribs[
'value'];
815 unset( $attribs[
'value'] );
821 $attribs[] =
'checked';
824 return self::input( $name, $value,
'radio', $attribs );
835 public static function label( $label, $id, array $attribs = [] ) {
851 public static function hidden( $name, $value, array $attribs = [] ) {
852 return self::input( $name, $value,
'hidden', $attribs );
867 public static function textarea( $name, $value =
'', array $attribs = [] ) {
868 $attribs[
'name'] = $name;
870 if ( substr( $value, 0, 1 ) ==
"\n" ) {
875 $spacedValue =
"\n" . $value;
877 $spacedValue = $value;
888 if ( !isset(
$params[
'exclude'] ) || !is_array(
$params[
'exclude'] ) ) {
892 if (
$params[
'in-user-lang'] ??
false ) {
900 if ( isset(
$params[
'all'] ) ) {
906 $options = $lang->getFormattedNamespaces();
908 foreach ( $options as $nsId => $nsName ) {
914 is_array(
$params[
'include'] ) &&
915 !in_array( $nsId,
$params[
'include'] )
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'] ) ) {
969 if ( !isset(
$params[
'disable'] ) || !is_array(
$params[
'disable'] ) ) {
978 foreach ( $options as $nsId => $nsName ) {
982 'disabled' => in_array( $nsId,
$params[
'disable'] ),
984 'selected' => $nsId ===
$params[
'selected'],
990 $selectAttribs[
'id'] ??=
'namespace';
991 $selectAttribs[
'name'] ??=
'namespace';
994 if ( isset(
$params[
'label'] ) ) {
997 'for' => $selectAttribs[
'id'],
1005 . implode(
"\n", $optionsHtml )
1032 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1035 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1038 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1039 $attribs[
"xmlns:$tag"] = $ns;
1042 $ret .=
"<!DOCTYPE html>\n";
1045 if ( $html5Version ) {
1046 $attribs[
'version'] = $html5Version;
1064 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1065 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1091 public static function srcSet( array $urls ) {
1093 foreach ( $urls as $density =>
$url ) {
1096 $density = (string)(
float)$density;
1097 $candidates[$density] =
$url;
1101 ksort( $candidates, SORT_NUMERIC );
1102 $candidates = array_unique( $candidates );
1105 foreach ( $candidates as $density =>
$url ) {
1106 $candidates[$density] =
$url .
' ' . $density .
'x';
1109 return implode(
", ", $candidates );
1128 return $value->value;
1130 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
1149 if ( $encodedArgs ===
false ) {
1152 return "$name($encodedArgs);";
1165 foreach ( $args as &$arg ) {
1167 if ( $arg ===
false ) {
1172 return ' ' . implode(
', ', $args ) .
' ';
1174 return implode(
',', $args );
1194 if ( isset(
$params[
'other'] ) ) {
1195 $options[
$params[
'other'] ] =
'other';
1199 foreach ( explode(
"\n", $list ) as $option ) {
1200 $value = trim( $option );
1201 if ( $value ==
'' ) {
1204 if ( substr( $value, 0, 1 ) ==
'*' && substr( $value, 1, 1 ) !=
'*' ) {
1205 # A new group is starting...
1206 $value = trim( substr( $value, 1 ) );
1207 if ( $value !==
'' &&
1215 } elseif ( substr( $value, 0, 2 ) ==
'**' ) {
1217 $opt = trim( substr( $value, 2 ) );
1218 if ( $optgroup ===
false ) {
1219 $options[$opt] = $opt;
1221 $options[$optgroup][$opt] = $opt;
1224 # groupless reason list
1226 $options[$option] = $option;
1244 foreach ( $options as $text => $value ) {
1245 if ( is_array( $value ) ) {
1246 $optionsOoui[] = [
'optgroup' => (string)$text ];
1247 foreach ( $value as $text2 => $value2 ) {
1248 $optionsOoui[] = [
'data' => (string)$value2,
'label' => (
string)$text2 ];
1251 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
1255 return $optionsOoui;
1269 foreach ( $options as $text => $value ) {
1270 if ( is_array( $value ) ) {
1272 $optionsCodex[] = [
'label' => (string)$text,
'value' =>
'',
'disabled' =>
true ];
1273 foreach ( $value as $text2 => $value2 ) {
1274 $optionsCodex[] = [
'label' => (string)$text2,
'value' => (
string)$value2 ];
1277 $optionsCodex[] = [
'label' => (string)$text,
'value' => (
string)$value ];
1280 return $optionsCodex;
1285class_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.