BlueSpice MediaWiki master
 All Classes Namespaces Files Functions Variables Groups Pages
Html Class Reference

Static Public Member Functions

static buttonAttributes (array $attrs, array $modifiers=[])
 
static getTextInputAttributes (array $attrs)
 
static linkButton ($contents, array $attrs, array $modifiers=[])
 
static submitButton ($contents, array $attrs, array $modifiers=[])
 
static rawElement ($element, $attribs=[], $contents= '')
 
static element ($element, $attribs=[], $contents= '')
 
static openElement ($element, $attribs=[])
 
static closeElement ($element)
 
static expandAttributes (array $attribs)
 
static inlineScript ($contents)
 
static linkedScript ($url)
 
static inlineStyle ($contents, $media= 'all', $attribs=[])
 
static linkedStyle ($url, $media= 'all')
 
static input ($name, $value= '', $type= 'text', array $attribs=[])
 
static check ($name, $checked=false, array $attribs=[])
 
static warningBox ($html)
 
static errorBox ($html, $heading= '')
 
static successBox ($html)
 
static radio ($name, $checked=false, array $attribs=[])
 
static label ($label, $id, array $attribs=[])
 
static hidden ($name, $value, array $attribs=[])
 
static textarea ($name, $value= '', array $attribs=[])
 
static namespaceSelectorOptions (array $params=[])
 
static namespaceSelector (array $params=[], array $selectAttribs=[])
 
static htmlHeader (array $attribs=[])
 
static isXmlMimeType ($mimetype)
 
static infoBox ($text, $icon, $alt, $class= '')
 
static srcSet (array $urls)
 

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

Member Function Documentation

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

Modifies a set of attributes meant for button elements and apply a set of default attributes when $wgUseMediaWikiUIEverywhere enabled.

Parameters
array$attrsHTML attributes in an associative array
string[]$modifiers classes to add to the button
See Also
https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
Returns
array $attrs A modified attribute array
static 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
static Html::closeElement (   $element)
static

Returns "</$element>"

Since
1.17
Parameters
string$elementName of the element, e.g., 'a'
Returns
string A closing tag
static 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
static Html::errorBox (   $html,
  $heading = '' 
)
static

Return an error box.

Since
1.31
Parameters
string$htmlof contents of error box
string$heading(optional)
Returns
string of HTML representing an error box.
static 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.
Exceptions
MWExceptionIf an attribute that doesn't allow lists is set to an array
Returns
string HTML fragment that goes between element name and '>' (starting with a space if at least one attribute is output)
static Html::getTextInputAttributes ( array  $attrs)
static

Modifies a set of attributes meant for text input elements and apply a set of default attributes. Removes size attribute when $wgUseMediaWikiUIEverywhere enabled.

Parameters
array$attrsAn attribute array.
Returns
array $attrs A modified attribute array
static Html::hidden (   $name,
  $value,
array  $attribs = [] 
)
static

Convenience function to produce an input element with type=hidden

Parameters
string$nameName attribute
string$valueValue attribute
array$attribsAssociative array of miscellaneous extra attributes, passed to Html::element()
Returns
string Raw HTML
static 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
static Html::infoBox (   $text,
  $icon,
  $alt,
  $class = '' 
)
static

Get HTML for an info box with an icon.

Parameters
string$textWikitext, get this with wfMessage()->plain()
string$iconPath to icon file (used as 'src' attribute)
string$altAlternate text for the icon
string$classAdditional class name to add to the wrapper div
Returns
string
static Html::inlineScript (   $contents)
static

Output a "<script>" tag with the given contents.

Todo:
do some useful escaping as well, like if $contents contains literal "</script>" or (for XML) literal "]]>".
Parameters
string$contentsJavaScript
Returns
string Raw HTML
static 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
static 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
static Html::isXmlMimeType (   $mimetype)
static

Determines if the given MIME type is xml.

Parameters
string$mimetypeMIME type
Returns
bool
static 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
static Html::linkButton (   $contents,
array  $attrs,
array  $modifiers = [] 
)
static

Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enabled).

Parameters
string$contentsThe raw HTML contents of the element: not escaped!
array$attrsAssociative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for further documentation.
string[]$modifiers classes to add to the button
See Also
https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
Returns
string Raw HTML
static Html::linkedScript (   $url)
static

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

Parameters
string$url
Returns
string Raw HTML
static 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
static 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.
static Html::namespaceSelectorOptions ( array  $params = [])
static

Helper for Html::namespaceSelector().

Parameters
array$paramsSee Html::namespaceSelector()
Returns
array
static 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
static 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
static 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
static 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:
'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
static Html::submitButton (   $contents,
array  $attrs,
array  $modifiers = [] 
)
static

Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enabled).

Parameters
string$contentsThe raw HTML contents of the element: not escaped!
array$attrsAssociative array of attributes, e.g., [ 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for further documentation.
string[]$modifiers classes to add to the button
See Also
https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
Returns
string Raw HTML
static Html::successBox (   $html)
static

Return a success box.

Since
1.31
Parameters
string$htmlof contents of box
Returns
string of HTML representing a success box.
static 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
static Html::warningBox (   $html)
static

Return a warning box.

Since
1.31
Parameters
string$htmlof contents of box
Returns
string of HTML representing a warning box.

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