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.

This commit is contained in:
Aryeh Gregor 2008-07-30 22:02:23 +00:00
parent 76c30983c9
commit dc07d4785f
2 changed files with 11 additions and 18 deletions

View 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 ) {

View 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;
}