Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / Xml.php
blob8f18046f5fc414864c3b04d3c807f6af36f3298f
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 = [], $contents = '' ) {
92 global $wgContLang;
93 if ( $attribs ) {
94 $attribs = array_map( [ 'UtfNormal\Validator', '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 = [];
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 = [ '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 = [], 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 = [ 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ];
232 $attrs = array_merge( $attrs, $overrideAttrs );
234 if ( $msg === null ) {
235 $msg = wfMessage( 'yourlanguage' );
237 return [
238 Xml::label( $msg->text(), $attrs['id'] ),
239 Xml::tags( 'select', $attrs, $options )
244 * Shortcut to make a span element
245 * @param string $text Content of the element, will be escaped
246 * @param string $class Class name of the span element
247 * @param array $attribs Other attributes
248 * @return string
250 public static function span( $text, $class, $attribs = [] ) {
251 return self::element( 'span', [ 'class' => $class ] + $attribs, $text );
255 * Shortcut to make a specific element with a class attribute
256 * @param string $text Content of the element, will be escaped
257 * @param string $class Class name of the span element
258 * @param string $tag Element name
259 * @param array $attribs Other attributes
260 * @return string
262 public static function wrapClass( $text, $class, $tag = 'span', $attribs = [] ) {
263 return self::tags( $tag, [ 'class' => $class ] + $attribs, $text );
267 * Convenience function to build an HTML text input field
268 * @param string $name Value of the name attribute
269 * @param int $size Value of the size attribute
270 * @param mixed $value Value of the value attribute
271 * @param array $attribs Other attributes
272 * @return string HTML
274 public static function input( $name, $size = false, $value = false, $attribs = [] ) {
275 $attributes = [ 'name' => $name ];
277 if ( $size ) {
278 $attributes['size'] = $size;
281 if ( $value !== false ) { // maybe 0
282 $attributes['value'] = $value;
285 return self::element( 'input',
286 Html::getTextInputAttributes( $attributes + $attribs ) );
290 * Convenience function to build an HTML password input field
291 * @param string $name Value of the name attribute
292 * @param int $size Value of the size attribute
293 * @param mixed $value Value of the value attribute
294 * @param array $attribs Other attributes
295 * @return string HTML
297 public static function password( $name, $size = false, $value = false,
298 $attribs = []
300 return self::input( $name, $size, $value,
301 array_merge( $attribs, [ 'type' => 'password' ] ) );
305 * Internal function for use in checkboxes and radio buttons and such.
307 * @param string $name
308 * @param bool $present
310 * @return array
312 public static function attrib( $name, $present = true ) {
313 return $present ? [ $name => $name ] : [];
317 * Convenience function to build an HTML checkbox
318 * @param string $name Value of the name attribute
319 * @param bool $checked Whether the checkbox is checked or not
320 * @param array $attribs Array other attributes
321 * @return string HTML
323 public static function check( $name, $checked = false, $attribs = [] ) {
324 return self::element( 'input', array_merge(
326 'name' => $name,
327 'type' => 'checkbox',
328 'value' => 1 ],
329 self::attrib( 'checked', $checked ),
330 $attribs ) );
334 * Convenience function to build an HTML radio button
335 * @param string $name Value of the name attribute
336 * @param string $value Value of the value attribute
337 * @param bool $checked Whether the checkbox is checked or not
338 * @param array $attribs Other attributes
339 * @return string HTML
341 public static function radio( $name, $value, $checked = false, $attribs = [] ) {
342 return self::element( 'input', [
343 'name' => $name,
344 'type' => 'radio',
345 'value' => $value ] + self::attrib( 'checked', $checked ) + $attribs );
349 * Convenience function to build an HTML form label
350 * @param string $label Text of the label
351 * @param string $id
352 * @param array $attribs An attribute array. This will usually be
353 * the same array as is passed to the corresponding input element,
354 * so this function will cherry-pick appropriate attributes to
355 * apply to the label as well; only class and title are applied.
356 * @return string HTML
358 public static function label( $label, $id, $attribs = [] ) {
359 $a = [ 'for' => $id ];
361 foreach ( [ 'class', 'title' ] as $attr ) {
362 if ( isset( $attribs[$attr] ) ) {
363 $a[$attr] = $attribs[$attr];
367 return self::element( 'label', $a, $label );
371 * Convenience function to build an HTML text input field with a label
372 * @param string $label Text of the label
373 * @param string $name Value of the name attribute
374 * @param string $id Id of the input
375 * @param int|bool $size Value of the size attribute
376 * @param string|bool $value Value of the value attribute
377 * @param array $attribs Other attributes
378 * @return string HTML
380 public static function inputLabel( $label, $name, $id, $size = false,
381 $value = false, $attribs = []
383 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
384 return $label . '&#160;' . $input;
388 * Same as Xml::inputLabel() but return input and label in an array
390 * @param string $label
391 * @param string $name
392 * @param string $id
393 * @param int|bool $size
394 * @param string|bool $value
395 * @param array $attribs
397 * @return array
399 public static function inputLabelSep( $label, $name, $id, $size = false,
400 $value = false, $attribs = []
402 return [
403 Xml::label( $label, $id, $attribs ),
404 self::input( $name, $size, $value, [ 'id' => $id ] + $attribs )
409 * Convenience function to build an HTML checkbox with a label
411 * @param string $label
412 * @param string $name
413 * @param string $id
414 * @param bool $checked
415 * @param array $attribs
417 * @return string HTML
419 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = [] ) {
420 global $wgUseMediaWikiUIEverywhere;
421 $chkLabel = self::check( $name, $checked, [ 'id' => $id ] + $attribs ) .
422 '&#160;' .
423 self::label( $label, $id, $attribs );
425 if ( $wgUseMediaWikiUIEverywhere ) {
426 $chkLabel = self::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
427 $chkLabel . self::closeElement( 'div' );
429 return $chkLabel;
433 * Convenience function to build an HTML radio button with a label
435 * @param string $label
436 * @param string $name
437 * @param string $value
438 * @param string $id
439 * @param bool $checked
440 * @param array $attribs
442 * @return string HTML
444 public static function radioLabel( $label, $name, $value, $id,
445 $checked = false, $attribs = []
447 return self::radio( $name, $value, $checked, [ 'id' => $id ] + $attribs ) .
448 '&#160;' .
449 self::label( $label, $id, $attribs );
453 * Convenience function to build an HTML submit button
454 * When $wgUseMediaWikiUIEverywhere is true it will default to a progressive button
455 * @param string $value Label text for the button
456 * @param array $attribs Optional custom attributes
457 * @return string HTML
459 public static function submitButton( $value, $attribs = [] ) {
460 global $wgUseMediaWikiUIEverywhere;
461 $baseAttrs = [
462 'type' => 'submit',
463 'value' => $value,
465 // Done conditionally for time being as it is possible
466 // some submit forms
467 // might need to be mw-ui-destructive (e.g. delete a page)
468 if ( $wgUseMediaWikiUIEverywhere ) {
469 $baseAttrs['class'] = 'mw-ui-button mw-ui-progressive';
471 // Any custom attributes will take precendence of anything in baseAttrs e.g. override the class
472 $attribs = $attribs + $baseAttrs;
473 return Html::element( 'input', $attribs );
477 * Convenience function to build an HTML drop-down list item.
478 * @param string $text Text for this item. Will be HTML escaped
479 * @param string $value Form submission value; if empty, use text
480 * @param bool $selected If true, will be the default selected item
481 * @param array $attribs Optional additional HTML attributes
482 * @return string HTML
484 public static function option( $text, $value = null, $selected = false,
485 $attribs = [] ) {
486 if ( !is_null( $value ) ) {
487 $attribs['value'] = $value;
489 if ( $selected ) {
490 $attribs['selected'] = 'selected';
492 return Html::element( 'option', $attribs, $text );
496 * Build a drop-down box from a textual list.
498 * @param string $name Name and id for the drop-down
499 * @param string $list Correctly formatted text (newline delimited) to be
500 * used to generate the options.
501 * @param string $other Text for the "Other reasons" option
502 * @param string $selected Option which should be pre-selected
503 * @param string $class CSS classes for the drop-down
504 * @param int $tabindex Value of the tabindex attribute
505 * @return string
507 public static function listDropDown( $name = '', $list = '', $other = '',
508 $selected = '', $class = '', $tabindex = null
510 $optgroup = false;
512 $options = self::option( $other, 'other', $selected === 'other' );
514 foreach ( explode( "\n", $list ) as $option ) {
515 $value = trim( $option );
516 if ( $value == '' ) {
517 continue;
518 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
519 // A new group is starting ...
520 $value = trim( substr( $value, 1 ) );
521 if ( $optgroup ) {
522 $options .= self::closeElement( 'optgroup' );
524 $options .= self::openElement( 'optgroup', [ 'label' => $value ] );
525 $optgroup = true;
526 } elseif ( substr( $value, 0, 2 ) == '**' ) {
527 // groupmember
528 $value = trim( substr( $value, 2 ) );
529 $options .= self::option( $value, $value, $selected === $value );
530 } else {
531 // groupless reason list
532 if ( $optgroup ) {
533 $options .= self::closeElement( 'optgroup' );
535 $options .= self::option( $value, $value, $selected === $value );
536 $optgroup = false;
540 if ( $optgroup ) {
541 $options .= self::closeElement( 'optgroup' );
544 $attribs = [];
546 if ( $name ) {
547 $attribs['id'] = $name;
548 $attribs['name'] = $name;
551 if ( $class ) {
552 $attribs['class'] = $class;
555 if ( $tabindex ) {
556 $attribs['tabindex'] = $tabindex;
559 return Xml::openElement( 'select', $attribs )
560 . "\n"
561 . $options
562 . "\n"
563 . Xml::closeElement( 'select' );
567 * Converts textual drop-down list to array
569 * @param string $list Correctly formatted text (newline delimited) to be
570 * used to generate the options.
571 * @return array
573 public static function getArrayFromWikiTextList( $list = '' ) {
574 $options = [];
576 foreach ( explode( "\n", $list ) as $option ) {
577 $value = trim( $option );
578 if ( $value == '' ) {
579 continue;
580 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
581 // A new group is starting ...
582 $value = trim( substr( $value, 1 ) );
583 $options[] = $value;
584 } elseif ( substr( $value, 0, 2 ) == '**' ) {
585 // groupmember
586 $value = trim( substr( $value, 2 ) );
587 $options[] = $value;
588 } else {
589 // groupless reason list
590 $options[] = $value;
593 return $options;
597 * Shortcut for creating fieldsets.
599 * @param string|bool $legend Legend of the fieldset. If evaluates to false,
600 * legend is not added.
601 * @param string $content Pre-escaped content for the fieldset. If false,
602 * only open fieldset is returned.
603 * @param array $attribs Any attributes to fieldset-element.
605 * @return string
607 public static function fieldset( $legend = false, $content = false, $attribs = [] ) {
608 $s = Xml::openElement( 'fieldset', $attribs ) . "\n";
610 if ( $legend ) {
611 $s .= Xml::element( 'legend', null, $legend ) . "\n";
614 if ( $content !== false ) {
615 $s .= $content . "\n";
616 $s .= Xml::closeElement( 'fieldset' ) . "\n";
619 return $s;
623 * Shortcut for creating textareas.
625 * @param string $name The 'name' for the textarea
626 * @param string $content Content for the textarea
627 * @param int $cols The number of columns for the textarea
628 * @param int $rows The number of rows for the textarea
629 * @param array $attribs Any other attributes for the textarea
631 * @return string
633 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) {
634 return self::element( 'textarea',
635 Html::getTextInputAttributes(
637 'name' => $name,
638 'id' => $name,
639 'cols' => $cols,
640 'rows' => $rows
641 ] + $attribs
643 $content, false );
647 * Encode a variable of arbitrary type to JavaScript.
648 * If the value is an XmlJsCode object, pass through the object's value verbatim.
650 * @note Only use this function for generating JavaScript code. If generating output
651 * for a proper JSON parser, just call FormatJson::encode() directly.
653 * @param mixed $value The value being encoded. Can be any type except a resource.
654 * @param bool $pretty If true, add non-significant whitespace to improve readability.
655 * @return string|bool String if successful; false upon failure
657 public static function encodeJsVar( $value, $pretty = false ) {
658 if ( $value instanceof XmlJsCode ) {
659 return $value->value;
661 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
665 * Create a call to a JavaScript function. The supplied arguments will be
666 * encoded using Xml::encodeJsVar().
668 * @since 1.17
669 * @param string $name The name of the function to call, or a JavaScript expression
670 * which evaluates to a function object which is called.
671 * @param array $args The arguments to pass to the function.
672 * @param bool $pretty If true, add non-significant whitespace to improve readability.
673 * @return string|bool String if successful; false upon failure
675 public static function encodeJsCall( $name, $args, $pretty = false ) {
676 foreach ( $args as &$arg ) {
677 $arg = Xml::encodeJsVar( $arg, $pretty );
678 if ( $arg === false ) {
679 return false;
683 return "$name(" . ( $pretty
684 ? ( ' ' . implode( ', ', $args ) . ' ' )
685 : implode( ',', $args )
686 ) . ");";
690 * Check if a string is well-formed XML.
691 * Must include the surrounding tag.
692 * This function is a DoS vector if an attacker can define
693 * entities in $text.
695 * @param string $text String to test.
696 * @return bool
698 * @todo Error position reporting return
700 private static function isWellFormed( $text ) {
701 $parser = xml_parser_create( "UTF-8" );
703 # case folding violates XML standard, turn it off
704 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
706 if ( !xml_parse( $parser, $text, true ) ) {
707 // $err = xml_error_string( xml_get_error_code( $parser ) );
708 // $position = xml_get_current_byte_index( $parser );
709 // $fragment = $this->extractFragment( $html, $position );
710 // $this->mXmlError = "$err at byte $position:\n$fragment";
711 xml_parser_free( $parser );
712 return false;
715 xml_parser_free( $parser );
717 return true;
721 * Check if a string is a well-formed XML fragment.
722 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
723 * and can use HTML named entities.
725 * @param string $text
726 * @return bool
728 public static function isWellFormedXmlFragment( $text ) {
729 $html =
730 Sanitizer::hackDocType() .
731 '<html>' .
732 $text .
733 '</html>';
735 return Xml::isWellFormed( $html );
739 * Replace " > and < with their respective HTML entities ( &quot;,
740 * &gt;, &lt;)
742 * @param string $in Text that might contain HTML tags.
743 * @return string Escaped string
745 public static function escapeTagsOnly( $in ) {
746 return str_replace(
747 [ '"', '>', '<' ],
748 [ '&quot;', '&gt;', '&lt;' ],
749 $in );
753 * Generate a form (without the opening form element).
754 * Output optionally includes a submit button.
755 * @param array $fields Associative array, key is the name of a message that
756 * contains a description for the field, value is an HTML string
757 * containing the appropriate input.
758 * @param string $submitLabel The name of a message containing a label for
759 * the submit button.
760 * @param array $submitAttribs The attributes to add to the submit button
761 * @return string HTML form.
763 public static function buildForm( $fields, $submitLabel = null, $submitAttribs = [] ) {
764 $form = '';
765 $form .= "<table><tbody>";
767 foreach ( $fields as $labelmsg => $input ) {
768 $id = "mw-$labelmsg";
769 $form .= Xml::openElement( 'tr', [ 'id' => $id ] );
771 // TODO use a <label> here for accessibility purposes - will need
772 // to either not use a table to build the form, or find the ID of
773 // the input somehow.
775 $form .= Xml::tags( 'td', [ 'class' => 'mw-label' ], wfMessage( $labelmsg )->parse() );
776 $form .= Xml::openElement( 'td', [ 'class' => 'mw-input' ] )
777 . $input . Xml::closeElement( 'td' );
778 $form .= Xml::closeElement( 'tr' );
781 if ( $submitLabel ) {
782 $form .= Xml::openElement( 'tr' );
783 $form .= Xml::tags( 'td', [], '' );
784 $form .= Xml::openElement( 'td', [ 'class' => 'mw-submit' ] )
785 . Xml::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
786 . Xml::closeElement( 'td' );
787 $form .= Xml::closeElement( 'tr' );
790 $form .= "</tbody></table>";
792 return $form;
796 * Build a table of data
797 * @param array $rows An array of arrays of strings, each to be a row in a table
798 * @param array $attribs An array of attributes to apply to the table tag [optional]
799 * @param array $headers An array of strings to use as table headers [optional]
800 * @return string
802 public static function buildTable( $rows, $attribs = [], $headers = null ) {
803 $s = Xml::openElement( 'table', $attribs );
805 if ( is_array( $headers ) ) {
806 $s .= Xml::openElement( 'thead', $attribs );
808 foreach ( $headers as $id => $header ) {
809 $attribs = [];
811 if ( is_string( $id ) ) {
812 $attribs['id'] = $id;
815 $s .= Xml::element( 'th', $attribs, $header );
817 $s .= Xml::closeElement( 'thead' );
820 foreach ( $rows as $id => $row ) {
821 $attribs = [];
823 if ( is_string( $id ) ) {
824 $attribs['id'] = $id;
827 $s .= Xml::buildTableRow( $attribs, $row );
830 $s .= Xml::closeElement( 'table' );
832 return $s;
836 * Build a row for a table
837 * @param array $attribs An array of attributes to apply to the tr tag
838 * @param array $cells An array of strings to put in <td>
839 * @return string
841 public static function buildTableRow( $attribs, $cells ) {
842 $s = Xml::openElement( 'tr', $attribs );
844 foreach ( $cells as $id => $cell ) {
845 $attribs = [];
847 if ( is_string( $id ) ) {
848 $attribs['id'] = $id;
851 $s .= Xml::element( 'td', $attribs, $cell );
854 $s .= Xml::closeElement( 'tr' );
856 return $s;
861 * A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to
862 * interpret a given string as being a JavaScript expression, instead of string
863 * data.
865 * Example:
867 * Xml::encodeJsVar( new XmlJsCode( 'a + b' ) );
869 * Returns "a + b".
871 * @note As of 1.21, XmlJsCode objects cannot be nested inside objects or arrays. The sole
872 * exception is the $args argument to Xml::encodeJsCall() because Xml::encodeJsVar() is
873 * called for each individual element in that array.
875 * @since 1.17
877 class XmlJsCode {
878 public $value;
880 function __construct( $value ) {
881 $this->value = $value;