Expand r96170's support for space separated attributes with support for boolean keys...
authorDaniel Friesen <dantman@users.mediawiki.org>
Sat, 3 Sep 2011 14:36:58 +0000 (14:36 +0000)
committerDaniel Friesen <dantman@users.mediawiki.org>
Sat, 3 Sep 2011 14:36:58 +0000 (14:36 +0000)
As per discussion with Krinkle make sure that in array( 'foo', 'foo' => false, 'foo' ) the 'foo' key is authoritive.

includes/Html.php

index fcdecaa..a43e572 100644 (file)
@@ -432,13 +432,31 @@ class Html {
                                // values. Implode/explode to get those into the main array as well.
                                if ( is_array( $value ) ) {
                                        // If input wasn't an array, we can skip this step
-                                       $value = implode( ' ', $value );
+                                       
+                                       $newValue = array();
+                                       foreach ( $value as $k => $v ) {
+                                               if ( is_string( $v ) ) {
+                                                       // String values should be normal `array( 'foo' )`
+                                                       // Just append them
+                                                       if ( !isset( $value[$v] ) ) {
+                                                               // As a special case don't set 'foo' if a
+                                                               // separate 'foo' => true/false exists in the array
+                                                               // keys should be authoritive
+                                                               $newValue[] = $v;
+                                                       }
+                                               } elseif ( $v ) {
+                                                       // If the value is truthy but not a string this is likely
+                                                       // an array( 'foo' => true ), falsy values don't add strings
+                                                       $newValue[] = $k;
+                                               }
+                                       }
+                                       $value = implode( ' ', $newValue );
                                }
                                $value = explode( ' ', $value );
 
                                // Normalize spacing by fixing up cases where people used
                                // more than 1 space and/or a trailing/leading space
-                               $value = array_diff( $value, array( '', ' ') );
+                               $value = array_diff( $value, array( '', ' ' ) );
 
                                // Remove duplicates and create the string
                                $value = implode( ' ', array_unique( $value ) );