New infrastructure for actions, as discussed on wikitech-l. Fairly huge commit.
[lhc/web/wiklou.git] / includes / Xml.php
index 77b43d9..77b6263 100644 (file)
@@ -347,7 +347,10 @@ class Xml {
         * Convenience function to build an HTML form label
         * @param $label String text of the label
         * @param $id
-        * @param $attribs Array, other attributes
+        * @param $attribs Array an attribute array.  This will usuall be 
+        *     the same array as is passed to the corresponding input element,
+        *     so this function will cherry-pick appropriate attributes to 
+        *     apply to the label as well; currently only class is applied.
         * @return string HTML
         */
        public static function label( $label, $id, $attribs=array() ) {
@@ -443,14 +446,14 @@ class Xml {
         * Build a drop-down box from a textual list.
         *
         * @param $name Mixed: Name and id for the drop-down
-        * @param $class Mixed: CSS classes for the drop-down
+        * @param $list Mixed: Correctly formatted text (newline delimited) to be used to generate the options
         * @param $other Mixed: Text for the "Other reasons" option
-        * @param $list Mixed: Correctly formatted text to be used to generate the options
         * @param $selected Mixed: Option which should be pre-selected
+        * @param $class Mixed: CSS classes for the drop-down
         * @param $tabindex Mixed: Value of the tabindex attribute
         * @return string
         */
-       public static function listDropDown( $name= '', $list = '', $other = '', $selected = '', $class = '', $tabindex = Null ) {
+       public static function listDropDown( $name= '', $list = '', $other = '', $selected = '', $class = '', $tabindex = null ) {
                $optgroup = false;
 
                $options = self::option( $other, 'other', $selected === 'other' );
@@ -580,8 +583,8 @@ class Xml {
                        $s = $value ? 'true' : 'false';
                } elseif ( is_null( $value ) ) {
                        $s = 'null';
-               } elseif ( is_int( $value ) ) {
-                       $s = $value;
+               } elseif ( is_int( $value ) || is_float( $value ) ) {
+                       $s = strval($value);
                } elseif ( is_array( $value ) && // Make sure it's not associative.
                                        array_keys($value) === range( 0, count($value) - 1 ) ||
                                        count($value) == 0
@@ -594,6 +597,8 @@ class Xml {
                                $s .= self::encodeJsVar( $elt );
                        }
                        $s .= ']';
+               } elseif ( $value instanceof XmlJsCode ) {
+                       $s = $value->value;
                } elseif ( is_object( $value ) || is_array( $value ) ) {
                        // Objects and associative arrays
                        $s = '{';
@@ -611,6 +616,30 @@ class Xml {
                return $s;
        }
 
+       /**
+        * Create a call to a JavaScript function. The supplied arguments will be 
+        * encoded using Xml::encodeJsVar(). 
+        *
+        * @param $name String The name of the function to call, or a JavaScript expression
+        *    which evaluates to a function object which is called.
+        * @param $args Array of arguments to pass to the function.
+        * @since 1.17
+        */
+       public static function encodeJsCall( $name, $args ) {
+               $s = "$name(";
+               $first = true;
+               foreach ( $args as $arg ) {
+                       if ( $first ) {
+                               $first = false;
+                       } else {
+                               $s .= ', ';
+                       }
+                       $s .= Xml::encodeJsVar( $arg );
+               }
+               $s .= ");\n";
+               return $s;
+       }
+
 
        /**
         * Check if a string is well-formed XML.
@@ -813,3 +842,23 @@ class XmlSelect {
        }
 
 }
+
+/**
+ * A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to 
+ * interpret a given string as being a JavaScript expression, instead of string 
+ * data.
+ *
+ * Example:
+ *
+ *    Xml::encodeJsVar( new XmlJsCode( 'a + b' ) );
+ *
+ * Returns "a + b".
+ * @since 1.17
+ */
+class XmlJsCode {
+       public $value;
+
+       function __construct( $value ) {
+               $this->value = $value;
+       }
+}