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 if ( in_array(
'multiple', $attribs )
408 || ( isset( $attribs[
'multiple'] ) && $attribs[
'multiple'] !==
false )
411 if ( strval( $attribs[
'size'] ) ==
'4' ) {
412 unset( $attribs[
'size'] );
416 if ( strval( $attribs[
'size'] ) ==
'1' ) {
417 unset( $attribs[
'size'] );
465 foreach ( $attribs as $key => $value ) {
467 if ( $value ===
false || $value ===
null ) {
473 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
479 $key = strtolower( $key );
483 $spaceSeparatedListAttributes = [
493 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
498 if ( is_array( $value ) ) {
501 foreach ( $value as $k => $v ) {
502 if ( is_string( $v ) ) {
505 if ( !isset( $value[$v] ) ) {
509 foreach ( explode(
' ', $v ) as $part ) {
512 if ( $part !==
'' && $part !==
' ' ) {
513 $arrayValue[] = $part;
524 $arrayValue = explode(
' ', $value );
527 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
531 $value = implode(
' ', array_unique( $arrayValue ) );
538 } elseif ( is_array( $value ) ) {
539 throw new UnexpectedValueException(
"HTML attribute $key can not contain a list of values" );
542 if ( isset( self::$boolAttribs[$key] ) ) {
543 $ret .=
" $key=\"\"";
548 $encValue = htmlspecialchars( $value, ENT_QUOTES );
552 $encValue = strtr( $encValue, [
557 $ret .=
" $key=\"$encValue\"";
577 if ( preg_match(
'/<\/?script/i', $contents ) ) {
578 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
579 $contents =
'/* ERROR: Invalid script */';
594 $attrs = [
'src' => $url ];
595 if ( $nonce !==
null ) {
596 $attrs[
'nonce'] = $nonce;
598 wfWarn(
"no nonce set on script. CSP will break it" );
616 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
621 $contents = strtr( $contents, [
625 ']]>' =>
'\5D\5D\3E '
628 if ( preg_match(
'/[<&]/', $contents ) ) {
629 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
634 ] + $attribs, $contents );
647 'rel' =>
'stylesheet',
664 public static function input( $name, $value =
'', $type =
'text', array $attribs = [] ) {
665 $attribs[
'type'] = $type;
666 $attribs[
'value'] = $value;
667 $attribs[
'name'] = $name;
679 public static function check( $name, $checked =
false, array $attribs = [] ) {
680 if ( isset( $attribs[
'value'] ) ) {
681 $value = $attribs[
'value'];
682 unset( $attribs[
'value'] );
688 $attribs[] =
'checked';
691 return self::input( $name, $value,
'checkbox', $attribs );
703 private static function messageBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
704 if ( $heading !==
'' ) {
712 if ( is_array( $className ) ) {
713 $className = array_merge(
718 $className .=
' ' . implode(
' ', $coreClasses );
721 self::element(
'span', [
'class' => [
725 self::rawElement(
'div', [
726 'class' =>
'cdx-message__content'
740 public static function noticeBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
741 return self::messageBox( $html, [
742 'mw-message-box-notice',
743 'cdx-message--notice',
745 ], $heading, $iconClassName );
756 public static function warningBox( $html, $className =
'' ) {
757 return self::messageBox( $html, [
758 'mw-message-box-warning',
759 'cdx-message--warning', $className ] );
771 public static function errorBox( $html, $heading =
'', $className =
'' ) {
772 return self::messageBox( $html, [
773 'mw-message-box-error',
774 'cdx-message--error', $className ], $heading );
785 public static function successBox( $html, $className =
'' ) {
786 return self::messageBox( $html, [
787 'mw-message-box-success',
788 'cdx-message--success', $className ] );
799 public static function radio( $name, $checked =
false, array $attribs = [] ) {
800 if ( isset( $attribs[
'value'] ) ) {
801 $value = $attribs[
'value'];
802 unset( $attribs[
'value'] );
808 $attribs[] =
'checked';
811 return self::input( $name, $value,
'radio', $attribs );
822 public static function label( $label, $id, array $attribs = [] ) {
838 public static function hidden( $name, $value, array $attribs = [] ) {
839 return self::input( $name, $value,
'hidden', $attribs );
854 public static function textarea( $name, $value =
'', array $attribs = [] ) {
855 $attribs[
'name'] = $name;
857 if ( substr( $value, 0, 1 ) ==
"\n" ) {
862 $spacedValue =
"\n" . $value;
864 $spacedValue = $value;
875 if ( !isset(
$params[
'exclude'] ) || !is_array(
$params[
'exclude'] ) ) {
879 if (
$params[
'in-user-lang'] ??
false ) {
887 if ( isset(
$params[
'all'] ) ) {
893 $options = $lang->getFormattedNamespaces();
895 foreach ( $options as $nsId => $nsName ) {
902 $nsName =
wfMessage(
'blanknamespace' )->text();
903 } elseif ( is_int( $nsId ) ) {
905 ->getLanguageConverter( $lang );
906 $nsName = $converter->convertNamespace( $nsId );
908 $optionsOut[$nsId] = $nsName;
932 array $selectAttribs = []
934 ksort( $selectAttribs );
937 if ( isset(
$params[
'selected'] ) ) {
940 if ( !is_int(
$params[
'selected'] ) && ctype_digit( (
string)
$params[
'selected'] ) ) {
948 if ( !isset(
$params[
'disable'] ) || !is_array(
$params[
'disable'] ) ) {
957 foreach ( $options as $nsId => $nsName ) {
961 'disabled' => in_array( $nsId,
$params[
'disable'] ),
963 'selected' => $nsId ===
$params[
'selected'],
969 $selectAttribs[
'id'] ??=
'namespace';
970 $selectAttribs[
'name'] ??=
'namespace';
973 if ( isset(
$params[
'label'] ) ) {
976 'for' => $selectAttribs[
'id'],
984 . implode(
"\n", $optionsHtml )
1011 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1014 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1017 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1018 $attribs[
"xmlns:$tag"] = $ns;
1021 $ret .=
"<!DOCTYPE html>\n";
1024 if ( $html5Version ) {
1025 $attribs[
'version'] = $html5Version;
1043 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1044 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1070 public static function srcSet( array $urls ) {
1072 foreach ( $urls as $density => $url ) {
1075 $density = (string)(
float)$density;
1076 $candidates[$density] = $url;
1080 ksort( $candidates, SORT_NUMERIC );
1081 $candidates = array_unique( $candidates );
1084 foreach ( $candidates as $density => $url ) {
1085 $candidates[$density] = $url .
' ' . $density .
'x';
1088 return implode(
", ", $candidates );
1107 return $value->value;
1109 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
1128 if ( $encodedArgs ===
false ) {
1131 return "$name($encodedArgs);";
1144 foreach ( $args as &$arg ) {
1146 if ( $arg ===
false ) {
1151 return ' ' . implode(
', ', $args ) .
' ';
1153 return implode(
',', $args );
1173 if ( isset(
$params[
'other'] ) ) {
1174 $options[
$params[
'other'] ] =
'other';
1178 foreach ( explode(
"\n", $list ) as $option ) {
1179 $value = trim( $option );
1180 if ( $value ==
'' ) {
1183 if ( substr( $value, 0, 1 ) ==
'*' && substr( $value, 1, 1 ) !=
'*' ) {
1184 # A new group is starting...
1185 $value = trim( substr( $value, 1 ) );
1186 if ( $value !==
'' &&
1194 } elseif ( substr( $value, 0, 2 ) ==
'**' ) {
1196 $opt = trim( substr( $value, 2 ) );
1197 if ( $optgroup ===
false ) {
1198 $options[$opt] = $opt;
1200 $options[$optgroup][$opt] = $opt;
1203 # groupless reason list
1205 $options[$option] = $option;
1223 foreach ( $options as $text => $value ) {
1224 if ( is_array( $value ) ) {
1225 $optionsOoui[] = [
'optgroup' => (string)$text ];
1226 foreach ( $value as $text2 => $value2 ) {
1227 $optionsOoui[] = [
'data' => (string)$value2,
'label' => (
string)$text2 ];
1230 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
1234 return $optionsOoui;
1239class_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.