Follow up to r97128;
[lhc/web/wiklou.git] / includes / Html.php
1 <?php
2 /**
3 * Collection of methods to generate HTML content
4 *
5 * Copyright © 2009 Aryeh Gregor
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 /**
27 * This class is a collection of static functions that serve two purposes:
28 *
29 * 1) Implement any algorithms specified by HTML5, or other HTML
30 * specifications, in a convenient and self-contained way.
31 *
32 * 2) Allow HTML elements to be conveniently and safely generated, like the
33 * current Xml class but a) less confused (Xml supports HTML-specific things,
34 * but only sometimes!) and b) not necessarily confined to XML-compatible
35 * output.
36 *
37 * There are two important configuration options this class uses:
38 *
39 * $wgHtml5: If this is set to false, then all output should be valid XHTML 1.0
40 * Transitional.
41 * $wgWellFormedXml: If this is set to true, then all output should be
42 * well-formed XML (quotes on attributes, self-closing tags, etc.).
43 *
44 * This class is meant to be confined to utility functions that are called from
45 * trusted code paths. It does not do enforcement of policy like not allowing
46 * <a> elements.
47 *
48 * @since 1.16
49 */
50 class Html {
51 # List of void elements from HTML5, section 8.1.2 as of 2011-08-12
52 private static $voidElements = array(
53 'area',
54 'base',
55 'br',
56 'col',
57 'command',
58 'embed',
59 'hr',
60 'img',
61 'input',
62 'keygen',
63 'link',
64 'meta',
65 'param',
66 'source',
67 'track',
68 'wbr',
69 );
70
71 # Boolean attributes, which may have the value omitted entirely. Manually
72 # collected from the HTML5 spec as of 2011-08-12.
73 private static $boolAttribs = array(
74 'async',
75 'autofocus',
76 'autoplay',
77 'checked',
78 'controls',
79 'default',
80 'defer',
81 'disabled',
82 'formnovalidate',
83 'hidden',
84 'ismap',
85 'itemscope',
86 'loop',
87 'multiple',
88 'muted',
89 'novalidate',
90 'open',
91 'pubdate',
92 'readonly',
93 'required',
94 'reversed',
95 'scoped',
96 'seamless',
97 'selected',
98 'truespeed',
99 'typemustmatch',
100 # HTML5 Microdata
101 'itemscope',
102 );
103
104 /**
105 * Returns an HTML element in a string. The major advantage here over
106 * manually typing out the HTML is that it will escape all attribute
107 * values. If you're hardcoding all the attributes, or there are none, you
108 * should probably just type out the html element yourself.
109 *
110 * This is quite similar to Xml::tags(), but it implements some useful
111 * HTML-specific logic. For instance, there is no $allowShortTag
112 * parameter: the closing tag is magically omitted if $element has an empty
113 * content model. If $wgWellFormedXml is false, then a few bytes will be
114 * shaved off the HTML output as well.
115 *
116 * @param $element string The element's name, e.g., 'a'
117 * @param $attribs array Associative array of attributes, e.g., array(
118 * 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
119 * further documentation.
120 * @param $contents string The raw HTML contents of the element: *not*
121 * escaped!
122 * @return string Raw HTML
123 */
124 public static function rawElement( $element, $attribs = array(), $contents = '' ) {
125 global $wgWellFormedXml;
126 $start = self::openElement( $element, $attribs );
127 if ( in_array( $element, self::$voidElements ) ) {
128 if ( $wgWellFormedXml ) {
129 # Silly XML.
130 return substr( $start, 0, -1 ) . ' />';
131 }
132 return $start;
133 } else {
134 return "$start$contents" . self::closeElement( $element );
135 }
136 }
137
138 /**
139 * Identical to rawElement(), but HTML-escapes $contents (like
140 * Xml::element()).
141 *
142 * @param $element string
143 * @param $attribs array
144 * @param $contents string
145 *
146 * @return string
147 */
148 public static function element( $element, $attribs = array(), $contents = '' ) {
149 return self::rawElement( $element, $attribs, strtr( $contents, array(
150 # There's no point in escaping quotes, >, etc. in the contents of
151 # elements.
152 '&' => '&amp;',
153 '<' => '&lt;'
154 ) ) );
155 }
156
157 /**
158 * Identical to rawElement(), but has no third parameter and omits the end
159 * tag (and the self-closing '/' in XML mode for empty elements).
160 *
161 * @param $element string
162 * @param $attribs array
163 *
164 * @return string
165 */
166 public static function openElement( $element, $attribs = array() ) {
167 global $wgHtml5, $wgWellFormedXml;
168 $attribs = (array)$attribs;
169 # This is not required in HTML5, but let's do it anyway, for
170 # consistency and better compression.
171 $element = strtolower( $element );
172
173 # In text/html, initial <html> and <head> tags can be omitted under
174 # pretty much any sane circumstances, if they have no attributes. See:
175 # <http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#optional-tags>
176 if ( !$wgWellFormedXml && !$attribs
177 && in_array( $element, array( 'html', 'head' ) ) ) {
178 return '';
179 }
180
181 # Remove HTML5-only attributes if we aren't doing HTML5, and disable
182 # form validation regardless (see bug 23769 and the more detailed
183 # comment in expandAttributes())
184 if ( $element == 'input' ) {
185 # Whitelist of types that don't cause validation. All except
186 # 'search' are valid in XHTML1.
187 $validTypes = array(
188 'hidden',
189 'text',
190 'password',
191 'checkbox',
192 'radio',
193 'file',
194 'submit',
195 'image',
196 'reset',
197 'button',
198 'search',
199 );
200
201 if ( isset( $attribs['type'] )
202 && !in_array( $attribs['type'], $validTypes ) ) {
203 unset( $attribs['type'] );
204 }
205
206 if ( isset( $attribs['type'] ) && $attribs['type'] == 'search'
207 && !$wgHtml5 ) {
208 unset( $attribs['type'] );
209 }
210 }
211
212 if ( !$wgHtml5 && $element == 'textarea' && isset( $attribs['maxlength'] ) ) {
213 unset( $attribs['maxlength'] );
214 }
215
216 return "<$element" . self::expandAttributes(
217 self::dropDefaults( $element, $attribs ) ) . '>';
218 }
219
220 /**
221 * Returns "</$element>", except if $wgWellFormedXml is off, in which case
222 * it returns the empty string when that's guaranteed to be safe.
223 *
224 * @since 1.17
225 * @param $element string Name of the element, e.g., 'a'
226 * @return string A closing tag, if required
227 */
228 public static function closeElement( $element ) {
229 global $wgWellFormedXml;
230
231 $element = strtolower( $element );
232
233 # Reference:
234 # http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#optional-tags
235 if ( !$wgWellFormedXml && in_array( $element, array(
236 'html',
237 'head',
238 'body',
239 'li',
240 'dt',
241 'dd',
242 'tr',
243 'td',
244 'th',
245 ) ) ) {
246 return '';
247 }
248 return "</$element>";
249 }
250
251 /**
252 * Given an element name and an associative array of element attributes,
253 * return an array that is functionally identical to the input array, but
254 * possibly smaller. In particular, attributes might be stripped if they
255 * are given their default values.
256 *
257 * This method is not guaranteed to remove all redundant attributes, only
258 * some common ones and some others selected arbitrarily at random. It
259 * only guarantees that the output array should be functionally identical
260 * to the input array (currently per the HTML 5 draft as of 2009-09-06).
261 *
262 * @param $element string Name of the element, e.g., 'a'
263 * @param $attribs array Associative array of attributes, e.g., array(
264 * 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
265 * further documentation.
266 * @return array An array of attributes functionally identical to $attribs
267 */
268 private static function dropDefaults( $element, $attribs ) {
269 # Don't bother doing anything if we aren't outputting HTML5; it's too
270 # much of a pain to maintain two sets of defaults.
271 global $wgHtml5;
272 if ( !$wgHtml5 ) {
273 return $attribs;
274 }
275
276 static $attribDefaults = array(
277 'area' => array( 'shape' => 'rect' ),
278 'button' => array(
279 'formaction' => 'GET',
280 'formenctype' => 'application/x-www-form-urlencoded',
281 'type' => 'submit',
282 ),
283 'canvas' => array(
284 'height' => '150',
285 'width' => '300',
286 ),
287 'command' => array( 'type' => 'command' ),
288 'form' => array(
289 'action' => 'GET',
290 'autocomplete' => 'on',
291 'enctype' => 'application/x-www-form-urlencoded',
292 ),
293 'input' => array(
294 'formaction' => 'GET',
295 'type' => 'text',
296 'value' => '',
297 ),
298 'keygen' => array( 'keytype' => 'rsa' ),
299 'link' => array( 'media' => 'all' ),
300 'menu' => array( 'type' => 'list' ),
301 # Note: the use of text/javascript here instead of other JavaScript
302 # MIME types follows the HTML5 spec.
303 'script' => array( 'type' => 'text/javascript' ),
304 'style' => array(
305 'media' => 'all',
306 'type' => 'text/css',
307 ),
308 'textarea' => array( 'wrap' => 'soft' ),
309 );
310
311 $element = strtolower( $element );
312
313 foreach ( $attribs as $attrib => $value ) {
314 $lcattrib = strtolower( $attrib );
315 $value = strval( $value );
316
317 # Simple checks using $attribDefaults
318 if ( isset( $attribDefaults[$element][$lcattrib] ) &&
319 $attribDefaults[$element][$lcattrib] == $value ) {
320 unset( $attribs[$attrib] );
321 }
322
323 if ( $lcattrib == 'class' && $value == '' ) {
324 unset( $attribs[$attrib] );
325 }
326 }
327
328 # More subtle checks
329 if ( $element === 'link' && isset( $attribs['type'] )
330 && strval( $attribs['type'] ) == 'text/css' ) {
331 unset( $attribs['type'] );
332 }
333 if ( $element === 'select' && isset( $attribs['size'] ) ) {
334 if ( in_array( 'multiple', $attribs )
335 || ( isset( $attribs['multiple'] ) && $attribs['multiple'] !== false )
336 ) {
337 # A multi-select
338 if ( strval( $attribs['size'] ) == '4' ) {
339 unset( $attribs['size'] );
340 }
341 } else {
342 # Single select
343 if ( strval( $attribs['size'] ) == '1' ) {
344 unset( $attribs['size'] );
345 }
346 }
347 }
348
349 return $attribs;
350 }
351
352 /**
353 * Given an associative array of element attributes, generate a string
354 * to stick after the element name in HTML output. Like array( 'href' =>
355 * 'http://www.mediawiki.org/' ) becomes something like
356 * ' href="http://www.mediawiki.org"'. Again, this is like
357 * Xml::expandAttributes(), but it implements some HTML-specific logic.
358 * For instance, it will omit quotation marks if $wgWellFormedXml is false,
359 * and will treat boolean attributes specially.
360 *
361 * Attributes that should contain space-separated lists (such as 'class') array
362 * values are allowed as well, which will automagically be normalized
363 * and converted to a space-separated string. In addition to a numerical
364 * array, the attribute value may also be an associative array. See the
365 * example below for how that works.
366 * @example Numerical array
367 * <code>
368 * Html::element( 'em', array(
369 * 'class' => array( 'foo', 'bar' )
370 * ) );
371 * // gives '<em class="foo bar"></em>'
372 * </code>
373 * @example Associative array
374 * <code>
375 * Html::element( 'em', array(
376 * 'class' => array( 'foo', 'bar', 'foo' => false, 'quux' => true )
377 * ) );
378 * // gives '<em class="bar quux"></em>'
379 * </code>
380 *
381 * @param $attribs array Associative array of attributes, e.g., array(
382 * 'href' => 'http://www.mediawiki.org/' ). Values will be HTML-escaped.
383 * A value of false means to omit the attribute. For boolean attributes,
384 * you can omit the key, e.g., array( 'checked' ) instead of
385 * array( 'checked' => 'checked' ) or such.
386 * @return string HTML fragment that goes between element name and '>'
387 * (starting with a space if at least one attribute is output)
388 */
389 public static function expandAttributes( $attribs ) {
390 global $wgHtml5, $wgWellFormedXml;
391
392 $ret = '';
393 $attribs = (array)$attribs;
394 foreach ( $attribs as $key => $value ) {
395 if ( $value === false || is_null( $value ) ) {
396 continue;
397 }
398
399 # For boolean attributes, support array( 'foo' ) instead of
400 # requiring array( 'foo' => 'meaningless' ).
401 if ( is_int( $key )
402 && in_array( strtolower( $value ), self::$boolAttribs ) ) {
403 $key = $value;
404 }
405
406 # Not technically required in HTML5, but required in XHTML 1.0,
407 # and we'd like consistency and better compression anyway.
408 $key = strtolower( $key );
409
410 # Here we're blacklisting some HTML5-only attributes...
411 if ( !$wgHtml5 && in_array( $key, array(
412 'autocomplete',
413 'autofocus',
414 'max',
415 'min',
416 'multiple',
417 'pattern',
418 'placeholder',
419 'required',
420 'step',
421 'spellcheck',
422 ) ) ) {
423 continue;
424 }
425
426 # Bug 23769: Blacklist all form validation attributes for now. Current
427 # (June 2010) WebKit has no UI, so the form just refuses to submit
428 # without telling the user why, which is much worse than failing
429 # server-side validation. Opera is the only other implementation at
430 # this time, and has ugly UI, so just kill the feature entirely until
431 # we have at least one good implementation.
432 if ( in_array( $key, array( 'max', 'min', 'pattern', 'required', 'step' ) ) ) {
433 continue;
434 }
435
436 // http://www.w3.org/TR/html401/index/attributes.html ("space-separated")
437 // http://www.w3.org/TR/html5/index.html#attributes-1 ("space-separated")
438 $spaceSeparatedListAttributes = array(
439 'class', // html4, html5
440 'accesskey', // as of html5, multiple space-separated values allowed
441 // html4-spec doesn't document rel= as space-separated
442 // but has been used like that and is now documented as such
443 // in the html5-spec.
444 'rel',
445 );
446
447 # Specific features for attributes that allow a list of space-separated values
448 if ( in_array( $key, $spaceSeparatedListAttributes ) ) {
449 // Apply some normalization and remove duplicates
450
451 // Convert into correct array. Array can contain space-seperated
452 // values. Implode/explode to get those into the main array as well.
453 if ( is_array( $value ) ) {
454 // If input wasn't an array, we can skip this step
455
456 $newValue = array();
457 foreach ( $value as $k => $v ) {
458 if ( is_string( $v ) ) {
459 // String values should be normal `array( 'foo' )`
460 // Just append them
461 if ( !isset( $value[$v] ) ) {
462 // As a special case don't set 'foo' if a
463 // separate 'foo' => true/false exists in the array
464 // keys should be authoritive
465 $newValue[] = $v;
466 }
467 } elseif ( $v ) {
468 // If the value is truthy but not a string this is likely
469 // an array( 'foo' => true ), falsy values don't add strings
470 $newValue[] = $k;
471 }
472 }
473 $value = implode( ' ', $newValue );
474 }
475 $value = explode( ' ', $value );
476
477 // Normalize spacing by fixing up cases where people used
478 // more than 1 space and/or a trailing/leading space
479 $value = array_diff( $value, array( '', ' ' ) );
480
481 // Remove duplicates and create the string
482 $value = implode( ' ', array_unique( $value ) );
483 }
484
485 # See the "Attributes" section in the HTML syntax part of HTML5,
486 # 9.1.2.3 as of 2009-08-10. Most attributes can have quotation
487 # marks omitted, but not all. (Although a literal " is not
488 # permitted, we don't check for that, since it will be escaped
489 # anyway.)
490 #
491 # See also research done on further characters that need to be
492 # escaped: http://code.google.com/p/html5lib/issues/detail?id=93
493 $badChars = "\\x00- '=<>`/\x{00a0}\x{1680}\x{180e}\x{180F}\x{2000}\x{2001}"
494 . "\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}"
495 . "\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}";
496 if ( $wgWellFormedXml || $value === ''
497 || preg_match( "![$badChars]!u", $value ) ) {
498 $quote = '"';
499 } else {
500 $quote = '';
501 }
502
503 if ( in_array( $key, self::$boolAttribs ) ) {
504 # In XHTML 1.0 Transitional, the value needs to be equal to the
505 # key. In HTML5, we can leave the value empty instead. If we
506 # don't need well-formed XML, we can omit the = entirely.
507 if ( !$wgWellFormedXml ) {
508 $ret .= " $key";
509 } elseif ( $wgHtml5 ) {
510 $ret .= " $key=\"\"";
511 } else {
512 $ret .= " $key=\"$key\"";
513 }
514 } else {
515 # Apparently we need to entity-encode \n, \r, \t, although the
516 # spec doesn't mention that. Since we're doing strtr() anyway,
517 # and we don't need <> escaped here, we may as well not call
518 # htmlspecialchars().
519 # @todo FIXME: Verify that we actually need to
520 # escape \n\r\t here, and explain why, exactly.
521 #
522 # We could call Sanitizer::encodeAttribute() for this, but we
523 # don't because we're stubborn and like our marginal savings on
524 # byte size from not having to encode unnecessary quotes.
525 $map = array(
526 '&' => '&amp;',
527 '"' => '&quot;',
528 "\n" => '&#10;',
529 "\r" => '&#13;',
530 "\t" => '&#9;'
531 );
532 if ( $wgWellFormedXml ) {
533 # This is allowed per spec: <http://www.w3.org/TR/xml/#NT-AttValue>
534 # But reportedly it breaks some XML tools?
535 # @todo FIXME: Is this really true?
536 $map['<'] = '&lt;';
537 }
538
539 $ret .= " $key=$quote" . strtr( $value, $map ) . $quote;
540 }
541 }
542 return $ret;
543 }
544
545 /**
546 * Output a <script> tag with the given contents. TODO: do some useful
547 * escaping as well, like if $contents contains literal '</script>' or (for
548 * XML) literal "]]>".
549 *
550 * @param $contents string JavaScript
551 * @return string Raw HTML
552 */
553 public static function inlineScript( $contents ) {
554 global $wgHtml5, $wgJsMimeType, $wgWellFormedXml;
555
556 $attrs = array();
557
558 if ( !$wgHtml5 ) {
559 $attrs['type'] = $wgJsMimeType;
560 }
561
562 if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
563 $contents = "/*<![CDATA[*/$contents/*]]>*/";
564 }
565
566 return self::rawElement( 'script', $attrs, $contents );
567 }
568
569 /**
570 * Output a <script> tag linking to the given URL, e.g.,
571 * <script src=foo.js></script>.
572 *
573 * @param $url string
574 * @return string Raw HTML
575 */
576 public static function linkedScript( $url ) {
577 global $wgHtml5, $wgJsMimeType;
578
579 $attrs = array( 'src' => $url );
580
581 if ( !$wgHtml5 ) {
582 $attrs['type'] = $wgJsMimeType;
583 }
584
585 return self::element( 'script', $attrs );
586 }
587
588 /**
589 * Output a <style> tag with the given contents for the given media type
590 * (if any). TODO: do some useful escaping as well, like if $contents
591 * contains literal '</style>' (admittedly unlikely).
592 *
593 * @param $contents string CSS
594 * @param $media mixed A media type string, like 'screen'
595 * @return string Raw HTML
596 */
597 public static function inlineStyle( $contents, $media = 'all' ) {
598 global $wgWellFormedXml;
599
600 if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
601 $contents = "/*<![CDATA[*/$contents/*]]>*/";
602 }
603
604 return self::rawElement( 'style', array(
605 'type' => 'text/css',
606 'media' => $media,
607 ), $contents );
608 }
609
610 /**
611 * Output a <link rel=stylesheet> linking to the given URL for the given
612 * media type (if any).
613 *
614 * @param $url string
615 * @param $media mixed A media type string, like 'screen'
616 * @return string Raw HTML
617 */
618 public static function linkedStyle( $url, $media = 'all' ) {
619 return self::element( 'link', array(
620 'rel' => 'stylesheet',
621 'href' => $url,
622 'type' => 'text/css',
623 'media' => $media,
624 ) );
625 }
626
627 /**
628 * Convenience function to produce an <input> element. This supports the
629 * new HTML5 input types and attributes, and will silently strip them if
630 * $wgHtml5 is false.
631 *
632 * @param $name string name attribute
633 * @param $value mixed value attribute
634 * @param $type string type attribute
635 * @param $attribs array Associative array of miscellaneous extra
636 * attributes, passed to Html::element()
637 * @return string Raw HTML
638 */
639 public static function input( $name, $value = '', $type = 'text', $attribs = array() ) {
640 $attribs['type'] = $type;
641 $attribs['value'] = $value;
642 $attribs['name'] = $name;
643
644 return self::element( 'input', $attribs );
645 }
646
647 /**
648 * Convenience function to produce an input element with type=hidden
649 *
650 * @param $name string name attribute
651 * @param $value string value attribute
652 * @param $attribs array Associative array of miscellaneous extra
653 * attributes, passed to Html::element()
654 * @return string Raw HTML
655 */
656 public static function hidden( $name, $value, $attribs = array() ) {
657 return self::input( $name, $value, 'hidden', $attribs );
658 }
659
660 /**
661 * Convenience function to produce an <input> element. This supports leaving
662 * out the cols= and rows= which Xml requires and are required by HTML4/XHTML
663 * but not required by HTML5 and will silently set cols="" and rows="" if
664 * $wgHtml5 is false and cols and rows are omitted (HTML4 validates present
665 * but empty cols="" and rows="" as valid).
666 *
667 * @param $name string name attribute
668 * @param $value string value attribute
669 * @param $attribs array Associative array of miscellaneous extra
670 * attributes, passed to Html::element()
671 * @return string Raw HTML
672 */
673 public static function textarea( $name, $value = '', $attribs = array() ) {
674 global $wgHtml5;
675
676 $attribs['name'] = $name;
677
678 if ( !$wgHtml5 ) {
679 if ( !isset( $attribs['cols'] ) ) {
680 $attribs['cols'] = "";
681 }
682
683 if ( !isset( $attribs['rows'] ) ) {
684 $attribs['rows'] = "";
685 }
686 }
687
688 return self::element( 'textarea', $attribs, $value );
689 }
690
691 /**
692 * Constructs the opening html-tag with necessary doctypes depending on
693 * global variables.
694 *
695 * @param $attribs array Associative array of miscellaneous extra
696 * attributes, passed to Html::element() of html tag.
697 * @return string Raw HTML
698 */
699 public static function htmlHeader( $attribs = array() ) {
700 $ret = '';
701
702 global $wgMimeType;
703
704 if ( self::isXmlMimeType( $wgMimeType ) ) {
705 $ret .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n";
706 }
707
708 global $wgHtml5, $wgHtml5Version, $wgDocType, $wgDTD;
709 global $wgXhtmlNamespaces, $wgXhtmlDefaultNamespace;
710
711 if ( $wgHtml5 ) {
712 $ret .= "<!DOCTYPE html>\n";
713
714 if ( $wgHtml5Version ) {
715 $attribs['version'] = $wgHtml5Version;
716 }
717 } else {
718 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\" \"$wgDTD\">\n";
719 $attribs['xmlns'] = $wgXhtmlDefaultNamespace;
720
721 foreach ( $wgXhtmlNamespaces as $tag => $ns ) {
722 $attribs["xmlns:$tag"] = $ns;
723 }
724 }
725
726 $html = Html::openElement( 'html', $attribs );
727
728 if ( $html ) {
729 $html .= "\n";
730 }
731
732 $ret .= $html;
733
734 return $ret;
735 }
736
737 /**
738 * Determines if the given mime type is xml.
739 *
740 * @param $mimetype string MimeType
741 * @return Boolean
742 */
743 public static function isXmlMimeType( $mimetype ) {
744 switch ( $mimetype ) {
745 case 'text/xml':
746 case 'application/xhtml+xml':
747 case 'application/xml':
748 return true;
749 default:
750 return false;
751 }
752 }
753
754 /**
755 * Get HTML for an info box with an icon.
756 *
757 * @param $text String: wikitext, get this with wfMsgNoTrans()
758 * @param $icon String: icon name, file in skins/common/images
759 * @param $alt String: alternate text for the icon
760 * @param $class String: additional class name to add to the wrapper div
761 * @param $useStylePath
762 *
763 * @return string
764 */
765 static function infoBox( $text, $icon, $alt, $class = false, $useStylePath = true ) {
766 global $wgStylePath;
767
768 if ( $useStylePath ) {
769 $icon = $wgStylePath.'/common/images/'.$icon;
770 }
771
772 $s = Html::openElement( 'div', array( 'class' => "mw-infobox $class") );
773
774 $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ).
775 Html::element( 'img',
776 array(
777 'src' => $icon,
778 'alt' => $alt,
779 )
780 ).
781 Html::closeElement( 'div' );
782
783 $s .= Html::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ).
784 $text.
785 Html::closeElement( 'div' );
786 $s .= Html::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
787
788 $s .= Html::closeElement( 'div' );
789
790 $s .= Html::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
791
792 return $s;
793 }
794 }