MediaWiki master
MediaWiki\Html\Html Class Reference

This class is a collection of static functions that serve two purposes: More...

Static Public Member Functions

static buttonAttributes (array $attrs, array $modifiers=[])
 Modifies a set of attributes meant for button elements.
 
static check ( $name, $checked=false, array $attribs=[])
 Convenience function to produce a checkbox (input element with type=checkbox)
 
static closeElement ( $element)
 Returns "</$element>".
 
static element ( $element, $attribs=[], $contents='')
 Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
 
static encodeJsCall ( $name, $args, $pretty=false)
 Create a call to a JavaScript function.
 
static encodeJsList ( $args, $pretty=false)
 Encode a JavaScript comma-separated list.
 
static encodeJsVar ( $value, $pretty=false)
 Encode a variable of arbitrary type to JavaScript.
 
static errorBox ( $html, $heading='', $className='')
 Return an error box.
 
static expandAttributes (array $attribs)
 Given an associative array of element attributes, generate a string to stick after the element name in HTML output.
 
static getTextInputAttributes (array $attrs)
 Modifies a set of attributes meant for text input elements.
 
static hidden ( $name, $value, array $attribs=[])
 Convenience function to produce an input element with type=hidden.
 
static htmlHeader (array $attribs=[])
 Constructs the opening html-tag with necessary doctypes depending on global variables.
 
static inlineScript ( $contents, $nonce=null)
 Output an HTML script tag with the given contents.
 
static inlineStyle ( $contents, $media='all', $attribs=[])
 Output a "<style>" tag with the given contents for the given media type (if any).
 
static input ( $name, $value='', $type='text', array $attribs=[])
 Convenience function to produce an <input> element.
 
static isXmlMimeType ( $mimetype)
 Determines if the given MIME type is xml.
 
static label ( $label, $id, array $attribs=[])
 Convenience function for generating a label for inputs.
 
static linkButton ( $text, array $attrs, array $modifiers=[])
 Returns an HTML link element in a string.
 
static linkedScript ( $url, $nonce=null)
 Output a "<script>" tag linking to the given URL, e.g., "<script src=foo.js></script>".
 
static linkedStyle ( $url, $media='all')
 Output a "<link rel=stylesheet>" linking to the given URL for the given media type (if any).
 
static listDropdownOptions ( $list, $params=[])
 Build options for a drop-down box from a textual list.
 
static listDropdownOptionsOoui ( $options)
 Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
 
static namespaceSelector (array $params=[], array $selectAttribs=[])
 Build a drop-down box for selecting a namespace.
 
static namespaceSelectorOptions (array $params=[])
 Helper for Html::namespaceSelector().
 
static noticeBox ( $html, $className, $heading='', $iconClassName='')
 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 '/' in XML mode for empty elements).
 
static radio ( $name, $checked=false, array $attribs=[])
 Convenience function to produce a radio button (input element with type=radio)
 
static rawElement ( $element, $attribs=[], $contents='')
 Returns an HTML element in a string.
 
static srcSet (array $urls)
 Generate a srcset attribute value.
 
static submitButton ( $contents, array $attrs=[], array $modifiers=[])
 Returns an HTML input element in a string.
 
static successBox ( $html, $className='')
 Return a success box.
 
static textarea ( $name, $value='', array $attribs=[])
 Convenience function to produce a <textarea> element.
 
static warningBox ( $html, $className='')
 Return a warning box.
 

Detailed Description

This class is a collection of static functions that serve two purposes:

1) Implement any algorithms specified by HTML5, or other HTML specifications, in a convenient and self-contained way.

2) Allow HTML elements to be conveniently and safely generated, like the current Xml class but a) less confused (Xml supports HTML-specific things, but only sometimes!) and b) not necessarily confined to XML-compatible output.

There are two important configuration options this class uses:

$wgMimeType: If this is set to an xml MIME type then output should be valid XHTML5.

This class is meant to be confined to utility functions that are called from trusted code paths. It does not do enforcement of policy like not allowing elements.

Since
1.16

Definition at line 56 of file Html.php.

Member Function Documentation

◆ buttonAttributes()

static MediaWiki\Html\Html::buttonAttributes ( array $attrs,
array $modifiers = [] )
static

Modifies a set of attributes meant for button elements.

Parameters
array$attrsHTML attributes in an associative array
string[]$modifiersUnused
Returns
array Modified attributes array
Deprecated
since 1.42 No-op

Definition at line 118 of file Html.php.

References wfDeprecated().

◆ check()

static MediaWiki\Html\Html::check ( $name,
$checked = false,
array $attribs = [] )
static

Convenience function to produce a checkbox (input element with type=checkbox)

Parameters
string$nameName attribute
bool$checkedWhether the checkbox is checked or not
array$attribsArray of additional attributes
Returns
string Raw HTML

Definition at line 679 of file Html.php.

References MediaWiki\Html\Html\input().

◆ closeElement()

static MediaWiki\Html\Html::closeElement ( $element)
static

Returns "</$element>".

Since
1.17
Parameters
string$elementName of the element, e.g., 'a'
Returns
string A closing tag

Definition at line 304 of file Html.php.

Referenced by MediaWiki\Html\Html\namespaceSelector(), and MediaWiki\Html\Html\rawElement().

◆ element()

static MediaWiki\Html\Html::element ( $element,
$attribs = [],
$contents = '' )
static

Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).

Parameters
string$elementName of the element, e.g., 'a'
array$attribsAssociative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for further documentation.
string$contents
Returns
string

Definition at line 216 of file Html.php.

References MediaWiki\Html\Html\rawElement().

Referenced by MediaWiki\Html\Html\input(), MediaWiki\Html\Html\label(), MediaWiki\Html\Html\linkButton(), MediaWiki\Html\Html\linkedScript(), MediaWiki\Html\Html\linkedStyle(), MediaWiki\Html\Html\namespaceSelector(), MediaWiki\Html\Html\submitButton(), and MediaWiki\Html\Html\textarea().

◆ encodeJsCall()

static MediaWiki\Html\Html::encodeJsCall ( $name,
$args,
$pretty = false )
static

Create a call to a JavaScript function.

The supplied arguments will be encoded using Html::encodeJsVar().

Since
1.41 (previously on Xml since 1.17)
Parameters
string$nameThe name of the function to call, or a JavaScript expression which evaluates to a function object which is called.
array$argsThe arguments to pass to the function.
bool$prettyIf true, add non-significant whitespace to improve readability.
Returns
string|false String if successful; false upon failure

Definition at line 1126 of file Html.php.

References MediaWiki\Html\Html\encodeJsList().

◆ encodeJsList()

static MediaWiki\Html\Html::encodeJsList ( $args,
$pretty = false )
static

Encode a JavaScript comma-separated list.

The supplied items will be encoded using Html::encodeJsVar().

Since
1.41.
Parameters
array$argsThe elements of the list.
bool$prettyIf true, add non-significant whitespace to improve readability.
Returns
false|string String if successful; false upon failure

Definition at line 1143 of file Html.php.

References MediaWiki\Html\Html\encodeJsVar().

Referenced by MediaWiki\Html\Html\encodeJsCall().

◆ encodeJsVar()

static MediaWiki\Html\Html::encodeJsVar ( $value,
$pretty = false )
static

Encode a variable of arbitrary type to JavaScript.

If the value is an HtmlJsCode object, pass through the object's value verbatim.

Note
Only use this function for generating JavaScript code. If generating output for a proper JSON parser, just call FormatJson::encode() directly.
Since
1.41 (previously on Xml)
Parameters
mixed$valueThe value being encoded. Can be any type except a resource.
bool$prettyIf true, add non-significant whitespace to improve readability.
Returns
string|false String if successful; false upon failure

Definition at line 1105 of file Html.php.

Referenced by MediaWiki\Html\Html\encodeJsList(), and MediaWiki\Html\HtmlJsCode\encodeObject().

◆ errorBox()

static MediaWiki\Html\Html::errorBox ( $html,
$heading = '',
$className = '' )
static

Return an error box.

Since
1.31
1.34 $className optional parameter added
Parameters
string$htmlof contents of error box
string$heading(optional)
string$className(optional) corresponding to box
Returns
string of HTML representing an error box.

Definition at line 771 of file Html.php.

◆ expandAttributes()

static MediaWiki\Html\Html::expandAttributes ( array $attribs)
static

Given an associative array of element attributes, generate a string to stick after the element name in HTML output.

Like [ 'href' => 'https://www.mediawiki.org/' ] becomes something like ' href="https://www.mediawiki.org"'. Again, this is like Xml::expandAttributes(), but it implements some HTML-specific logic.

Attributes that can contain space-separated lists ('class', 'accesskey' and 'rel') array values are allowed as well, which will automagically be normalized and converted to a space-separated string. In addition to a numerical array, the attribute value may also be an associative array. See the example below for how that works.

Numerical array
Html::element( 'em', [
'class' => [ 'foo', 'bar' ]
] );
// gives '<em class="foo bar"></em>'
Associative array
Html::element( 'em', [
'class' => [ 'foo', 'bar', 'foo' => false, 'quux' => true ]
] );
// gives '<em class="bar quux"></em>'
Parameters
array$attribsAssociative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. Values will be HTML-escaped. A value of false or null means to omit the attribute. For boolean attributes, you can omit the key, e.g., [ 'checked' ] instead of [ 'checked' => 'checked' ] or such.
Returns
string HTML fragment that goes between element name and '>' (starting with a space if at least one attribute is output)

Definition at line 463 of file Html.php.

Referenced by MediaWiki\Html\Html\openElement().

◆ getTextInputAttributes()

static MediaWiki\Html\Html::getTextInputAttributes ( array $attrs)
static

Modifies a set of attributes meant for text input elements.

Parameters
array$attrsAn attribute array.
Returns
array Modified attributes array
Deprecated
since 1.42 No-op

Definition at line 130 of file Html.php.

References wfDeprecated().

◆ hidden()

static MediaWiki\Html\Html::hidden ( $name,
$value,
array $attribs = [] )
static

Convenience function to produce an input element with type=hidden.

Parameters
string$nameName attribute
mixed$valueValue attribute
array$attribsAssociative array of miscellaneous extra attributes, passed to Html::element()
Returns
string Raw HTML

Definition at line 838 of file Html.php.

References MediaWiki\Html\Html\input().

◆ htmlHeader()

static MediaWiki\Html\Html::htmlHeader ( array $attribs = [])
static

Constructs the opening html-tag with necessary doctypes depending on global variables.

Parameters
array$attribsAssociative array of miscellaneous extra attributes, passed to Html::element() of html tag.
Returns
string Raw HTML

Definition at line 999 of file Html.php.

References MediaWiki\MediaWikiServices\getInstance(), MediaWiki\MainConfigNames\Html5Version, MediaWiki\Html\Html\isXmlMimeType(), MediaWiki\MainConfigNames\MimeType, MediaWiki\Html\Html\openElement(), and MediaWiki\MainConfigNames\XhtmlNamespaces.

◆ inlineScript()

static MediaWiki\Html\Html::inlineScript ( $contents,
$nonce = null )
static

Output an HTML script tag with the given contents.

It is unsupported for the contents to contain the sequence <script or </script (case-insensitive). This ensures the script can be terminated easily and consistently. It is the responsibility of the caller to avoid such character sequence by escaping or avoiding it. If found at run-time, the contents are replaced with a comment, and a warning is logged server-side.

Parameters
string$contentsJavaScript
string | null$nonceUnused
Returns
string Raw HTML

Definition at line 576 of file Html.php.

References MediaWiki\Html\Html\rawElement(), and wfLogWarning().

◆ inlineStyle()

static MediaWiki\Html\Html::inlineStyle ( $contents,
$media = 'all',
$attribs = [] )
static

Output a "<style>" tag with the given contents for the given media type (if any).

TODO: do some useful escaping as well, like if $contents contains literal "</style>" (admittedly unlikely).

Parameters
string$contentsCSS
string$mediaA media type string, like 'screen'
array$attribs(since 1.31) Associative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for further documentation.
Returns
string Raw HTML

Definition at line 616 of file Html.php.

References MediaWiki\Html\Html\rawElement().

◆ input()

static MediaWiki\Html\Html::input ( $name,
$value = '',
$type = 'text',
array $attribs = [] )
static

Convenience function to produce an <input> element.

This supports the new HTML5 input types and attributes.

Parameters
string$nameName attribute
string$valueValue attribute
string$typeType attribute
array$attribsAssociative array of miscellaneous extra attributes, passed to Html::element()
Returns
string Raw HTML

Definition at line 664 of file Html.php.

References MediaWiki\Html\Html\element().

Referenced by MediaWiki\Html\Html\check(), MediaWiki\Html\Html\hidden(), and MediaWiki\Html\Html\radio().

◆ isXmlMimeType()

static MediaWiki\Html\Html::isXmlMimeType ( $mimetype)
static

Determines if the given MIME type is xml.

Parameters
string$mimetype
Returns
bool

Definition at line 1039 of file Html.php.

Referenced by MediaWiki\Html\Html\htmlHeader().

◆ label()

static MediaWiki\Html\Html::label ( $label,
$id,
array $attribs = [] )
static

Convenience function for generating a label for inputs.

Parameters
string$labelContents of the label
string$idID of the element being labeled
array$attribsAdditional attributes
Returns
string Raw HTML

Definition at line 822 of file Html.php.

References MediaWiki\Html\Html\element().

◆ linkButton()

static MediaWiki\Html\Html::linkButton ( $text,
array $attrs,
array $modifiers = [] )
static

Returns an HTML link element in a string.

Parameters
string$textThe text of the element. Will be escaped (not raw HTML)
array$attrsAssociative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for further documentation.
string[]$modifiersUnused
Returns
string Raw HTML

Definition at line 145 of file Html.php.

References MediaWiki\Html\Html\element().

◆ linkedScript()

static MediaWiki\Html\Html::linkedScript ( $url,
$nonce = null )
static

Output a "<script>" tag linking to the given URL, e.g., "<script src=foo.js></script>".

Parameters
string$url
string | null$nonceNonce for CSP header, from OutputPage->getCSP()->getNonce()
Returns
string Raw HTML

Definition at line 593 of file Html.php.

References MediaWiki\Html\Html\element(), MediaWiki\MediaWikiServices\getInstance(), and wfWarn().

◆ linkedStyle()

static MediaWiki\Html\Html::linkedStyle ( $url,
$media = 'all' )
static

Output a "<link rel=stylesheet>" linking to the given URL for the given media type (if any).

Parameters
string$url
string$mediaA media type string, like 'screen'
Returns
string Raw HTML

Definition at line 645 of file Html.php.

References MediaWiki\Html\Html\element().

◆ listDropdownOptions()

static MediaWiki\Html\Html::listDropdownOptions ( $list,
$params = [] )
static

Build options for a drop-down box from a textual list.

The result of this function can be passed to XmlSelect::addOptions() (to render a plain <select> dropdown box) or to Html::listDropdownOptionsOoui() and then OOUI\DropdownInputWidget() (to render a pretty one).

Parameters
string$listCorrectly formatted text (newline delimited) to be used to generate the options.
array$paramsExtra parameters:
  • string $params['other'] If set, add an option with this as text and a value of 'other'
Returns
array Array keys are textual labels, values are internal values

Definition at line 1170 of file Html.php.

References $params.

◆ listDropdownOptionsOoui()

static MediaWiki\Html\Html::listDropdownOptionsOoui ( $options)
static

Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.

TODO Find a better home for this function.

Parameters
array$optionsOptions, as returned e.g. by Html::listDropdownOptions()
Returns
array

Definition at line 1220 of file Html.php.

◆ namespaceSelector()

static MediaWiki\Html\Html::namespaceSelector ( array $params = [],
array $selectAttribs = [] )
static

Build a drop-down box for selecting a namespace.

Parameters
array$paramsParams to set.
  • selected: [optional] Id of namespace which should be pre-selected
  • all: [optional] Value of item for "all namespaces". If null or unset, no "<option>" is generated to select all namespaces.
  • label: text for label to add before the field.
  • exclude: [optional] Array of namespace ids to exclude.
  • disable: [optional] Array of namespace ids for which the option should be disabled in the selector.
array$selectAttribsHTML attributes for the generated select element.
  • id: [optional], default: 'namespace'.
  • name: [optional], default: 'namespace'.
Returns
string HTML code to select a namespace.

Definition at line 930 of file Html.php.

References $params, MediaWiki\Html\Html\closeElement(), MediaWiki\Html\Html\element(), MediaWiki\Html\Html\namespaceSelectorOptions(), and MediaWiki\Html\Html\openElement().

◆ namespaceSelectorOptions()

static MediaWiki\Html\Html::namespaceSelectorOptions ( array $params = [])
static

Helper for Html::namespaceSelector().

Parameters
array$paramsSee Html::namespaceSelector()
Returns
array

Definition at line 874 of file Html.php.

References $params, $wgLang, MediaWiki\MediaWikiServices\getInstance(), NS_MAIN, and wfMessage().

Referenced by MediaWiki\Html\Html\namespaceSelector().

◆ noticeBox()

static MediaWiki\Html\Html::noticeBox ( $html,
$className,
$heading = '',
$iconClassName = '' )
static

Return the HTML for a notice message box.

Since
1.38
Parameters
string$htmlof contents of notice
string | array$classNamecorresponding to notice
string$heading(optional)
string | array$iconClassName(optional) corresponding to notice icon
Returns
string of HTML representing the notice

Definition at line 740 of file Html.php.

◆ openElement()

static MediaWiki\Html\Html::openElement ( $element,
$attribs = [] )
static

Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/' in XML mode for empty elements).

Parameters
string$elementName of the element, e.g., 'a'
array$attribsAssociative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for further documentation.
Returns
string

Definition at line 240 of file Html.php.

References MediaWiki\Html\Html\expandAttributes(), and wfWarn().

Referenced by MediaWiki\Html\Html\htmlHeader(), MediaWiki\Html\Html\namespaceSelector(), and MediaWiki\Html\Html\rawElement().

◆ radio()

static MediaWiki\Html\Html::radio ( $name,
$checked = false,
array $attribs = [] )
static

Convenience function to produce a radio button (input element with type=radio)

Parameters
string$nameName attribute
bool$checkedWhether the radio button is checked or not
array$attribsArray of additional attributes
Returns
string Raw HTML

Definition at line 799 of file Html.php.

References MediaWiki\Html\Html\input().

◆ rawElement()

static MediaWiki\Html\Html::rawElement ( $element,
$attribs = [],
$contents = '' )
static

Returns an HTML element in a string.

The major advantage here over manually typing out the HTML is that it will escape all attribute values.

This is quite similar to Xml::tags(), but it implements some useful HTML-specific logic. For instance, there is no $allowShortTag parameter: the closing tag is magically omitted if $element has an empty content model.

Parameters
string$elementThe element's name, e.g., 'a'
array$attribsAssociative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for further documentation.
string$contentsThe raw HTML contents of the element: not escaped!
Returns
string Raw HTML

Definition at line 191 of file Html.php.

References MediaWiki\Html\Html\closeElement(), and MediaWiki\Html\Html\openElement().

Referenced by MediaWiki\Html\Html\element(), MediaWiki\Html\ListToggle\getHTML(), MediaWiki\Html\Html\inlineScript(), and MediaWiki\Html\Html\inlineStyle().

◆ srcSet()

static MediaWiki\Html\Html::srcSet ( array $urls)
static

Generate a srcset attribute value.

Generates a srcset attribute value from an array mapping pixel densities to URLs. A trailing 'x' in pixel density values is optional.

Note
srcset width and height values are not supported.
See also
https://html.spec.whatwg.org/#attr-img-srcset
Example:
Html::srcSet( [
'1x' => 'standard.jpeg',
'1.5x' => 'large.jpeg',
'3x' => 'extra-large.jpeg',
] );
// gives 'standard.jpeg 1x, large.jpeg 1.5x, extra-large.jpeg 2x'
Parameters
string[]$urls
Returns
string

Definition at line 1070 of file Html.php.

◆ submitButton()

static MediaWiki\Html\Html::submitButton ( $contents,
array $attrs = [],
array $modifiers = [] )
static

Returns an HTML input element in a string.

Parameters
string$contentsPlain text label for the button value
array$attrsAssociative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for further documentation.
string[]$modifiersUnused
Returns
string Raw HTML

Definition at line 163 of file Html.php.

References MediaWiki\Html\Html\element().

◆ successBox()

static MediaWiki\Html\Html::successBox ( $html,
$className = '' )
static

Return a success box.

Since
1.31
1.34 $className optional parameter added
Parameters
string$htmlof contents of box
string$className(optional) corresponding to box
Returns
string of HTML representing a success box.

Definition at line 785 of file Html.php.

◆ textarea()

static MediaWiki\Html\Html::textarea ( $name,
$value = '',
array $attribs = [] )
static

Convenience function to produce a <textarea> element.

This supports leaving out the cols= and rows= which Xml requires and are required by HTML4/XHTML but not required by HTML5.

Parameters
string$nameName attribute
string$valueValue attribute
array$attribsAssociative array of miscellaneous extra attributes, passed to Html::element()
Returns
string Raw HTML

Definition at line 854 of file Html.php.

References MediaWiki\Html\Html\element().

◆ warningBox()

static MediaWiki\Html\Html::warningBox ( $html,
$className = '' )
static

Return a warning box.

Since
1.31
1.34 $className optional parameter added
Parameters
string$htmlof contents of box
string$className(optional) corresponding to box
Returns
string of HTML representing a warning box.

Definition at line 756 of file Html.php.


The documentation for this class was generated from the following file: