Merge "Add new hook WatchlistEditorBeforeFormRender"
[lhc/web/wiklou.git] / includes / Html.php
index ce439cb..48dbdba 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.).
@@ -101,6 +101,35 @@ class Html {
                'itemscope',
        );
 
+       /**
+        * Modifies a set of attributes meant for text input elements
+        * and apply a set of default attributes.
+        * Removes size attribute when $wgUseMediaWikiUIEverywhere enabled.
+        * @param array $attrs An attribute array.
+        * @return array $attrs A modified attribute array
+        */
+       public static function getTextInputAttributes( $attrs ) {
+               global $wgUseMediaWikiUIEverywhere;
+               if ( !$attrs ) {
+                       $attrs = array();
+               }
+               if ( isset( $attrs['class'] ) ) {
+                       if ( is_array( $attrs['class'] ) ) {
+                               $attrs['class'][] = 'mw-ui-input';
+                       } else {
+                               $attrs['class'] .= ' mw-ui-input';
+                       }
+               } else {
+                       $attrs['class'] = 'mw-ui-input';
+               }
+               if ( $wgUseMediaWikiUIEverywhere ) {
+                       // Note that size can effect the desired width rendering of mw-ui-input elements
+                       // so it is removed. Left intact when mediawiki ui not enabled.
+                       unset( $attrs['size'] );
+               }
+               return $attrs;
+       }
+
        /**
         * Returns an HTML element in a string.  The major advantage here over
         * manually typing out the HTML is that it will escape all attribute
@@ -225,33 +254,15 @@ class Html {
        }
 
        /**
-        * Returns "</$element>", except if $wgWellFormedXml is off, in which case
-        * it returns the empty string when that's guaranteed to be safe.
+        * Returns "</$element>"
         *
         * @since 1.17
         * @param string $element Name of the element, e.g., 'a'
-        * @return string A closing tag, if required
+        * @return string A closing tag
         */
        public static function closeElement( $element ) {
-               global $wgWellFormedXml;
-
                $element = strtolower( $element );
 
-               // Reference:
-               // http://www.whatwg.org/html/syntax.html#optional-tags
-               if ( !$wgWellFormedXml && in_array( $element, array(
-                       'html',
-                       'head',
-                       'body',
-                       'li',
-                       'dt',
-                       'dd',
-                       'tr',
-                       'td',
-                       'th',
-               ) ) ) {
-                       return '';
-               }
                return "</$element>";
        }
 
@@ -650,7 +661,9 @@ class Html {
                $attribs['type'] = $type;
                $attribs['value'] = $value;
                $attribs['name'] = $name;
-
+               if ( in_array( $type, array( 'text', 'search', 'email', 'password', 'number' ) ) ) {
+                       $attribs = Html::getTextInputAttributes( $attribs );
+               }
                return self::element( 'input', $attribs );
        }
 
@@ -660,6 +673,7 @@ class Html {
         * @param string $name Name attribute
         * @param bool $checked Whether the checkbox is checked or not
         * @param array $attribs Array of additional attributes
+        * @return string
         */
        public static function check( $name, $checked = false, array $attribs = array() ) {
                if ( isset( $attribs['value'] ) ) {
@@ -682,6 +696,7 @@ class Html {
         * @param string $name Name attribute
         * @param bool $checked Whether the checkbox is checked or not
         * @param array $attribs Array of additional attributes
+        * @return string
         */
        public static function radio( $name, $checked = false, array $attribs = array() ) {
                if ( isset( $attribs['value'] ) ) {
@@ -704,6 +719,7 @@ class Html {
         * @param string $label Contents of the label
         * @param string $id ID of the element being labeled
         * @param array $attribs Additional attributes
+        * @return string
         */
        public static function label( $label, $id, array $attribs = array() ) {
                $attribs += array(
@@ -749,7 +765,7 @@ class Html {
                } else {
                        $spacedValue = $value;
                }
-               return self::element( 'textarea', $attribs, $spacedValue );
+               return self::element( 'textarea', Html::getTextInputAttributes( $attribs ), $spacedValue );
        }
 
        /**
@@ -872,7 +888,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";
 
@@ -904,16 +920,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 );
        }