Make mediawiki.special.pageLanguage work again
[lhc/web/wiklou.git] / includes / XmlSelect.php
index e765eed..9d37b95 100644 (file)
  */
 
 /**
- * Class for generating HTML <select> elements.
+ * Class for generating HTML <select> or <datalist> elements.
  */
 class XmlSelect {
        protected $options = array();
        protected $default = false;
+       protected $tagName = 'select';
        protected $attributes = array();
 
        public function __construct( $name = false, $id = false, $default = false ) {
@@ -43,12 +44,19 @@ class XmlSelect {
        }
 
        /**
-        * @param string $default
+        * @param string|array $default
         */
        public function setDefault( $default ) {
                $this->default = $default;
        }
 
+       /**
+        * @param string|array $tagName
+        */
+       public function setTagName( $tagName ) {
+               $this->tagName = $tagName;
+       }
+
        /**
         * @param string $name
         * @param string $value
@@ -95,7 +103,7 @@ class XmlSelect {
         * label => ( label => value, label => value )
         *
         * @param array $options
-        * @param string $default
+        * @param string|array $default
         * @return string
         */
        static function formatOptions( $options, $default = false ) {
@@ -106,7 +114,11 @@ class XmlSelect {
                                $contents = self::formatOptions( $value, $default );
                                $data .= Html::rawElement( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
                        } else {
-                               $data .= Xml::option( $label, $value, $value === $default ) . "\n";
+                               // If $default is an array, then the <select> probably has the multiple attribute,
+                               // so we should check if each $value is in $default, rather than checking if
+                               // $value is equal to $default.
+                               $selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
+                               $data .= Xml::option( $label, $value, $selected ) . "\n";
                        }
                }
 
@@ -123,6 +135,6 @@ class XmlSelect {
                        $contents .= self::formatOptions( $options, $this->default );
                }
 
-               return Html::rawElement( 'select', $this->attributes, rtrim( $contents ) );
+               return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) );
        }
 }