r38262 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r38261‎ | r38262 | r38263 >
Date:22:02, 30 July 2008
Author:simetrical
Status:old
Tags:
Comment:
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.
Modified paths:
  • /trunk/phase3/includes/Linker.php (modified) (history)
  • /trunk/phase3/includes/Sanitizer.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Linker.php
@@ -257,11 +257,9 @@
258258
259259 # Finally, merge the custom attribs with the default ones, and iterate
260260 # over that, deleting all "false" attributes.
261 - if( !empty( $attribs['class'] ) and !empty( $defaults['class'] ) ) {
262 - $attribs['class'] .= ' '.$defaults['class'];
263 - }
264261 $ret = array();
265 - foreach( array_merge( $defaults, $attribs ) as $key => $val ) {
 262+ $merged = Sanitizer::mergeAttributes( $defaults, $attribs );
 263+ foreach( $merged as $key => $val ) {
266264 # A false value suppresses the attribute, and we don't want the
267265 # href attribute to be overridden.
268266 if( $key != 'href' and $val !== false ) {
Index: trunk/phase3/includes/Sanitizer.php
@@ -627,10 +627,9 @@
628628 }
629629
630630 /**
631 - * Merge two sets of HTML attributes.
632 - * Conflicting items in the second set will override those
633 - * in the first, except for 'class' attributes which will be
634 - * combined.
 631+ * Merge two sets of HTML attributes. Conflicting items in the second set
 632+ * will override those in the first, except for 'class' attributes which
 633+ * will be combined (if they're both strings).
635634 *
636635 * @todo implement merging for other attributes such as style
637636 * @param array $a
@@ -639,16 +638,12 @@
640639 */
641640 static function mergeAttributes( $a, $b ) {
642641 $out = array_merge( $a, $b );
643 - if( isset( $a['class'] )
644 - && isset( $b['class'] )
645 - && $a['class'] !== $b['class'] ) {
646 -
647 - $out['class'] = implode( ' ',
648 - array_unique(
649 - preg_split( '/\s+/',
650 - $a['class'] . ' ' . $b['class'],
651 - -1,
652 - PREG_SPLIT_NO_EMPTY ) ) );
 642+ if( isset( $a['class'] ) && isset( $b['class'] )
 643+ && is_string( $a['class'] ) && is_string( $b['class'] )
 644+ && $a['class'] !== $b['class'] ) {
 645+ $classes = preg_split( '/\s+/', "{$a['class']} {$b['class']}",
 646+ -1, PREG_SPLIT_NO_EMPTY );
 647+ $out['class'] = implode( ' ', array_unique( $classes ) );
653648 }
654649 return $out;
655650 }

Status & tagging log