Unify the spelling of MIME in documentation
[lhc/web/wiklou.git] / includes / Html.php
index 5262ffe..46bee95 100644 (file)
@@ -36,7 +36,7 @@
  *
  * There are two important configuration options this class uses:
  *
- * $wgMimeType: If this is set to an xml mimetype then output should be
+ * $wgMimeType: If this is set to an xml MIME type then output should be
  *     valid XHTML5.
  * $wgWellFormedXml: If this is set to true, then all output should be
  *     well-formed XML (quotes on attributes, self-closing tags, etc.).
@@ -114,7 +114,7 @@ class Html {
         * shaved off the HTML output as well.
         *
         * @param string $element The element's name, e.g., 'a'
-        * @param array $attribs  Associative array of attributes, e.g., array(
+        * @param array $attribs Associative array of attributes, e.g., array(
         *   'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
         *   further documentation.
         * @param string $contents The raw HTML contents of the element: *not*
@@ -386,7 +386,7 @@ class Html {
         * For instance, it will omit quotation marks if $wgWellFormedXml is false,
         * and will treat boolean attributes specially.
         *
-        * Attributes that should contain space-separated lists (such as 'class') array
+        * Attributes that can contain space-separated lists ('class', 'accesskey' and 'rel') array
         * values are allowed as well, which will automagically be normalized
         * and converted to a space-separated string. In addition to a numerical
         * array, the attribute value may also be an associative array. See the
@@ -413,6 +413,8 @@ class Html {
         *   A value of false means to omit the attribute.  For boolean attributes,
         *   you can omit the key, e.g., array( 'checked' ) instead of
         *   array( 'checked' => 'checked' ) or such.
+        *
+        * @throws MWException If an attribute that doesn't allow lists is set to an array
         * @return string HTML fragment that goes between element name and '>'
         *   (starting with a space if at least one attribute is output)
         */
@@ -500,6 +502,8 @@ class Html {
 
                                // Remove duplicates and create the string
                                $value = implode( ' ', array_unique( $value ) );
+                       } elseif ( is_array( $value ) ) {
+                               throw new MWException( "HTML attribute $key can not contain a list of values" );
                        }
 
                        // See the "Attributes" section in the HTML syntax part of HTML5,
@@ -650,6 +654,64 @@ class Html {
                return self::element( 'input', $attribs );
        }
 
+       /**
+        * Convenience function to produce a checkbox (input element with type=checkbox)
+        *
+        * @param string $name Name attribute
+        * @param bool $checked Whether the checkbox is checked or not
+        * @param array $attribs Array of additional attributes
+        */
+       public static function check( $name, $checked = false, array $attribs = array() ) {
+               if ( isset( $attribs['value'] ) ) {
+                       $value = $attribs['value'];
+                       unset( $attribs['value'] );
+               } else {
+                       $value = 1;
+               }
+
+               if ( $checked ) {
+                       $attribs[] = 'checked';
+               }
+
+               return self::input( $name, $value, 'checkbox', $attribs );
+       }
+
+       /**
+        * Convenience function to produce a checkbox (input element with type=checkbox)
+        *
+        * @param string $name Name attribute
+        * @param bool $checked Whether the checkbox is checked or not
+        * @param array $attribs Array of additional attributes
+        */
+       public static function radio( $name, $checked = false, array $attribs = array() ) {
+               if ( isset( $attribs['value'] ) ) {
+                       $value = $attribs['value'];
+                       unset( $attribs['value'] );
+               } else {
+                       $value = 1;
+               }
+
+               if ( $checked ) {
+                       $attribs[] = 'checked';
+               }
+
+               return self::input( $name, $value, 'radio', $attribs );
+       }
+
+       /**
+        * Convenience function for generating a label for inputs.
+        *
+        * @param string $label Contents of the label
+        * @param string $id ID of the element being labeled
+        * @param array $attribs Additional attributes
+        */
+       public static function label( $label, $id, array $attribs = array() ) {
+               $attribs += array(
+                       'for' => $id
+               );
+               return self::element( 'label', $attribs, $label );
+       }
+
        /**
         * Convenience function to produce an input element with type=hidden
         *
@@ -810,7 +872,7 @@ class Html {
                $isXHTML = self::isXmlMimeType( $wgMimeType );
 
                if ( $isXHTML ) { // XHTML5
-                       // XML mimetyped markup should have an xml header.
+                       // XML MIME-typed markup should have an xml header.
                        // However a DOCTYPE is not needed.
                        $ret .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n";
 
@@ -842,16 +904,16 @@ class Html {
        }
 
        /**
-        * Determines if the given mime type is xml.
+        * Determines if the given MIME type is xml.
         *
-        * @param string $mimetype MimeType
+        * @param string $mimetype MIME type
         * @return bool
         */
        public static function isXmlMimeType( $mimetype ) {
                # http://www.whatwg.org/html/infrastructure.html#xml-mime-type
                # * text/xml
                # * application/xml
-               # * Any mimetype with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
+               # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
                return (bool)preg_match( '!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
        }