From cebad72f95013c2cd726ce738624a798aa0c11ce Mon Sep 17 00:00:00 2001 From: Daniel Friesen Date: Sat, 3 Sep 2011 14:36:58 +0000 Subject: [PATCH] Expand r96170's support for space separated attributes with support for boolean keys such as array( 'class' => array( 'selected' => true ) ) to match our array( 'checked' => false ) support. As per discussion with Krinkle make sure that in array( 'foo', 'foo' => false, 'foo' ) the 'foo' key is authoritive. --- includes/Html.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/includes/Html.php b/includes/Html.php index fcdecaa82d..a43e572a7f 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -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 ) ); -- 2.20.1