summaryrefslogtreecommitdiff
path: root/includes/Html.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/Html.php')
-rw-r--r--includes/Html.php102
1 files changed, 93 insertions, 9 deletions
diff --git a/includes/Html.php b/includes/Html.php
index 6c802ca3..be9a1e1b 100644
--- a/includes/Html.php
+++ b/includes/Html.php
@@ -107,8 +107,8 @@ class Html {
* features might be added, like allowing arrays for the values of
* attributes like class= and media=.
*
- * @param $element string The element's name, e.g., 'a'
- * @param $attribs array Associative array of attributes, e.g., array(
+ * @param $element string The element's name, e.g., 'a'
+ * @param $attribs array Associative array of attributes, e.g., array(
* 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
* further documentation.
* @param $contents string The raw HTML contents of the element: *not*
@@ -132,6 +132,12 @@ class Html {
/**
* Identical to rawElement(), but HTML-escapes $contents (like
* Xml::element()).
+ *
+ * @param $element string
+ * @param $attribs array
+ * @param $contents string
+ *
+ * @return string
*/
public static function element( $element, $attribs = array(), $contents = '' ) {
return self::rawElement( $element, $attribs, strtr( $contents, array(
@@ -145,6 +151,11 @@ class Html {
/**
* Identical to rawElement(), but has no third parameter and omits the end
* tag (and the self-closing '/' in XML mode for empty elements).
+ *
+ * @param $element string
+ * @param $attribs array
+ *
+ * @return string
*/
public static function openElement( $element, $attribs = array() ) {
global $wgHtml5, $wgWellFormedXml;
@@ -180,15 +191,18 @@ class Html {
'button',
'search',
);
+
if ( isset( $attribs['type'] )
&& !in_array( $attribs['type'], $validTypes ) ) {
unset( $attribs['type'] );
}
+
if ( isset( $attribs['type'] ) && $attribs['type'] == 'search'
&& !$wgHtml5 ) {
unset( $attribs['type'] );
}
}
+
if ( !$wgHtml5 && $element == 'textarea' && isset( $attribs['maxlength'] ) ) {
unset( $attribs['maxlength'] );
}
@@ -201,6 +215,7 @@ class Html {
* Returns "</$element>", except if $wgWellFormedXml is off, in which case
* it returns the empty string when that's guaranteed to be safe.
*
+ * @since 1.17
* @param $element string Name of the element, e.g., 'a'
* @return string A closing tag, if required
*/
@@ -351,7 +366,7 @@ class Html {
$ret = '';
$attribs = (array)$attribs;
foreach ( $attribs as $key => $value ) {
- if ( $value === false ) {
+ if ( $value === false || is_null( $value ) ) {
continue;
}
@@ -425,7 +440,8 @@ class Html {
# Apparently we need to entity-encode \n, \r, \t, although the
# spec doesn't mention that. Since we're doing strtr() anyway,
# and we don't need <> escaped here, we may as well not call
- # htmlspecialchars(). FIXME: verify that we actually need to
+ # htmlspecialchars().
+ # @todo FIXME: Verify that we actually need to
# escape \n\r\t here, and explain why, exactly.
#
# We could call Sanitizer::encodeAttribute() for this, but we
@@ -440,8 +456,8 @@ class Html {
);
if ( $wgWellFormedXml ) {
# This is allowed per spec: <http://www.w3.org/TR/xml/#NT-AttValue>
- # But reportedly it breaks some XML tools? FIXME: is this
- # really true?
+ # But reportedly it breaks some XML tools?
+ # @todo FIXME: Is this really true?
$map['<'] = '&lt;';
}
$ret .= " $key=$quote" . strtr( $value, $map ) . $quote;
@@ -462,12 +478,15 @@ class Html {
global $wgHtml5, $wgJsMimeType, $wgWellFormedXml;
$attrs = array();
+
if ( !$wgHtml5 ) {
$attrs['type'] = $wgJsMimeType;
}
+
if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
$contents = "/*<![CDATA[*/$contents/*]]>*/";
}
+
return self::rawElement( 'script', $attrs, $contents );
}
@@ -482,9 +501,11 @@ class Html {
global $wgHtml5, $wgJsMimeType;
$attrs = array( 'src' => $url );
+
if ( !$wgHtml5 ) {
$attrs['type'] = $wgJsMimeType;
}
+
return self::element( 'script', $attrs );
}
@@ -503,6 +524,7 @@ class Html {
if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
$contents = "/*<![CDATA[*/$contents/*]]>*/";
}
+
return self::rawElement( 'style', array(
'type' => 'text/css',
'media' => $media,
@@ -574,16 +596,29 @@ class Html {
*/
public static function textarea( $name, $value = '', $attribs = array() ) {
global $wgHtml5;
+
$attribs['name'] = $name;
+
if ( !$wgHtml5 ) {
if ( !isset( $attribs['cols'] ) ) {
$attribs['cols'] = "";
}
+
if ( !isset( $attribs['rows'] ) ) {
$attribs['rows'] = "";
}
}
- return self::element( 'textarea', $attribs, $value );
+
+ if (substr($value, 0, 1) == "\n") {
+ // Workaround for bug 12130: browsers eat the initial newline
+ // assuming that it's just for show, but they do keep the later
+ // newlines, which we may want to preserve during editing.
+ // Prepending a single newline
+ $spacedValue = "\n" . $value;
+ } else {
+ $spacedValue = $value;
+ }
+ return self::element( 'textarea', $attribs, $spacedValue );
}
/**
@@ -597,30 +632,38 @@ class Html {
public static function htmlHeader( $attribs = array() ) {
$ret = '';
- global $wgMimeType, $wgOutputEncoding;
+ global $wgMimeType;
+
if ( self::isXmlMimeType( $wgMimeType ) ) {
- $ret .= "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\n";
+ $ret .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n";
}
global $wgHtml5, $wgHtml5Version, $wgDocType, $wgDTD;
global $wgXhtmlNamespaces, $wgXhtmlDefaultNamespace;
+
if ( $wgHtml5 ) {
$ret .= "<!DOCTYPE html>\n";
+
if ( $wgHtml5Version ) {
$attribs['version'] = $wgHtml5Version;
}
} else {
$ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\" \"$wgDTD\">\n";
$attribs['xmlns'] = $wgXhtmlDefaultNamespace;
+
foreach ( $wgXhtmlNamespaces as $tag => $ns ) {
$attribs["xmlns:$tag"] = $ns;
}
}
+
$html = Html::openElement( 'html', $attribs );
+
if ( $html ) {
$html .= "\n";
}
+
$ret .= $html;
+
return $ret;
}
@@ -640,4 +683,45 @@ class Html {
return false;
}
}
+
+ /**
+ * Get HTML for an info box with an icon.
+ *
+ * @param $text String: wikitext, get this with wfMsgNoTrans()
+ * @param $icon String: icon name, file in skins/common/images
+ * @param $alt String: alternate text for the icon
+ * @param $class String: additional class name to add to the wrapper div
+ * @param $useStylePath
+ *
+ * @return string
+ */
+ static function infoBox( $text, $icon, $alt, $class = false, $useStylePath = true ) {
+ global $wgStylePath;
+
+ if ( $useStylePath ) {
+ $icon = $wgStylePath.'/common/images/'.$icon;
+ }
+
+ $s = Html::openElement( 'div', array( 'class' => "mw-infobox $class") );
+
+ $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ).
+ Html::element( 'img',
+ array(
+ 'src' => $icon,
+ 'alt' => $alt,
+ )
+ ).
+ Html::closeElement( 'div' );
+
+ $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ).
+ $text.
+ Html::closeElement( 'div' );
+ $s .= Html::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
+
+ $s .= Html::closeElement( 'div' );
+
+ $s .= Html::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
+
+ return $s;
+ }
}