MediaWiki  REL1_22
Html Class Reference

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

List of all members.

Static Public Member Functions

static closeElement ($element)
 Returns "</$element>", except if $wgWellFormedXml is off, in which case it returns the empty string when that's guaranteed to be safe.
static element ($element, $attribs=array(), $contents= '')
 Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
static expandAttributes ($attribs)
 Given an associative array of element attributes, generate a string to stick after the element name in HTML output.
static hidden ($name, $value, $attribs=array())
 Convenience function to produce an input element with type=hidden.
static htmlHeader ($attribs=array())
 Constructs the opening html-tag with necessary doctypes depending on global variables.
static infoBox ($text, $icon, $alt, $class=false, $useStylePath=true)
 Get HTML for an info box with an icon.
static inlineScript ($contents)
 Output a "<script>" tag with the given contents.
static inlineStyle ($contents, $media= 'all')
 Output a "<style>" tag with the given contents for the given media type (if any).
static input ($name, $value= '', $type= 'text', $attribs=array())
 Convenience function to produce an "<input>" element.
static isXmlMimeType ($mimetype)
 Determines if the given mime type is xml.
static linkedScript ($url)
 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 namespaceSelector (array $params=array(), array $selectAttribs=array())
 Build a drop-down box for selecting a namespace.
static openElement ($element, $attribs=array())
 Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/' in XML mode for empty elements).
static rawElement ($element, $attribs=array(), $contents= '')
 Returns an HTML element in a string.
static srcSet ($urls)
 Generate a srcset attribute value from an array mapping pixel densities to URLs.
static textarea ($name, $value= '', $attribs=array())
 Convenience function to produce an "<input>" element.

Static Private Member Functions

static dropDefaults ($element, $attribs)
 Given an element name and an associative array of element attributes, return an array that is functionally identical to the input array, but possibly smaller.

Static Private Attributes

static $boolAttribs
static $voidElements

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 mimetype then output should be valid XHTML5. $wgWellFormedXml: If this is set to true, then all output should be well-formed XML (quotes on attributes, self-closing tags, etc.).

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 50 of file Html.php.


Member Function Documentation

static Html::closeElement ( element) [static]
static Html::dropDefaults ( element,
attribs 
) [static, private]

Given an element name and an associative array of element attributes, return an array that is functionally identical to the input array, but possibly smaller.

In particular, attributes might be stripped if they are given their default values.

This method is not guaranteed to remove all redundant attributes, only some common ones and some others selected arbitrarily at random. It only guarantees that the output array should be functionally identical to the input array (currently per the HTML 5 draft as of 2009-09-06).

Parameters:
string$elementName of the element, e.g., 'a'
array$attribsAssociative array of attributes, e.g., array( 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for further documentation.
Returns:
array An array of attributes functionally identical to $attribs

Definition at line 275 of file Html.php.

References $attribs, $value, array(), and as.

static Html::element ( element,
attribs = array(),
contents = '' 
) [static]

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

Parameters:
$elementstring
$attribsarray
$contentsstring
Returns:
string

Definition at line 148 of file Html.php.

References $attribs, array(), and rawElement().

Referenced by MWDebug\deprecated(), UserloginTemplate\execute(), LinkSearchPage\execute(), MonoBookTemplate\execute(), FileDuplicateSearchPage\execute(), WebInstaller_DBConnect\execute(), PageQueryPage\formatResult(), PopularPagesPage\formatResult(), UnwatchedpagesPage\formatResult(), FewestrevisionsPage\formatResult(), MostlinkedCategoriesPage\formatResult(), ShortPagesPage\formatResult(), MostcategoriesPage\formatResult(), MostinterwikisPage\formatResult(), MostlinkedTemplatesPage\formatResult(), MostlinkedPage\formatResult(), SpecialPagesWithProp\formatResult(), SpecialProtectedtitles\formatRow(), SpecialProtectedpages\formatRow(), WebInstaller_Options\getCCChooser(), WebInstaller_Options\getCCDoneBox(), PostgresInstaller\getConnectForm(), OracleInstaller\getConnectForm(), MysqlInstaller\getConnectForm(), FeedUtils\getDiffLink(), SpecialVersion\getEntryPointInfo(), WithoutInterwikiPage\getPageHeader(), SpecialLog\getRevisionButton(), HistoryPager\getRevisionButton(), infoBox(), input(), linkedScript(), linkedStyle(), Linker\makeExternalImage(), Skin\makeFooterIcon(), SpecialBookSources\makeForm(), InfoAction\makeHeader(), SpecialBookSources\makeListItem(), BaseTemplate\makeSearchButton(), BaseTemplate\makeSearchInput(), Linker\makeThumbLink2(), namespaceSelector(), InfoAction\onView(), SpecialVersion\openExtType(), SpecialSpecialpages\outputPageList(), InfoAction\pageInfo(), ImportReporter\reportNotice(), SpecialBlockList\showList(), SpecialPrefixindex\showPrefixChunk(), SpecialUploadStash\showUploads(), HtmlTest\testDropDefaults(), HtmlTest\testElementBasics(), HtmlTest\testHtmlElementAcceptsNewHtml5TypesInHtml5Mode(), textarea(), and wfHtmlValidationHandler().

static Html::expandAttributes ( attribs) [static]

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

Like array( 'href' => 'http://www.mediawiki.org/' ) becomes something like ' href="http://www.mediawiki.org"'. Again, this is like Xml::expandAttributes(), but it implements some HTML-specific logic. For instance, it will omit quotation marks if $wgWellFormedXml is false, and will treat boolean attributes specially.

Attributes that should contain space-separated lists (such as 'class') 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', array(
         'class' => array( 'foo', 'bar' )
     ) );
     // gives '<em class="foo bar"></em>'
Associative array
     Html::element( 'em', array(
         'class' => array( 'foo', 'bar', 'foo' => false, 'quux' => true )
     ) );
     // gives '<em class="bar quux"></em>'
Parameters:
array$attribsAssociative array of attributes, e.g., array( 'href' => 'http://www.mediawiki.org/' ). Values will be HTML-escaped. A value of false means to omit the attribute. For boolean attributes, you can omit the key, e.g., array( 'checked' ) instead of array( '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 419 of file Html.php.

References $attribs, $key, $ret, $value, array(), as, and global.

Referenced by openElement(), and HtmlTest\testFormValidationBlacklist().

static Html::htmlHeader ( attribs = array()) [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 800 of file Html.php.

References $attribs, $html, $ret, as, global, isXmlMimeType(), and openElement().

Referenced by wfHtmlValidationHandler().

static Html::infoBox ( text,
icon,
alt,
class = false,
useStylePath = true 
) [static]

Get HTML for an info box with an icon.

Parameters:
string$textwikitext, get this with wfMessage()->plain()
string$iconicon name, file in skins/common/images
string$altalternate text for the icon
string$classadditional class name to add to the wrapper div
$useStylePath
Returns:
string

Definition at line 864 of file Html.php.

References $s, array(), closeElement(), element(), global, and openElement().

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

Definition at line 570 of file Html.php.

References array(), global, and rawElement().

Referenced by ProtectionForm\buildCleanupScript(), OutputPage\getBottomScripts(), MWDebug\getDebugHTML(), and Skin\makeVariablesScript().

static Html::inlineStyle ( contents,
media = 'all' 
) [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
$mediamixed A media type string, like 'screen'
Returns:
string Raw HTML

Definition at line 604 of file Html.php.

References array(), global, and rawElement().

Referenced by wfHtmlValidationHandler().

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

Convenience function to produce an "<input>" element.

This supports the new HTML5 input types and attributes.

Parameters:
$namestring name attribute
$valuemixed value attribute
$typestring type attribute
array$attribsAssociative array of miscellaneous extra attributes, passed to Html::element()
Returns:
string Raw HTML

Definition at line 645 of file Html.php.

References $attribs, $name, $value, and element().

Referenced by UserloginTemplate\execute(), UsercreateTemplate\execute(), SpecialRandomInCategory\execute(), ImageListPager\getForm(), UsersPager\getPageHeader(), hidden(), SpecialChangePassword\pretty(), and SpecialImport\showForm().

static Html::isXmlMimeType ( mimetype) [static]

Determines if the given mime type is xml.

Parameters:
$mimetypestring MimeType
Returns:
Boolean

Definition at line 845 of file Html.php.

Referenced by htmlHeader().

static Html::linkedScript ( url) [static]

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

Parameters:
$urlstring
Returns:
string Raw HTML

Definition at line 589 of file Html.php.

References array(), and element().

Referenced by ResourceLoaderStartUpModule\getScript().

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:
$urlstring
$mediamixed A media type string, like 'screen'
Returns:
string Raw HTML

Definition at line 625 of file Html.php.

References array(), and element().

Referenced by WebInstallerOutput\getCssUrl().

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

Build a drop-down box for selecting a namespace.

Parameters:
$paramsarray:
  • 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 706 of file Html.php.

References $options, $params, $ret, $wgContLang, array(), as, closeElement(), element(), global, openElement(), and wfMessage().

Referenced by SpecialWatchlist\execute(), SpecialExport\execute(), LinkSearchPage\execute(), SpecialNewpages\form(), SpecialProtectedtitles\getNamespaceMenu(), SpecialProtectedpages\getNamespaceMenu(), SpecialRecentChanges\namespaceFilterForm(), SpecialPrefixindex\namespacePrefixForm(), and SpecialImport\showForm().

static Html::rawElement ( element,
attribs = array(),
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. If you're hardcoding all the attributes, or there are none, you should probably just type out the html element yourself.

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. If $wgWellFormedXml is false, then a few bytes will be shaved off the HTML output as well.

Parameters:
string$elementThe element's name, e.g., 'a'
array$attribsAssociative array of attributes, e.g., array( 'href' => 'http://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 124 of file Html.php.

References $attribs, closeElement(), global, and openElement().

Referenced by InfoAction\addRow(), InfoAction\addTable(), SpecialBlock\alterForm(), ChangeTags\buildTagFilterSelector(), SpecialEditWatchlist\buildTools(), MWDebug\deprecated(), element(), SpecialListGroupRights\execute(), SpecialRandomInCategory\execute(), SpecialListUsers\execute(), DateFormatter\formatDate(), SpecialMergeHistory\formatRevisionRow(), SpecialProtectedtitles\formatRow(), SpecialProtectedpages\formatRow(), SpecialStatistics\formatRow(), UsersPager\formatRow(), BlockListPager\formatValue(), Skin\getCachedNotice(), Skin\getCopyright(), SpecialVersion\getEntryPointInfo(), SpecialJavaScriptTest\getFrameworkListHtml(), SpecialProtectedpages\getNamespaceMenu(), Block\getRedactedName(), inlineScript(), inlineStyle(), Linker\link(), Linker\makeExternalLink(), Skin\makeFooterIcon(), InfoAction\makeHeader(), BaseTemplate\makeLink(), SpecialBookSources\makeListItem(), BaseTemplate\makeListItem(), BaseTemplate\makeSearchButton(), Linker\makeThumbLink2(), CreditsAction\onView(), SkinTemplate\outputPage(), SpecialSpecialpages\outputPageList(), InfoAction\pageInfo(), SpecialBlock\postText(), SpecialBlock\preText(), SpecialRecentChanges\setTopText(), SpecialBlockList\showList(), SpecialPrefixindex\showPrefixChunk(), SpecialUploadStash\showUploads(), and wfHtmlValidationHandler().

static Html::srcSet ( urls) [static]

Generate a srcset attribute value from an array mapping pixel densities to URLs.

Note that srcset supports width and height values as well, which are not used here.

Parameters:
array$urls
Returns:
string

Definition at line 902 of file Html.php.

References $urls, array(), and as.

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

Convenience function to produce an "<input>" element.

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

Parameters:
$namestring name attribute
$valuestring value attribute
array$attribsAssociative array of miscellaneous extra attributes, passed to Html::element()
Returns:
string Raw HTML

Definition at line 678 of file Html.php.

References $attribs, $name, $value, and element().


Member Data Documentation

Html::$boolAttribs [static, private]
Initial value:
 array(
        'async',
        'autofocus',
        'autoplay',
        'checked',
        'controls',
        'default',
        'defer',
        'disabled',
        'formnovalidate',
        'hidden',
        'ismap',
        'itemscope',
        'loop',
        'multiple',
        'muted',
        'novalidate',
        'open',
        'pubdate',
        'readonly',
        'required',
        'reversed',
        'scoped',
        'seamless',
        'selected',
        'truespeed',
        'typemustmatch',
        
        'itemscope',
    )

Definition at line 73 of file Html.php.

Html::$voidElements [static, private]
Initial value:
 array(
        'area',
        'base',
        'br',
        'col',
        'command',
        'embed',
        'hr',
        'img',
        'input',
        'keygen',
        'link',
        'meta',
        'param',
        'source',
        'track',
        'wbr',
    )

Definition at line 52 of file Html.php.


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