53 private static $voidElements = [
76 private static $boolAttribs = [
85 'formnovalidate' =>
true,
102 'typemustmatch' =>
true,
114 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()
115 ->getMainConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
116 if ( $useMediaWikiUIEverywhere ) {
117 if ( isset( $attrs[
'class'] ) ) {
118 if ( is_array( $attrs[
'class'] ) ) {
119 $attrs[
'class'][] =
'mw-ui-button';
120 $attrs[
'class'] = array_merge( $attrs[
'class'], $modifiers );
122 $attrs[
'class'] = implode(
' ', $attrs[
'class'] );
124 $attrs[
'class'] .=
' mw-ui-button ' . implode(
' ', $modifiers );
128 $attrs[
'class'] =
'mw-ui-button ' . implode(
' ', $modifiers );
142 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()
143 ->getMainConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
144 if ( $useMediaWikiUIEverywhere ) {
145 if ( isset( $attrs[
'class'] ) ) {
146 if ( is_array( $attrs[
'class'] ) ) {
147 $attrs[
'class'][] =
'mw-ui-input';
149 $attrs[
'class'] .=
' mw-ui-input';
152 $attrs[
'class'] =
'mw-ui-input';
170 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
171 return self::element(
'a',
172 self::buttonAttributes( $attrs, $modifiers ),
190 public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
191 $attrs[
'type'] =
'submit';
192 $attrs[
'value'] = $contents;
193 return self::element(
'input', self::buttonAttributes( $attrs, $modifiers ) );
214 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
215 $start = self::openElement( $element, $attribs );
216 if ( isset( self::$voidElements[$element] ) ) {
218 return substr( $start, 0, -1 ) .
'/>';
220 $contents = Sanitizer::escapeCombiningChar( $contents ??
'' );
221 return $start . $contents . self::closeElement( $element );
237 public static function element( $element, $attribs = [], $contents =
'' ) {
238 return self::rawElement( $element, $attribs, strtr( $contents ??
'', [
258 $attribs = (array)$attribs;
261 $element = strtolower( $element );
265 if ( strpos( $element,
' ' ) !==
false ) {
266 wfWarn( __METHOD__ .
" given element name with space '$element'" );
270 if ( $element ==
'input' ) {
285 'datetime-local' =>
true,
298 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
299 unset( $attribs[
'type'] );
306 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
307 $attribs[
'type'] =
'submit';
310 return "<$element" . self::expandAttributes(
311 self::dropDefaults( $element, $attribs ) ) .
'>';
322 $element = strtolower( $element );
324 return "</$element>";
344 private static function dropDefaults( $element, array $attribs ) {
347 static $attribDefaults = [
348 'area' => [
'shape' =>
'rect' ],
350 'formaction' =>
'GET',
351 'formenctype' =>
'application/x-www-form-urlencoded',
359 'autocomplete' =>
'on',
360 'enctype' =>
'application/x-www-form-urlencoded',
363 'formaction' =>
'GET',
366 'keygen' => [
'keytype' =>
'rsa' ],
367 'link' => [
'media' =>
'all' ],
368 'menu' => [
'type' =>
'list' ],
369 'script' => [
'type' =>
'text/javascript' ],
372 'type' =>
'text/css',
374 'textarea' => [
'wrap' =>
'soft' ],
377 foreach ( $attribs as $attrib => $value ) {
378 if ( $attrib ===
'class' ) {
379 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
380 unset( $attribs[$attrib] );
382 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
383 if ( is_array( $value ) ) {
384 $value = implode(
' ', $value );
386 $value = strval( $value );
388 if ( $attribDefaults[$element][$attrib] == $value ) {
389 unset( $attribs[$attrib] );
395 if ( $element ===
'link'
396 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
398 unset( $attribs[
'type'] );
400 if ( $element ===
'input' ) {
401 $type = $attribs[
'type'] ??
null;
402 $value = $attribs[
'value'] ??
null;
403 if (
$type ===
'checkbox' ||
$type ===
'radio' ) {
407 if ( $value ===
'on' ) {
408 unset( $attribs[
'value'] );
410 } elseif (
$type ===
'submit' ) {
418 if ( $value ===
'' ) {
419 unset( $attribs[
'value'] );
423 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
424 if ( in_array(
'multiple', $attribs )
425 || ( isset( $attribs[
'multiple'] ) && $attribs[
'multiple'] !==
false )
428 if ( strval( $attribs[
'size'] ) ==
'4' ) {
429 unset( $attribs[
'size'] );
433 if ( strval( $attribs[
'size'] ) ==
'1' ) {
434 unset( $attribs[
'size'] );
483 foreach ( $attribs as $key => $value ) {
485 if ( $value ===
false || $value ===
null ) {
491 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
497 $key = strtolower( $key );
501 $spaceSeparatedListAttributes = [
511 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
516 if ( is_array( $value ) ) {
519 foreach ( $value as $k => $v ) {
520 if ( is_string( $v ) ) {
523 if ( !isset( $value[$v] ) ) {
527 foreach ( explode(
' ', $v ) as $part ) {
530 if ( $part !==
'' && $part !==
' ' ) {
531 $arrayValue[] = $part;
542 $arrayValue = explode(
' ', $value );
545 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
549 $value = implode(
' ', array_unique( $arrayValue ) );
556 } elseif ( is_array( $value ) ) {
557 throw new MWException(
"HTML attribute $key can not contain a list of values" );
560 if ( isset( self::$boolAttribs[$key] ) ) {
561 $ret .=
" $key=\"\"";
566 $encValue = htmlspecialchars( $value, ENT_QUOTES );
570 $encValue = strtr( $encValue, [
575 $ret .=
" $key=\"$encValue\"";
596 if ( $nonce !==
null ) {
597 $attrs[
'nonce'] = $nonce;
599 wfWarn(
"no nonce set on script. CSP will break it" );
602 if ( preg_match(
'/<\/?script/i', $contents ) ) {
603 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
604 $contents =
'/* ERROR: Invalid script */';
607 return self::rawElement(
'script', $attrs, $contents );
619 $attrs = [
'src' => $url ];
620 if ( $nonce !==
null ) {
621 $attrs[
'nonce'] = $nonce;
623 wfWarn(
"no nonce set on script. CSP will break it" );
626 return self::element(
'script', $attrs );
641 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
646 $contents = strtr( $contents, [
650 ']]>' =>
'\5D\5D\3E '
653 if ( preg_match(
'/[<&]/', $contents ) ) {
654 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
657 return self::rawElement(
'style', [
659 ] + $attribs, $contents );
671 return self::element(
'link', [
672 'rel' =>
'stylesheet',
689 public static function input( $name, $value =
'',
$type =
'text', array $attribs = [] ) {
690 $attribs[
'type'] =
$type;
691 $attribs[
'value'] = $value;
692 $attribs[
'name'] = $name;
693 $textInputAttributes = [
700 if ( isset( $textInputAttributes[
$type] ) ) {
701 $attribs = self::getTextInputAttributes( $attribs );
703 $buttonAttributes = [
708 if ( isset( $buttonAttributes[
$type] ) ) {
709 $attribs = self::buttonAttributes( $attribs );
711 return self::element(
'input', $attribs );
722 public static function check( $name, $checked =
false, array $attribs = [] ) {
723 if ( isset( $attribs[
'value'] ) ) {
724 $value = $attribs[
'value'];
725 unset( $attribs[
'value'] );
731 $attribs[] =
'checked';
734 return self::input( $name, $value,
'checkbox', $attribs );
745 private static function messageBox( $html, $className, $heading =
'' ) {
746 if ( $heading !==
'' ) {
747 $html = self::element(
'h2', [], $heading ) . $html;
749 if ( is_array( $className ) ) {
750 $className[] =
'mw-message-box';
752 $className .=
' mw-message-box';
754 return self::rawElement(
'div', [
'class' => $className ], $html );
765 return self::messageBox( $html, [
'mw-message-box-notice', $className ] );
776 public static function warningBox( $html, $className =
'' ) {
777 return self::messageBox( $html, [
'mw-message-box-warning', $className ] );
789 public static function errorBox( $html, $heading =
'', $className =
'' ) {
790 return self::messageBox( $html, [
'mw-message-box-error', $className ], $heading );
801 public static function successBox( $html, $className =
'' ) {
802 return self::messageBox( $html, [
'mw-message-box-success', $className ] );
813 public static function radio( $name, $checked =
false, array $attribs = [] ) {
814 if ( isset( $attribs[
'value'] ) ) {
815 $value = $attribs[
'value'];
816 unset( $attribs[
'value'] );
822 $attribs[] =
'checked';
825 return self::input( $name, $value,
'radio', $attribs );
836 public static function label( $label, $id, array $attribs = [] ) {
840 return self::element(
'label', $attribs, $label );
852 public static function hidden( $name, $value, array $attribs = [] ) {
853 return self::input( $name, $value,
'hidden', $attribs );
868 public static function textarea( $name, $value =
'', array $attribs = [] ) {
869 $attribs[
'name'] = $name;
871 if ( substr( $value, 0, 1 ) ==
"\n" ) {
876 $spacedValue =
"\n" . $value;
878 $spacedValue = $value;
880 return self::element(
'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
889 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
890 $params[
'exclude'] = [];
893 if ( $params[
'in-user-lang'] ??
false ) {
897 $lang = MediaWikiServices::getInstance()->getContentLanguage();
901 if ( isset( $params[
'all'] ) ) {
904 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
907 $options =
$lang->getFormattedNamespaces();
909 foreach ( $options as $nsId => $nsName ) {
910 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
916 $nsName =
wfMessage(
'blanknamespace' )->text();
917 } elseif ( is_int( $nsId ) ) {
918 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
919 ->getLanguageConverter(
$lang );
920 $nsName = $converter->convertNamespace( $nsId );
922 $optionsOut[$nsId] = $nsName;
945 array $selectAttribs = []
947 ksort( $selectAttribs );
950 if ( isset( $params[
'selected'] ) ) {
953 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
954 $params[
'selected'] = (int)$params[
'selected'];
958 $params[
'selected'] =
'';
961 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
962 $params[
'disable'] = [];
966 $options = self::namespaceSelectorOptions( $params );
970 foreach ( $options as $nsId => $nsName ) {
971 $optionsHtml[] = self::element(
973 'disabled' => in_array( $nsId, $params[
'disable'] ),
975 'selected' => $nsId === $params[
'selected'],
980 if ( !array_key_exists(
'id', $selectAttribs ) ) {
981 $selectAttribs[
'id'] =
'namespace';
984 if ( !array_key_exists(
'name', $selectAttribs ) ) {
985 $selectAttribs[
'name'] =
'namespace';
989 if ( isset( $params[
'label'] ) ) {
990 $ret .= self::element(
992 'for' => $selectAttribs[
'id'] ??
null,
998 $ret .= self::openElement(
'select', $selectAttribs )
1000 . implode(
"\n", $optionsHtml )
1002 . self::closeElement(
'select' );
1017 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
1018 $html5Version = $mainConfig->get( MainConfigNames::Html5Version );
1019 $mimeType = $mainConfig->get( MainConfigNames::MimeType );
1020 $xhtmlNamespaces = $mainConfig->get( MainConfigNames::XhtmlNamespaces );
1022 $isXHTML = self::isXmlMimeType( $mimeType );
1027 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1030 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1033 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1034 $attribs[
"xmlns:$tag"] = $ns;
1037 $ret .=
"<!DOCTYPE html>\n";
1040 if ( $html5Version ) {
1041 $attribs[
'version'] = $html5Version;
1044 $ret .= self::openElement(
'html', $attribs );
1059 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1060 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1086 public static function srcSet( array $urls ) {
1088 foreach ( $urls as $density => $url ) {
1091 $density = (string)(
float)$density;
1092 $candidates[$density] = $url;
1096 ksort( $candidates, SORT_NUMERIC );
1097 $candidates = array_unique( $candidates );
1100 foreach ( $candidates as $density => $url ) {
1101 $candidates[$density] = $url .
' ' . $density .
'x';
1104 return implode(
", ", $candidates );
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.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
static isNonceRequired(Config $config)
Should we set nonce attribute.
This class is a collection of static functions that serve two purposes:
static inlineScript( $contents, $nonce=null)
Output an HTML script tag with the given contents.
static label( $label, $id, array $attribs=[])
Convenience function for generating a label for inputs.
static textarea( $name, $value='', array $attribs=[])
Convenience function to produce a <textarea> element.
static namespaceSelectorOptions(array $params=[])
Helper for Html::namespaceSelector().
static linkedScript( $url, $nonce=null)
Output a "<script>" tag linking to the given URL, e.g., "<script src=foo.js></script>".
static buttonAttributes(array $attrs, array $modifiers=[])
Modifies a set of attributes meant for button elements and apply a set of default attributes when $wg...
static linkedStyle( $url, $media='all')
Output a "<link rel=stylesheet>" linking to the given URL for the given media type (if any).
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
static isXmlMimeType( $mimetype)
Determines if the given MIME type is xml.
static htmlHeader(array $attribs=[])
Constructs the opening html-tag with necessary doctypes depending on global variables.
static input( $name, $value='', $type='text', array $attribs=[])
Convenience function to produce an "<input>" element.
static submitButton( $contents, array $attrs, array $modifiers=[])
Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enab...
static getTextInputAttributes(array $attrs)
Modifies a set of attributes meant for text input elements and apply a set of default attributes.
static radio( $name, $checked=false, array $attribs=[])
Convenience function to produce a radio button (input element with type=radio)
static expandAttributes(array $attribs)
Given an associative array of element attributes, generate a string to stick after the element name i...
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
static warningBox( $html, $className='')
Return a warning box.
static namespaceSelector(array $params=[], array $selectAttribs=[])
Build a drop-down box for selecting a namespace.
static noticeBox( $html, $className)
Return the HTML for a notice message box.
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
static linkButton( $text, array $attrs, array $modifiers=[])
Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enab...
static check( $name, $checked=false, array $attribs=[])
Convenience function to produce a checkbox (input element with type=checkbox)
static srcSet(array $urls)
Generate a srcset attribute value.
static successBox( $html, $className='')
Return a success box.
static errorBox( $html, $heading='', $className='')
Return an error box.
static closeElement( $element)
Returns "</$element>".
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
static inlineStyle( $contents, $media='all', $attribs=[])
Output a "<style>" tag with the given contents for the given media type (if any).
A class containing constants representing the names of configuration variables.
if(!isset( $args[0])) $lang