From dc07d4785f05e6d63c2d2f24728a1f1954994292 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Wed, 30 Jul 2008 22:02:23 +0000 Subject: [PATCH] Use Sanitizer::mergeAttributes() for Linker::linkAttribs(). Also clean up whitespace for mergeAttributes and reduce number of nested functions, and don't try to merge non-string 'class' arguments. This last point is necessary so I can have 'class' => false work right for linkAttribs(), but it makes sense. Parser tests pass. --- includes/Linker.php | 6 ++---- includes/Sanitizer.php | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/includes/Linker.php b/includes/Linker.php index ad9e0604d9..5b95abec6b 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -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 ) { diff --git a/includes/Sanitizer.php b/includes/Sanitizer.php index d71dee41cd..5f01e99c49 100644 --- a/includes/Sanitizer.php +++ b/includes/Sanitizer.php @@ -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; } -- 2.20.1