33use UnexpectedValueException;
59 private static $voidElements = [
82 private static $boolAttribs = [
91 'formnovalidate' =>
true,
108 'typemustmatch' =>
true,
146 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] ) ) {
197 $contents = Sanitizer::escapeCombiningChar( $contents ??
'' );
218 public static function element( $element, $attribs = [], $contents =
'' ) {
222 strtr( $contents ??
'', [
243 $attribs = (array)$attribs;
246 $element = strtolower( $element );
250 if ( str_contains( $element,
' ' ) ) {
251 wfWarn( __METHOD__ .
" given element name with space '$element'" );
255 if ( $element ==
'input' ) {
270 'datetime-local' =>
true,
283 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
284 unset( $attribs[
'type'] );
291 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
292 $attribs[
'type'] =
'submit';
296 self::dropDefaults( $element, $attribs ) ) .
'>';
307 $element = strtolower( $element );
309 return "</$element>";
329 private static function dropDefaults( $element, array $attribs ) {
332 static $attribDefaults = [
333 'area' => [
'shape' =>
'rect' ],
335 'formaction' =>
'GET',
336 'formenctype' =>
'application/x-www-form-urlencoded',
344 'autocomplete' =>
'on',
345 'enctype' =>
'application/x-www-form-urlencoded',
348 'formaction' =>
'GET',
351 'keygen' => [
'keytype' =>
'rsa' ],
352 'link' => [
'media' =>
'all' ],
353 'menu' => [
'type' =>
'list' ],
354 'script' => [
'type' =>
'text/javascript' ],
357 'type' =>
'text/css',
359 'textarea' => [
'wrap' =>
'soft' ],
362 foreach ( $attribs as $attrib => $value ) {
363 if ( $attrib ===
'class' ) {
364 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
365 unset( $attribs[$attrib] );
367 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
368 if ( is_array( $value ) ) {
369 $value = implode(
' ', $value );
371 $value = strval( $value );
373 if ( $attribDefaults[$element][$attrib] == $value ) {
374 unset( $attribs[$attrib] );
380 if ( $element ===
'link'
381 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
383 unset( $attribs[
'type'] );
385 if ( $element ===
'input' ) {
386 $type = $attribs[
'type'] ??
null;
387 $value = $attribs[
'value'] ??
null;
388 if ( $type ===
'checkbox' || $type ===
'radio' ) {
392 if ( $value ===
'on' ) {
393 unset( $attribs[
'value'] );
395 } elseif ( $type ===
'submit' ) {
403 if ( $value ===
'' ) {
404 unset( $attribs[
'value'] );
408 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
409 $multiple = ( $attribs[
'multiple'] ?? false ) !==
false ||
410 in_array(
'multiple', $attribs );
411 $default = $multiple ? 4 : 1;
412 if ( (
int)$attribs[
'size'] === $default ) {
413 unset( $attribs[
'size'] );
460 foreach ( $attribs as $key => $value ) {
462 if ( $value ===
false || $value ===
null ) {
468 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
474 $key = strtolower( $key );
478 $spaceSeparatedListAttributes = [
488 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
493 if ( is_array( $value ) ) {
496 foreach ( $value as $k => $v ) {
497 if ( is_string( $v ) ) {
500 if ( !isset( $value[$v] ) ) {
504 foreach ( explode(
' ', $v ) as $part ) {
507 if ( $part !==
'' && $part !==
' ' ) {
508 $arrayValue[] = $part;
519 $arrayValue = explode(
' ', $value );
522 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
526 $value = implode(
' ', array_unique( $arrayValue ) );
533 } elseif ( is_array( $value ) ) {
534 throw new UnexpectedValueException(
"HTML attribute $key can not contain a list of values" );
537 if ( isset( self::$boolAttribs[$key] ) ) {
538 $ret .=
" $key=\"\"";
543 $encValue = htmlspecialchars( $value, ENT_QUOTES );
547 $encValue = strtr( $encValue, [
552 $ret .=
" $key=\"$encValue\"";
572 if ( preg_match(
'/<\/?script/i', $contents ) ) {
573 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
574 $contents =
'/* ERROR: Invalid script */';
589 $attrs = [
'src' =>
$url ];
590 if ( $nonce !==
null ) {
591 $attrs[
'nonce'] = $nonce;
593 wfWarn(
"no nonce set on script. CSP will break it" );
611 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
616 $contents = strtr( $contents, [
620 ']]>' =>
'\5D\5D\3E '
623 if ( preg_match(
'/[<&]/', $contents ) ) {
624 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
629 ] + $attribs, $contents );
642 'rel' =>
'stylesheet',
659 public static function input( $name, $value =
'', $type =
'text', array $attribs = [] ) {
660 $attribs[
'type'] = $type;
661 $attribs[
'value'] = $value;
662 $attribs[
'name'] = $name;
674 public static function check( $name, $checked =
false, array $attribs = [] ) {
675 if ( isset( $attribs[
'value'] ) ) {
676 $value = $attribs[
'value'];
677 unset( $attribs[
'value'] );
683 $attribs[] =
'checked';
686 return self::input( $name, $value,
'checkbox', $attribs );
699 private static function messageBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
700 if ( $heading !==
'' ) {
707 if ( is_array( $className ) ) {
708 $className = array_merge(
713 $className .=
' ' . implode(
' ', $coreClasses );
716 self::element(
'span', [
'class' => [
720 self::rawElement(
'div', [
721 'class' =>
'cdx-message__content'
736 public static function noticeBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
737 return self::messageBox( $html, [
738 'cdx-message--notice',
740 ], $heading, $iconClassName );
752 public static function warningBox( $html, $className =
'' ) {
753 return self::messageBox( $html, [
754 'cdx-message--warning', $className ] );
767 public static function errorBox( $html, $heading =
'', $className =
'' ) {
768 return self::messageBox( $html, [
769 'cdx-message--error', $className ], $heading );
781 public static function successBox( $html, $className =
'' ) {
782 return self::messageBox( $html, [
783 'cdx-message--success', $className ] );
794 public static function radio( $name, $checked =
false, array $attribs = [] ) {
795 if ( isset( $attribs[
'value'] ) ) {
796 $value = $attribs[
'value'];
797 unset( $attribs[
'value'] );
803 $attribs[] =
'checked';
806 return self::input( $name, $value,
'radio', $attribs );
817 public static function label( $label, $id, array $attribs = [] ) {
833 public static function hidden( $name, $value, array $attribs = [] ) {
834 return self::input( $name, $value,
'hidden', $attribs );
849 public static function textarea( $name, $value =
'', array $attribs = [] ) {
850 $attribs[
'name'] = $name;
852 if ( substr( $value ??
'', 0, 1 ) ==
"\n" ) {
857 $spacedValue =
"\n" . $value;
859 $spacedValue = $value;
870 if ( !isset(
$params[
'exclude'] ) || !is_array(
$params[
'exclude'] ) ) {
874 if (
$params[
'in-user-lang'] ??
false ) {
882 if ( isset(
$params[
'all'] ) ) {
888 $options = $lang->getFormattedNamespaces();
890 foreach ( $options as $nsId => $nsName ) {
897 $nsName =
wfMessage(
'blanknamespace' )->text();
898 } elseif ( is_int( $nsId ) ) {
900 ->getLanguageConverter( $lang );
901 $nsName = $converter->convertNamespace( $nsId );
903 $optionsOut[$nsId] = $nsName;
927 array $selectAttribs = []
929 ksort( $selectAttribs );
932 if ( isset(
$params[
'selected'] ) ) {
935 if ( !is_int(
$params[
'selected'] ) && ctype_digit( (
string)
$params[
'selected'] ) ) {
943 if ( !isset(
$params[
'disable'] ) || !is_array(
$params[
'disable'] ) ) {
952 foreach ( $options as $nsId => $nsName ) {
956 'disabled' => in_array( $nsId,
$params[
'disable'] ),
958 'selected' => $nsId ===
$params[
'selected'],
964 $selectAttribs[
'id'] ??=
'namespace';
965 $selectAttribs[
'name'] ??=
'namespace';
968 if ( isset(
$params[
'label'] ) ) {
971 'for' => $selectAttribs[
'id'],
979 . implode(
"\n", $optionsHtml )
1006 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1009 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1012 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1013 $attribs[
"xmlns:$tag"] = $ns;
1016 $ret .=
"<!DOCTYPE html>\n";
1019 if ( $html5Version ) {
1020 $attribs[
'version'] = $html5Version;
1038 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1039 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1065 public static function srcSet( array $urls ) {
1067 foreach ( $urls as $density =>
$url ) {
1070 $density = (string)(
float)$density;
1071 $candidates[$density] =
$url;
1075 ksort( $candidates, SORT_NUMERIC );
1076 $candidates = array_unique( $candidates );
1079 foreach ( $candidates as $density =>
$url ) {
1080 $candidates[$density] =
$url .
' ' . $density .
'x';
1083 return implode(
", ", $candidates );
1102 return $value->value;
1104 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
1123 if ( $encodedArgs ===
false ) {
1126 return "$name($encodedArgs);";
1139 foreach ( $args as &$arg ) {
1141 if ( $arg ===
false ) {
1146 return ' ' . implode(
', ', $args ) .
' ';
1148 return implode(
',', $args );
1168 if ( isset(
$params[
'other'] ) ) {
1169 $options[
$params[
'other'] ] =
'other';
1173 foreach ( explode(
"\n", $list ) as $option ) {
1174 $value = trim( $option );
1175 if ( $value ==
'' ) {
1178 if ( substr( $value, 0, 1 ) ==
'*' && substr( $value, 1, 1 ) !=
'*' ) {
1179 # A new group is starting...
1180 $value = trim( substr( $value, 1 ) );
1181 if ( $value !==
'' &&
1189 } elseif ( substr( $value, 0, 2 ) ==
'**' ) {
1191 $opt = trim( substr( $value, 2 ) );
1192 if ( $optgroup ===
false ) {
1193 $options[$opt] = $opt;
1195 $options[$optgroup][$opt] = $opt;
1198 # groupless reason list
1200 $options[$option] = $option;
1218 foreach ( $options as $text => $value ) {
1219 if ( is_array( $value ) ) {
1220 $optionsOoui[] = [
'optgroup' => (string)$text ];
1221 foreach ( $value as $text2 => $value2 ) {
1222 $optionsOoui[] = [
'data' => (string)$value2,
'label' => (
string)$text2 ];
1225 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
1229 return $optionsOoui;
1243 foreach ( $options as $text => $value ) {
1244 if ( is_array( $value ) ) {
1246 $optionsCodex[] = [
'label' => (string)$text,
'value' =>
'',
'disabled' =>
true ];
1247 foreach ( $value as $text2 => $value2 ) {
1248 $optionsCodex[] = [
'label' => (string)$text2,
'value' => (
string)$value2 ];
1251 $optionsCodex[] = [
'label' => (string)$text,
'value' => (
string)$value ];
1254 return $optionsCodex;
1259class_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.