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 rawElement ($element, $attribs=array(), $contents= '')
 Returns an HTML element in a string.
static element ($element, $attribs=array(), $contents= '')
 Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
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 expandAttributes ($attribs)
 Given an associative array of element attributes, generate a string to stick after the element name in HTML output.
static inlineScript ($contents)
 Output a <script> tag with the given contents.
static linkedScript ($url)
 Output a <script> tag linking to the given URL, e.g., <script src="foo.js"></script>.
static inlineStyle ($contents, $media= 'all')
 Output a <style> tag with the given contents for the given media type (if any).
static linkedStyle ($url, $media= 'all')
 Output a <link rel="stylesheet"> linking to the given URL for the given media type (if any).
static input ($name, $value= '', $type= 'text', $attribs=array())
 Convenience function to produce an <input> element.
static hidden ($name, $value, $attribs=array())
 Convenience function to produce an input element with type=hidden, like Xml::hidden.
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 $voidElements
static $boolAttribs


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:

$wgHtml5: If this is set to false, then all output should be valid XHTML 1.0 Transitional. $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.

Definition at line 42 of file Html.php.


Member Function Documentation

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:
$element string Name of the element, e.g., 'a'
$attribs array Associative 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 215 of file Html.php.

References $attrib, and $wgHtml5.

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

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.

Parameters:
$attribs array Associative 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 316 of file Html.php.

References $wgHtml5, and $wgWellFormedXml.

Referenced by Linker::makeExternalLink().

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

Convenience function to produce an input element with type=hidden, like Xml::hidden.

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

Definition at line 511 of file Html.php.

References $name, and input().

Referenced by SpecialSearch::shortDialog().

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:
$contents string JavaScript
Returns:
string Raw HTML

Definition at line 413 of file Html.php.

References $wgHtml5, $wgJsMimeType, $wgWellFormedXml, and rawElement().

Referenced by OutputPage::addInlineScript(), bottomScripts(), EditPage::getEditToolbar(), Skin::makeVariablesScript(), SpecialSearch::searchFocus(), and Linker::tocList().

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

Definition at line 452 of file Html.php.

References $wgWellFormedXml, and rawElement().

Referenced by OutputPage::addInlineStyle(), and OutputPage::headElement().

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, and will silently strip them if $wgHtml5 is false.

Parameters:
$name string name attribute
$value mixed value attribute
$type string type attribute
$attribs array Associative array of miscellaneous extra attributes, passed to Html::element()
Returns:
string Raw HTML

Definition at line 493 of file Html.php.

References $name, and element().

Referenced by Article::confirmDelete(), UserloginTemplate::execute(), DeletedContributionsPage::getForm(), SpecialContributions::getForm(), HTMLSelectOrOtherField::getInputHTML(), EditPage::getSummaryInput(), hidden(), SpecialResetpass::pretty(), MonoBookTemplate::searchBox(), and SpecialSearch::shortDialog().

static Html::linkedScript ( url  )  [static]

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

Parameters:
$url string
Returns:
string Raw HTML

Definition at line 433 of file Html.php.

References $url, $wgHtml5, $wgJsMimeType, and element().

Referenced by OutputPage::addScriptFile(), EnhancedChangesList::beginRecentChangesList(), and OutputPage::getHeadScripts().

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

Definition at line 472 of file Html.php.

References $url, and element().

Referenced by OutputPage::styleLink().

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

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

Definition at line 139 of file Html.php.

Referenced by OutputPage::headElement(), and rawElement().

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 type out the string 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. In the future, other HTML-specific features might be added, like allowing arrays for the values of attributes like class= and media=.

Parameters:
$element string The element's name, e.g., 'a'
$attribs array Associative array of attributes, e.g., array( 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for further documentation.
$contents string The raw HTML contents of the element: *not* escaped!
Returns:
string Raw HTML

Definition at line 108 of file Html.php.

References $wgWellFormedXml, and openElement().

Referenced by Article::delete(), Article::doDelete(), element(), SpecialActiveUsers::execute(), DateFormatter::formatDate(), HTMLRadioField::formatOptions(), HTMLMultiSelectField::formatOptions(), ProtectedPagesForm::formatRow(), DeletedContribsPager::formatRow(), ActiveUsersPager::formatRow(), Licenses::getInputHTML(), UploadSourceField::getLabelHtml(), HistoryPager::historyLine(), inlineScript(), inlineStyle(), Linker::link(), OutputPage::showLagWarning(), IPUnblockForm::showList(), and LogEventsList::showLogExtract().

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 and will silently set cols="" and rows="" if $wgHtml5 is false and cols and rows are omitted (HTML4 validates present but empty cols="" and rows="" as valid).

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

Definition at line 528 of file Html.php.

References $name, $wgHtml5, and element().

Referenced by EditPage::showTextbox().


Member Data Documentation

Html::$boolAttribs [static, private]

Initial value:

 array(
                'async',
                'autobuffer',
                'autofocus',
                'autoplay',
                'checked',
                'controls',
                'defer',
                'disabled',
                'formnovalidate',
                'hidden',
                'ismap',
                'loop',
                'multiple',
                'novalidate',
                'open',
                'readonly',
                'required',
                'reversed',
                'scoped',
                'seamless',
        )

Definition at line 63 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',
        )

Definition at line 44 of file Html.php.


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

Generated on Sat Apr 30 22:57:39 2011 for MediaWiki by  doxygen 1.5.9