From 84a27cc33e562cf4764d4d1d67bdfe75ea966c07 Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Thu, 10 May 2007 20:51:15 +0000 Subject: [PATCH] Some fixes to avoid Xml::element call with null content when we actually want to open a XML element. It should help fix bug #5312 later on. * XML attributes rendering now made by private Xml::expandAttributes() method * Xml::openElement() and Xml::tags() no more rely on Xml::element() with a null content. * Fix wfElement() call with null content when we actually open an element. --- includes/HTMLForm.php | 2 +- includes/Xml.php | 33 +++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 715c8c8828..1ad0129f23 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -134,7 +134,7 @@ function HTMLSelectGroups($selectname, $selectmsg, $selected=array(), $multiple= $attribs = array( 'name' => $selectname ); } $attribs['style'] = 'width: 100%'; - $out .= wfElement( 'select', $attribs, null ); + $out .= wfOpenElement( 'select', $attribs ); foreach( $groups as $group ) { $attribs = array( 'value' => $group ); diff --git a/includes/Xml.php b/includes/Xml.php index aeb9d5a1e6..146232ac67 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -19,9 +19,7 @@ class Xml { public static function element( $element, $attribs = null, $contents = '') { $out = '<' . $element; if( !is_null( $attribs ) ) { - foreach( $attribs as $name => $val ) { - $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"'; - } + $out .= self::expandAttributes( $attribs ); } if( is_null( $contents ) ) { $out .= '>'; @@ -35,6 +33,25 @@ class Xml { return $out; } + /** + * Given an array of ('attributename' => 'value'), it generates the code + * to set the XML attributes : attributename="value". + * The values are passed to Sanitizer::encodeAttribute. + * Return null if no attributes given. + * @param $attribs Array of attributes for an XML element + */ + private static function expandAttributes( $attribs ) { + if( is_null( $attribs ) ) { + return null; + } else { + $out = ''; + foreach( $attribs as $name => $val ) { + $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"'; + } + return $out; + } + } + /** * Format an XML element as with self::element(), but run text through the * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8 @@ -57,8 +74,12 @@ class Xml { return self::element( $element, $attribs, $contents ); } - // Shortcuts - public static function openElement( $element, $attribs = null ) { return self::element( $element, $attribs, null ); } + /** This open an XML element */ + public static function openElement( $element, $attribs = null ) { + return '<' . $element . self::expandAttributes( $attribs ) . '>'; + } + + // Shortcut public static function closeElement( $element ) { return ""; } /** @@ -66,7 +87,7 @@ class Xml { * content you have is already valid xml. */ public static function tags( $element, $attribs = null, $contents ) { - return self::element( $element, $attribs, null ) . $contents . ""; + return self::openElement( $element, $attribs ) . $contents . ""; } /** -- 2.20.1