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 = [] ) {
164 public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
165 $attrs[
'type'] =
'submit';
166 $attrs[
'value'] = $contents;
192 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
194 if ( isset( self::$voidElements[$element] ) ) {
217 public static function element( $element, $attribs = [], $contents =
'' ) {
221 strtr( $contents ??
'', [
242 $attribs = (array)$attribs;
245 $element = strtolower( $element );
249 if ( str_contains( $element,
' ' ) ) {
250 wfWarn( __METHOD__ .
" given element name with space '$element'" );
254 if ( $element ==
'input' ) {
269 'datetime-local' =>
true,
282 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
283 unset( $attribs[
'type'] );
290 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
291 $attribs[
'type'] =
'submit';
295 self::dropDefaults( $element, $attribs ) ) .
'>';
306 $element = strtolower( $element );
308 return "</$element>";
328 private static function dropDefaults( $element, array $attribs ) {
331 static $attribDefaults = [
332 'area' => [
'shape' =>
'rect' ],
334 'formaction' =>
'GET',
335 'formenctype' =>
'application/x-www-form-urlencoded',
343 'autocomplete' =>
'on',
344 'enctype' =>
'application/x-www-form-urlencoded',
347 'formaction' =>
'GET',
350 'keygen' => [
'keytype' =>
'rsa' ],
351 'link' => [
'media' =>
'all' ],
352 'menu' => [
'type' =>
'list' ],
353 'script' => [
'type' =>
'text/javascript' ],
356 'type' =>
'text/css',
358 'textarea' => [
'wrap' =>
'soft' ],
361 foreach ( $attribs as $attrib => $value ) {
362 if ( $attrib ===
'class' ) {
363 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
364 unset( $attribs[$attrib] );
366 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
367 if ( is_array( $value ) ) {
368 $value = implode(
' ', $value );
370 $value = strval( $value );
372 if ( $attribDefaults[$element][$attrib] == $value ) {
373 unset( $attribs[$attrib] );
379 if ( $element ===
'link'
380 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
382 unset( $attribs[
'type'] );
384 if ( $element ===
'input' ) {
385 $type = $attribs[
'type'] ??
null;
386 $value = $attribs[
'value'] ??
null;
387 if ( $type ===
'checkbox' || $type ===
'radio' ) {
391 if ( $value ===
'on' ) {
392 unset( $attribs[
'value'] );
394 } elseif ( $type ===
'submit' ) {
402 if ( $value ===
'' ) {
403 unset( $attribs[
'value'] );
407 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
408 if ( in_array(
'multiple', $attribs )
409 || ( isset( $attribs[
'multiple'] ) && $attribs[
'multiple'] !==
false )
412 if ( strval( $attribs[
'size'] ) ==
'4' ) {
413 unset( $attribs[
'size'] );
417 if ( strval( $attribs[
'size'] ) ==
'1' ) {
418 unset( $attribs[
'size'] );
466 foreach ( $attribs as $key => $value ) {
468 if ( $value ===
false || $value ===
null ) {
474 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
480 $key = strtolower( $key );
484 $spaceSeparatedListAttributes = [
494 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
499 if ( is_array( $value ) ) {
502 foreach ( $value as $k => $v ) {
503 if ( is_string( $v ) ) {
506 if ( !isset( $value[$v] ) ) {
510 foreach ( explode(
' ', $v ) as $part ) {
513 if ( $part !==
'' && $part !==
' ' ) {
514 $arrayValue[] = $part;
525 $arrayValue = explode(
' ', $value );
528 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
532 $value = implode(
' ', array_unique( $arrayValue ) );
539 } elseif ( is_array( $value ) ) {
540 throw new UnexpectedValueException(
"HTML attribute $key can not contain a list of values" );
543 if ( isset( self::$boolAttribs[$key] ) ) {
544 $ret .=
" $key=\"\"";
549 $encValue = htmlspecialchars( $value, ENT_QUOTES );
553 $encValue = strtr( $encValue, [
558 $ret .=
" $key=\"$encValue\"";
578 if ( preg_match(
'/<\/?script/i', $contents ) ) {
579 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
580 $contents =
'/* ERROR: Invalid script */';
595 $attrs = [
'src' => $url ];
596 if ( $nonce !==
null ) {
597 $attrs[
'nonce'] = $nonce;
599 wfWarn(
"no nonce set on script. CSP will break it" );
617 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
622 $contents = strtr( $contents, [
626 ']]>' =>
'\5D\5D\3E '
629 if ( preg_match(
'/[<&]/', $contents ) ) {
630 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
635 ] + $attribs, $contents );
648 'rel' =>
'stylesheet',
665 public static function input( $name, $value =
'', $type =
'text', array $attribs = [] ) {
666 $attribs[
'type'] = $type;
667 $attribs[
'value'] = $value;
668 $attribs[
'name'] = $name;
680 public static function check( $name, $checked =
false, array $attribs = [] ) {
681 if ( isset( $attribs[
'value'] ) ) {
682 $value = $attribs[
'value'];
683 unset( $attribs[
'value'] );
689 $attribs[] =
'checked';
692 return self::input( $name, $value,
'checkbox', $attribs );
704 private static function messageBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
705 if ( $heading !==
'' ) {
713 if ( is_array( $className ) ) {
714 $className = array_merge(
719 $className .=
' ' . implode(
' ', $coreClasses );
722 self::element(
'span', [
'class' => [
726 self::rawElement(
'div', [
727 'class' =>
'cdx-message__content'
741 public static function noticeBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
742 return self::messageBox( $html, [
743 'mw-message-box-notice',
744 'cdx-message--notice',
746 ], $heading, $iconClassName );
757 public static function warningBox( $html, $className =
'' ) {
758 return self::messageBox( $html, [
759 'mw-message-box-warning',
760 'cdx-message--warning', $className ] );
772 public static function errorBox( $html, $heading =
'', $className =
'' ) {
773 return self::messageBox( $html, [
774 'mw-message-box-error',
775 'cdx-message--error', $className ], $heading );
786 public static function successBox( $html, $className =
'' ) {
787 return self::messageBox( $html, [
788 'mw-message-box-success',
789 'cdx-message--success', $className ] );
800 public static function radio( $name, $checked =
false, array $attribs = [] ) {
801 if ( isset( $attribs[
'value'] ) ) {
802 $value = $attribs[
'value'];
803 unset( $attribs[
'value'] );
809 $attribs[] =
'checked';
812 return self::input( $name, $value,
'radio', $attribs );
823 public static function label( $label, $id, array $attribs = [] ) {
839 public static function hidden( $name, $value, array $attribs = [] ) {
840 return self::input( $name, $value,
'hidden', $attribs );
855 public static function textarea( $name, $value =
'', array $attribs = [] ) {
856 $attribs[
'name'] = $name;
858 if ( substr( $value, 0, 1 ) ==
"\n" ) {
863 $spacedValue =
"\n" . $value;
865 $spacedValue = $value;
876 if ( !isset(
$params[
'exclude'] ) || !is_array(
$params[
'exclude'] ) ) {
880 if (
$params[
'in-user-lang'] ??
false ) {
888 if ( isset(
$params[
'all'] ) ) {
894 $options = $lang->getFormattedNamespaces();
896 foreach ( $options as $nsId => $nsName ) {
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;
1242class_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.