* 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() ) {
* 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' );
$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
$s .= self::encodeJsVar( $elt );
}
$s .= ']';
+ } elseif ( $value instanceof XmlJsCode ) {
+ $s = $value->value;
} elseif ( is_object( $value ) || is_array( $value ) ) {
// Objects and associative arrays
$s = '{';
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.
}
}
+
+/**
+ * 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;
+ }
+}