Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / Xml.php
blob11f14dbb698da6c464e6a9a242b6479b5ef33aba
1 <?php
2 /**
3 * Methods to generate XML.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Module of static functions for generating XML
26 class Xml {
27 /**
28 * Format an XML element with given attributes and, optionally, text content.
29 * Element and attribute names are assumed to be ready for literal inclusion.
30 * Strings are assumed to not contain XML-illegal characters; special
31 * characters (<, >, &) are escaped but illegals are not touched.
33 * @param string $element Element name
34 * @param array $attribs Name=>value pairs. Values will be escaped.
35 * @param string $contents Null to make an open tag only; '' for a contentless closed tag (default)
36 * @param bool $allowShortTag Whether '' in $contents will result in a contentless closed tag
37 * @return string
39 public static function element( $element, $attribs = null, $contents = '',
40 $allowShortTag = true
41 ) {
42 $out = '<' . $element;
43 if ( !is_null( $attribs ) ) {
44 $out .= self::expandAttributes( $attribs );
46 if ( is_null( $contents ) ) {
47 $out .= '>';
48 } else {
49 if ( $allowShortTag && $contents === '' ) {
50 $out .= ' />';
51 } else {
52 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
55 return $out;
58 /**
59 * Given an array of ('attributename' => 'value'), it generates the code
60 * to set the XML attributes : attributename="value".
61 * The values are passed to Sanitizer::encodeAttribute.
62 * Return null if no attributes given.
63 * @param array $attribs Array of attributes for an XML element
64 * @throws MWException
65 * @return null|string
67 public static function expandAttributes( $attribs ) {
68 $out = '';
69 if ( is_null( $attribs ) ) {
70 return null;
71 } elseif ( is_array( $attribs ) ) {
72 foreach ( $attribs as $name => $val ) {
73 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
75 return $out;
76 } else {
77 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
81 /**
82 * Format an XML element as with self::element(), but run text through the
83 * $wgContLang->normalize() validator first to ensure that no invalid UTF-8
84 * is passed.
86 * @param string $element
87 * @param array $attribs Name=>value pairs. Values will be escaped.
88 * @param string $contents Null to make an open tag only; '' for a contentless closed tag (default)
89 * @return string
91 public static function elementClean( $element, $attribs = array(), $contents = '' ) {
92 global $wgContLang;
93 if ( $attribs ) {
94 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
96 if ( $contents ) {
97 $contents = $wgContLang->normalize( $contents );
99 return self::element( $element, $attribs, $contents );
103 * This opens an XML element
105 * @param string $element Name of the element
106 * @param array $attribs Array of attributes, see Xml::expandAttributes()
107 * @return string
109 public static function openElement( $element, $attribs = null ) {
110 return '<' . $element . self::expandAttributes( $attribs ) . '>';
114 * Shortcut to close an XML element
115 * @param string $element Element name
116 * @return string
118 public static function closeElement( $element ) {
119 return "</$element>";
123 * Same as Xml::element(), but does not escape contents. Handy when the
124 * content you have is already valid xml.
126 * @param string $element Element name
127 * @param array $attribs Array of attributes
128 * @param string $contents Content of the element
129 * @return string
131 public static function tags( $element, $attribs = null, $contents ) {
132 return self::openElement( $element, $attribs ) . $contents . "</$element>";
136 * Create a date selector
138 * @param string $selected The month which should be selected, default ''.
139 * @param string $allmonths Value of a special item denoting all month.
140 * Null to not include (default).
141 * @param string $id Element identifier
142 * @return string Html string containing the month selector
144 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
145 global $wgLang;
146 $options = array();
147 $data = new XmlSelect( 'month', $id, $selected );
148 if ( is_null( $selected ) ) {
149 $selected = '';
151 if ( !is_null( $allmonths ) ) {
152 $options[wfMessage( 'monthsall' )->text()] = $allmonths;
154 for ( $i = 1; $i < 13; $i++ ) {
155 $options[$wgLang->getMonthName( $i )] = $i;
157 $data->addOptions( $options );
158 $data->setAttribute( 'class', 'mw-month-selector' );
159 return $data->getHTML();
163 * @param int $year
164 * @param int $month
165 * @return string Formatted HTML
167 public static function dateMenu( $year, $month ) {
168 # Offset overrides year/month selection
169 if ( $month && $month !== -1 ) {
170 $encMonth = intval( $month );
171 } else {
172 $encMonth = '';
174 if ( $year ) {
175 $encYear = intval( $year );
176 } elseif ( $encMonth ) {
177 $timestamp = MWTimestamp::getInstance();
178 $thisMonth = intval( $timestamp->format( 'n' ) );
179 $thisYear = intval( $timestamp->format( 'Y' ) );
180 if ( intval( $encMonth ) > $thisMonth ) {
181 $thisYear--;
183 $encYear = $thisYear;
184 } else {
185 $encYear = '';
187 $inputAttribs = array( 'id' => 'year', 'maxlength' => 4, 'size' => 7 );
188 return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
189 Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
190 self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
191 self::monthSelector( $encMonth, -1 );
195 * Construct a language selector appropriate for use in a form or preferences
197 * @param string $selected The language code of the selected language
198 * @param bool $customisedOnly If true only languages which have some content are listed
199 * @param string $inLanguage The ISO code of the language to display the select list in (optional)
200 * @param array $overrideAttrs Override the attributes of the select tag (since 1.20)
201 * @param Message|null $msg Label message key (since 1.20)
202 * @return array Array containing 2 items: label HTML and select list HTML
204 public static function languageSelector( $selected, $customisedOnly = true,
205 $inLanguage = null, $overrideAttrs = array(), Message $msg = null
207 global $wgLanguageCode;
209 $include = $customisedOnly ? 'mwfile' : 'mw';
210 $languages = Language::fetchLanguageNames( $inLanguage, $include );
212 // Make sure the site language is in the list;
213 // a custom language code might not have a defined name...
214 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
215 $languages[$wgLanguageCode] = $wgLanguageCode;
218 ksort( $languages );
221 * If a bogus value is set, default to the content language.
222 * Otherwise, no default is selected and the user ends up
223 * with Afrikaans since it's first in the list.
225 $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode;
226 $options = "\n";
227 foreach ( $languages as $code => $name ) {
228 $options .= Xml::option( "$code - $name", $code, $code == $selected ) . "\n";
231 $attrs = array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' );
232 $attrs = array_merge( $attrs, $overrideAttrs );
234 if ( $msg === null ) {
235 $msg = wfMessage( 'yourlanguage' );
237 return array(
238 Xml::label( $msg->text(), $attrs['id'] ),
239 Xml::tags( 'select', $attrs, $options )
245 * Shortcut to make a span element
246 * @param string $text Content of the element, will be escaped
247 * @param string $class Class name of the span element
248 * @param array $attribs Other attributes
249 * @return string
251 public static function span( $text, $class, $attribs = array() ) {
252 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
256 * Shortcut to make a specific element with a class attribute
257 * @param string $text Content of the element, will be escaped
258 * @param string $class Class name of the span element
259 * @param string $tag Element name
260 * @param array $attribs Other attributes
261 * @return string
263 public static function wrapClass( $text, $class, $tag = 'span', $attribs = array() ) {
264 return self::tags( $tag, array( 'class' => $class ) + $attribs, $text );
268 * Convenience function to build an HTML text input field
269 * @param string $name Value of the name attribute
270 * @param int $size Value of the size attribute
271 * @param mixed $value Value of the value attribute
272 * @param array $attribs Other attributes
273 * @return string HTML
275 public static function input( $name, $size = false, $value = false, $attribs = array() ) {
276 $attributes = array( 'name' => $name );
278 if ( $size ) {
279 $attributes['size'] = $size;
282 if ( $value !== false ) { // maybe 0
283 $attributes['value'] = $value;
286 return self::element( 'input',
287 Html::getTextInputAttributes( $attributes + $attribs ) );
291 * Convenience function to build an HTML password input field
292 * @param string $name Value of the name attribute
293 * @param int $size Value of the size attribute
294 * @param mixed $value Value of the value attribute
295 * @param array $attribs Other attributes
296 * @return string HTML
298 public static function password( $name, $size = false, $value = false,
299 $attribs = array()
301 return self::input( $name, $size, $value,
302 array_merge( $attribs, array( 'type' => 'password' ) ) );
306 * Internal function for use in checkboxes and radio buttons and such.
308 * @param string $name
309 * @param bool $present
311 * @return array
313 public static function attrib( $name, $present = true ) {
314 return $present ? array( $name => $name ) : array();
318 * Convenience function to build an HTML checkbox
319 * @param string $name Value of the name attribute
320 * @param bool $checked Whether the checkbox is checked or not
321 * @param array $attribs Array other attributes
322 * @return string HTML
324 public static function check( $name, $checked = false, $attribs = array() ) {
325 return self::element( 'input', array_merge(
326 array(
327 'name' => $name,
328 'type' => 'checkbox',
329 'value' => 1 ),
330 self::attrib( 'checked', $checked ),
331 $attribs ) );
335 * Convenience function to build an HTML radio button
336 * @param string $name Value of the name attribute
337 * @param string $value Value of the value attribute
338 * @param bool $checked Whether the checkbox is checked or not
339 * @param array $attribs Other attributes
340 * @return string HTML
342 public static function radio( $name, $value, $checked = false, $attribs = array() ) {
343 return self::element( 'input', array(
344 'name' => $name,
345 'type' => 'radio',
346 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
350 * Convenience function to build an HTML form label
351 * @param string $label Text of the label
352 * @param string $id
353 * @param array $attribs An attribute array. This will usually be
354 * the same array as is passed to the corresponding input element,
355 * so this function will cherry-pick appropriate attributes to
356 * apply to the label as well; only class and title are applied.
357 * @return string HTML
359 public static function label( $label, $id, $attribs = array() ) {
360 $a = array( 'for' => $id );
362 foreach ( array( 'class', 'title' ) as $attr ) {
363 if ( isset( $attribs[$attr] ) ) {
364 $a[$attr] = $attribs[$attr];
368 return self::element( 'label', $a, $label );
372 * Convenience function to build an HTML text input field with a label
373 * @param string $label Text of the label
374 * @param string $name Value of the name attribute
375 * @param string $id Id of the input
376 * @param int|bool $size Value of the size attribute
377 * @param string|bool $value Value of the value attribute
378 * @param array $attribs Other attributes
379 * @return string HTML
381 public static function inputLabel( $label, $name, $id, $size = false,
382 $value = false, $attribs = array()
384 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
385 return $label . '&#160;' . $input;
389 * Same as Xml::inputLabel() but return input and label in an array
391 * @param string $label
392 * @param string $name
393 * @param string $id
394 * @param int|bool $size
395 * @param string|bool $value
396 * @param array $attribs
398 * @return array
400 public static function inputLabelSep( $label, $name, $id, $size = false,
401 $value = false, $attribs = array()
403 return array(
404 Xml::label( $label, $id, $attribs ),
405 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs )
410 * Convenience function to build an HTML checkbox with a label
412 * @param string $label
413 * @param string $name
414 * @param string $id
415 * @param bool $checked
416 * @param array $attribs
418 * @return string HTML
420 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = array() ) {
421 global $wgUseMediaWikiUIEverywhere;
422 $chkLabel = self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
423 '&#160;' .
424 self::label( $label, $id, $attribs );
426 if ( $wgUseMediaWikiUIEverywhere ) {
427 $chkLabel = self::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) .
428 $chkLabel . self::closeElement( 'div' );
430 return $chkLabel;
434 * Convenience function to build an HTML radio button with a label
436 * @param string $label
437 * @param string $name
438 * @param string $value
439 * @param string $id
440 * @param bool $checked
441 * @param array $attribs
443 * @return string HTML
445 public static function radioLabel( $label, $name, $value, $id,
446 $checked = false, $attribs = array()
448 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
449 '&#160;' .
450 self::label( $label, $id, $attribs );
454 * Convenience function to build an HTML submit button
455 * When $wgUseMediaWikiUIEverywhere is true it will default to a constructive button
456 * @param string $value Label text for the button
457 * @param array $attribs Optional custom attributes
458 * @return string HTML
460 public static function submitButton( $value, $attribs = array() ) {
461 global $wgUseMediaWikiUIEverywhere;
462 $baseAttrs = array(
463 'type' => 'submit',
464 'value' => $value,
466 // Done conditionally for time being as it is possible
467 // some submit forms
468 // might need to be mw-ui-destructive (e.g. delete a page)
469 if ( $wgUseMediaWikiUIEverywhere ) {
470 $baseAttrs['class'] = 'mw-ui-button mw-ui-constructive';
472 // Any custom attributes will take precendence of anything in baseAttrs e.g. override the class
473 $attribs = $attribs + $baseAttrs;
474 return Html::element( 'input', $attribs );
478 * Convenience function to build an HTML drop-down list item.
479 * @param string $text Text for this item. Will be HTML escaped
480 * @param string $value Form submission value; if empty, use text
481 * @param bool $selected If true, will be the default selected item
482 * @param array $attribs Optional additional HTML attributes
483 * @return string HTML
485 public static function option( $text, $value = null, $selected = false,
486 $attribs = array() ) {
487 if ( !is_null( $value ) ) {
488 $attribs['value'] = $value;
490 if ( $selected ) {
491 $attribs['selected'] = 'selected';
493 return Html::element( 'option', $attribs, $text );
497 * Build a drop-down box from a textual list.
499 * @param string $name Name and id for the drop-down
500 * @param string $list Correctly formatted text (newline delimited) to be
501 * used to generate the options.
502 * @param string $other Text for the "Other reasons" option
503 * @param string $selected Option which should be pre-selected
504 * @param string $class CSS classes for the drop-down
505 * @param int $tabindex Value of the tabindex attribute
506 * @return string
508 public static function listDropDown( $name = '', $list = '', $other = '',
509 $selected = '', $class = '', $tabindex = null
511 $optgroup = false;
513 $options = self::option( $other, 'other', $selected === 'other' );
515 foreach ( explode( "\n", $list ) as $option ) {
516 $value = trim( $option );
517 if ( $value == '' ) {
518 continue;
519 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
520 // A new group is starting ...
521 $value = trim( substr( $value, 1 ) );
522 if ( $optgroup ) {
523 $options .= self::closeElement( 'optgroup' );
525 $options .= self::openElement( 'optgroup', array( 'label' => $value ) );
526 $optgroup = true;
527 } elseif ( substr( $value, 0, 2 ) == '**' ) {
528 // groupmember
529 $value = trim( substr( $value, 2 ) );
530 $options .= self::option( $value, $value, $selected === $value );
531 } else {
532 // groupless reason list
533 if ( $optgroup ) {
534 $options .= self::closeElement( 'optgroup' );
536 $options .= self::option( $value, $value, $selected === $value );
537 $optgroup = false;
541 if ( $optgroup ) {
542 $options .= self::closeElement( 'optgroup' );
545 $attribs = array();
547 if ( $name ) {
548 $attribs['id'] = $name;
549 $attribs['name'] = $name;
552 if ( $class ) {
553 $attribs['class'] = $class;
556 if ( $tabindex ) {
557 $attribs['tabindex'] = $tabindex;
560 return Xml::openElement( 'select', $attribs )
561 . "\n"
562 . $options
563 . "\n"
564 . Xml::closeElement( 'select' );
568 * Shortcut for creating fieldsets.
570 * @param string|bool $legend Legend of the fieldset. If evaluates to false,
571 * legend is not added.
572 * @param string $content Pre-escaped content for the fieldset. If false,
573 * only open fieldset is returned.
574 * @param array $attribs Any attributes to fieldset-element.
576 * @return string
578 public static function fieldset( $legend = false, $content = false, $attribs = array() ) {
579 $s = Xml::openElement( 'fieldset', $attribs ) . "\n";
581 if ( $legend ) {
582 $s .= Xml::element( 'legend', null, $legend ) . "\n";
585 if ( $content !== false ) {
586 $s .= $content . "\n";
587 $s .= Xml::closeElement( 'fieldset' ) . "\n";
590 return $s;
594 * Shortcut for creating textareas.
596 * @param string $name The 'name' for the textarea
597 * @param string $content Content for the textarea
598 * @param int $cols The number of columns for the textarea
599 * @param int $rows The number of rows for the textarea
600 * @param array $attribs Any other attributes for the textarea
602 * @return string
604 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = array() ) {
605 return self::element( 'textarea',
606 Html::getTextInputAttributes(
607 array(
608 'name' => $name,
609 'id' => $name,
610 'cols' => $cols,
611 'rows' => $rows
612 ) + $attribs
614 $content, false );
618 * Returns an escaped string suitable for inclusion in a string literal
619 * for JavaScript source code.
620 * Illegal control characters are assumed not to be present.
622 * @deprecated since 1.21; use Xml::encodeJsVar() or Xml::encodeJsCall() instead
623 * @param string $string String to escape
624 * @return string
626 public static function escapeJsString( $string ) {
627 // See ECMA 262 section 7.8.4 for string literal format
628 $pairs = array(
629 "\\" => "\\\\",
630 "\"" => "\\\"",
631 '\'' => '\\\'',
632 "\n" => "\\n",
633 "\r" => "\\r",
635 # To avoid closing the element or CDATA section
636 "<" => "\\x3c",
637 ">" => "\\x3e",
639 # To avoid any complaints about bad entity refs
640 "&" => "\\x26",
642 # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
643 # Encode certain Unicode formatting chars so affected
644 # versions of Gecko don't misinterpret our strings;
645 # this is a common problem with Farsi text.
646 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
647 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
650 return strtr( $string, $pairs );
654 * Encode a variable of arbitrary type to JavaScript.
655 * If the value is an XmlJsCode object, pass through the object's value verbatim.
657 * @note Only use this function for generating JavaScript code. If generating output
658 * for a proper JSON parser, just call FormatJson::encode() directly.
660 * @param mixed $value The value being encoded. Can be any type except a resource.
661 * @param bool $pretty If true, add non-significant whitespace to improve readability.
662 * @return string|bool String if successful; false upon failure
664 public static function encodeJsVar( $value, $pretty = false ) {
665 if ( $value instanceof XmlJsCode ) {
666 return $value->value;
668 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
672 * Create a call to a JavaScript function. The supplied arguments will be
673 * encoded using Xml::encodeJsVar().
675 * @since 1.17
676 * @param string $name The name of the function to call, or a JavaScript expression
677 * which evaluates to a function object which is called.
678 * @param array $args The arguments to pass to the function.
679 * @param bool $pretty If true, add non-significant whitespace to improve readability.
680 * @return string|bool String if successful; false upon failure
682 public static function encodeJsCall( $name, $args, $pretty = false ) {
683 foreach ( $args as &$arg ) {
684 $arg = Xml::encodeJsVar( $arg, $pretty );
685 if ( $arg === false ) {
686 return false;
690 return "$name(" . ( $pretty
691 ? ( ' ' . implode( ', ', $args ) . ' ' )
692 : implode( ',', $args )
693 ) . ");";
697 * Check if a string is well-formed XML.
698 * Must include the surrounding tag.
699 * This function is a DoS vector if an attacker can define
700 * entities in $text.
702 * @param string $text String to test.
703 * @return bool
705 * @todo Error position reporting return
707 private static function isWellFormed( $text ) {
708 $parser = xml_parser_create( "UTF-8" );
710 # case folding violates XML standard, turn it off
711 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
713 if ( !xml_parse( $parser, $text, true ) ) {
714 // $err = xml_error_string( xml_get_error_code( $parser ) );
715 // $position = xml_get_current_byte_index( $parser );
716 // $fragment = $this->extractFragment( $html, $position );
717 // $this->mXmlError = "$err at byte $position:\n$fragment";
718 xml_parser_free( $parser );
719 return false;
722 xml_parser_free( $parser );
724 return true;
728 * Check if a string is a well-formed XML fragment.
729 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
730 * and can use HTML named entities.
732 * @param string $text
733 * @return bool
735 public static function isWellFormedXmlFragment( $text ) {
736 $html =
737 Sanitizer::hackDocType() .
738 '<html>' .
739 $text .
740 '</html>';
742 return Xml::isWellFormed( $html );
746 * Replace " > and < with their respective HTML entities ( &quot;,
747 * &gt;, &lt;)
749 * @param string $in Text that might contain HTML tags.
750 * @return string Escaped string
752 public static function escapeTagsOnly( $in ) {
753 return str_replace(
754 array( '"', '>', '<' ),
755 array( '&quot;', '&gt;', '&lt;' ),
756 $in );
760 * Generate a form (without the opening form element).
761 * Output optionally includes a submit button.
762 * @param array $fields Associative array, key is the name of a message that
763 * contains a description for the field, value is an HTML string
764 * containing the appropriate input.
765 * @param string $submitLabel The name of a message containing a label for
766 * the submit button.
767 * @param array $submitAttribs The attributes to add to the submit button
768 * @return string HTML form.
770 public static function buildForm( $fields, $submitLabel = null, $submitAttribs = array() ) {
771 $form = '';
772 $form .= "<table><tbody>";
774 foreach ( $fields as $labelmsg => $input ) {
775 $id = "mw-$labelmsg";
776 $form .= Xml::openElement( 'tr', array( 'id' => $id ) );
778 // TODO use a <label> here for accessibility purposes - will need
779 // to either not use a table to build the form, or find the ID of
780 // the input somehow.
782 $form .= Xml::tags( 'td', array( 'class' => 'mw-label' ), wfMessage( $labelmsg )->parse() );
783 $form .= Xml::openElement( 'td', array( 'class' => 'mw-input' ) )
784 . $input . Xml::closeElement( 'td' );
785 $form .= Xml::closeElement( 'tr' );
788 if ( $submitLabel ) {
789 $form .= Xml::openElement( 'tr' );
790 $form .= Xml::tags( 'td', array(), '' );
791 $form .= Xml::openElement( 'td', array( 'class' => 'mw-submit' ) )
792 . Xml::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
793 . Xml::closeElement( 'td' );
794 $form .= Xml::closeElement( 'tr' );
797 $form .= "</tbody></table>";
799 return $form;
803 * Build a table of data
804 * @param array $rows An array of arrays of strings, each to be a row in a table
805 * @param array $attribs An array of attributes to apply to the table tag [optional]
806 * @param array $headers An array of strings to use as table headers [optional]
807 * @return string
809 public static function buildTable( $rows, $attribs = array(), $headers = null ) {
810 $s = Xml::openElement( 'table', $attribs );
812 if ( is_array( $headers ) ) {
813 $s .= Xml::openElement( 'thead', $attribs );
815 foreach ( $headers as $id => $header ) {
816 $attribs = array();
818 if ( is_string( $id ) ) {
819 $attribs['id'] = $id;
822 $s .= Xml::element( 'th', $attribs, $header );
824 $s .= Xml::closeElement( 'thead' );
827 foreach ( $rows as $id => $row ) {
828 $attribs = array();
830 if ( is_string( $id ) ) {
831 $attribs['id'] = $id;
834 $s .= Xml::buildTableRow( $attribs, $row );
837 $s .= Xml::closeElement( 'table' );
839 return $s;
843 * Build a row for a table
844 * @param array $attribs An array of attributes to apply to the tr tag
845 * @param array $cells An array of strings to put in <td>
846 * @return string
848 public static function buildTableRow( $attribs, $cells ) {
849 $s = Xml::openElement( 'tr', $attribs );
851 foreach ( $cells as $id => $cell ) {
852 $attribs = array();
854 if ( is_string( $id ) ) {
855 $attribs['id'] = $id;
858 $s .= Xml::element( 'td', $attribs, $cell );
861 $s .= Xml::closeElement( 'tr' );
863 return $s;
868 * A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to
869 * interpret a given string as being a JavaScript expression, instead of string
870 * data.
872 * Example:
874 * Xml::encodeJsVar( new XmlJsCode( 'a + b' ) );
876 * Returns "a + b".
878 * @note As of 1.21, XmlJsCode objects cannot be nested inside objects or arrays. The sole
879 * exception is the $args argument to Xml::encodeJsCall() because Xml::encodeJsVar() is
880 * called for each individual element in that array.
882 * @since 1.17
884 class XmlJsCode {
885 public $value;
887 function __construct( $value ) {
888 $this->value = $value;