Use Sanitizer::mergeAttributes() for Linker::linkAttribs(). Also clean up whitespace...
authorAryeh Gregor <simetrical@users.mediawiki.org>
Wed, 30 Jul 2008 22:02:23 +0000 (22:02 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Wed, 30 Jul 2008 22:02:23 +0000 (22:02 +0000)
includes/Linker.php
includes/Sanitizer.php

index ad9e060..5b95abe 100644 (file)
@@ -257,11 +257,9 @@ class Linker {
 
                # Finally, merge the custom attribs with the default ones, and iterate
                # over that, deleting all "false" attributes.
-               if( !empty( $attribs['class'] ) and !empty( $defaults['class'] ) ) {
-                       $attribs['class'] .= ' '.$defaults['class'];
-               }
                $ret = array();
-               foreach( array_merge( $defaults, $attribs ) as $key => $val ) {
+               $merged = Sanitizer::mergeAttributes( $defaults, $attribs );
+               foreach( $merged as $key => $val ) {
                        # A false value suppresses the attribute, and we don't want the
                        # href attribute to be overridden.
                        if( $key != 'href' and $val !== false ) {
index d71dee4..5f01e99 100644 (file)
@@ -627,10 +627,9 @@ class Sanitizer {
        }
 
        /**
-        * Merge two sets of HTML attributes.
-        * Conflicting items in the second set will override those
-        * in the first, except for 'class' attributes which will be
-        * combined.
+        * Merge two sets of HTML attributes.  Conflicting items in the second set
+        * will override those in the first, except for 'class' attributes which
+        * will be combined (if they're both strings).
         *
         * @todo implement merging for other attributes such as style
         * @param array $a
@@ -639,16 +638,12 @@ class Sanitizer {
         */
        static function mergeAttributes( $a, $b ) {
                $out = array_merge( $a, $b );
-               if( isset( $a['class'] )
-                       && isset( $b['class'] )
-                       && $a['class'] !== $b['class'] ) {
-
-                       $out['class'] = implode( ' ',
-                               array_unique(
-                                       preg_split( '/\s+/',
-                                               $a['class'] . ' ' . $b['class'],
-                                               -1,
-                                               PREG_SPLIT_NO_EMPTY ) ) );
+               if( isset( $a['class'] ) && isset( $b['class'] )
+               && is_string( $a['class'] ) && is_string( $b['class'] )
+               && $a['class'] !== $b['class'] ) {
+                       $classes = preg_split( '/\s+/', "{$a['class']} {$b['class']}",
+                               -1, PREG_SPLIT_NO_EMPTY );
+                       $out['class'] = implode( ' ', array_unique( $classes ) );
                }
                return $out;
        }