r22083 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22082‎ | r22083 | r22084 >
Date:20:51, 10 May 2007
Author:hashar
Status:old
Tags:
Comment:
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.
Modified paths:
  • /trunk/phase3/includes/HTMLForm.php (modified) (history)
  • /trunk/phase3/includes/Xml.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/HTMLForm.php
@@ -134,7 +134,7 @@
135135 $attribs = array( 'name' => $selectname );
136136 }
137137 $attribs['style'] = 'width: 100%';
138 - $out .= wfElement( 'select', $attribs, null );
 138+ $out .= wfOpenElement( 'select', $attribs );
139139
140140 foreach( $groups as $group ) {
141141 $attribs = array( 'value' => $group );
Index: trunk/phase3/includes/Xml.php
@@ -19,9 +19,7 @@
2020 public static function element( $element, $attribs = null, $contents = '') {
2121 $out = '<' . $element;
2222 if( !is_null( $attribs ) ) {
23 - foreach( $attribs as $name => $val ) {
24 - $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"';
25 - }
 23+ $out .= self::expandAttributes( $attribs );
2624 }
2725 if( is_null( $contents ) ) {
2826 $out .= '>';
@@ -36,6 +34,25 @@
3735 }
3836
3937 /**
 38+ * Given an array of ('attributename' => 'value'), it generates the code
 39+ * to set the XML attributes : attributename="value".
 40+ * The values are passed to Sanitizer::encodeAttribute.
 41+ * Return null if no attributes given.
 42+ * @param $attribs Array of attributes for an XML element
 43+ */
 44+ private static function expandAttributes( $attribs ) {
 45+ if( is_null( $attribs ) ) {
 46+ return null;
 47+ } else {
 48+ $out = '';
 49+ foreach( $attribs as $name => $val ) {
 50+ $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"';
 51+ }
 52+ return $out;
 53+ }
 54+ }
 55+
 56+ /**
4057 * Format an XML element as with self::element(), but run text through the
4158 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
4259 * is passed.
@@ -57,8 +74,12 @@
5875 return self::element( $element, $attribs, $contents );
5976 }
6077
61 - // Shortcuts
62 - public static function openElement( $element, $attribs = null ) { return self::element( $element, $attribs, null ); }
 78+ /** This open an XML element */
 79+ public static function openElement( $element, $attribs = null ) {
 80+ return '<' . $element . self::expandAttributes( $attribs ) . '>';
 81+ }
 82+
 83+ // Shortcut
6384 public static function closeElement( $element ) { return "</$element>"; }
6485
6586 /**
@@ -66,7 +87,7 @@
6788 * content you have is already valid xml.
6889 */
6990 public static function tags( $element, $attribs = null, $contents ) {
70 - return self::element( $element, $attribs, null ) . $contents . "</$element>";
 91+ return self::openElement( $element, $attribs ) . $contents . "</$element>";
7192 }
7293
7394 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r22086Dont rely Xml::element with null content to open an element instead...hashar21:09, 10 May 2007