r55434 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55433‎ | r55434 | r55435 >
Date:20:50, 21 August 2009
Author:simetrical
Status:ok
Tags:
Comment:
Move validation logic from input() to rawElement()

This way callers of rawElement() or element() will also get correct
behavior.
Modified paths:
  • /trunk/phase3/includes/Html.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Html.php
@@ -105,8 +105,53 @@
106106 * @return string Raw HTML
107107 */
108108 public static function rawElement( $element, $attribs = array(), $contents = '' ) {
109 - global $wgWellFormedXml;
 109+ global $wgHtml5, $wgWellFormedXml;
 110+ # This is not required in HTML 5, but let's do it anyway, for
 111+ # consistency and better compression.
110112 $element = strtolower( $element );
 113+
 114+ # Element-specific hacks to slim down output and ensure validity
 115+ if ( $element == 'input' ) {
 116+ if ( !$wgHtml5 ) {
 117+ # With $wgHtml5 off we want to validate as XHTML 1, so we
 118+ # strip out any fancy HTML 5-only input types for now.
 119+ #
 120+ # Whitelist of valid types:
 121+ $validTypes = array(
 122+ 'hidden',
 123+ 'text',
 124+ 'password',
 125+ 'checkbox',
 126+ 'radio',
 127+ 'file',
 128+ 'submit',
 129+ 'image',
 130+ 'reset',
 131+ 'button',
 132+ );
 133+ if ( isset( $attribs['type'] )
 134+ && !in_array( $attribs['type'], $validTypes ) ) {
 135+ # Fall back to type=text, the default
 136+ unset( $attribs['type'] );
 137+ }
 138+ # Here we're blacklisting some HTML5-only attributes...
 139+ $html5attribs = array(
 140+ 'autocomplete',
 141+ 'autofocus',
 142+ 'max',
 143+ 'min',
 144+ 'multiple',
 145+ 'pattern',
 146+ 'placeholder',
 147+ 'required',
 148+ 'step',
 149+ );
 150+ foreach ( $html5attribs as $badAttr ) {
 151+ unset( $attribs[$badAttr] );
 152+ }
 153+ }
 154+ }
 155+
111156 $start = "<$element" . self::expandAttributes( $attribs );
112157 if ( in_array( $element, self::$voidElements ) ) {
113158 if ( $wgWellFormedXml ) {
@@ -288,44 +333,6 @@
289334 * @return string Raw HTML
290335 */
291336 public static function input( $name, $value = null, $type = 'text', $attribs = array() ) {
292 - global $wgHtml5;
293 -
294 - if ( !$wgHtml5 ) {
295 - // With $wgHtml5 off we want to validate as XHTML 1, so we
296 - // strip out any fancy HTML 5-only input types for now.
297 - //
298 - // Whitelist of valid types:
299 - $validTypes = array(
300 - 'hidden',
301 - 'text',
302 - 'password',
303 - 'checkbox',
304 - 'radio',
305 - 'file',
306 - 'submit',
307 - 'image',
308 - 'reset',
309 - 'button',
310 - );
311 - if ( !in_array( $type, $validTypes ) ) {
312 - $type = 'text';
313 - }
314 - // Here we're blacklisting some HTML5-only attributes...
315 - $html5attribs = array(
316 - 'autocomplete',
317 - 'autofocus',
318 - 'max',
319 - 'min',
320 - 'multiple',
321 - 'pattern',
322 - 'placeholder',
323 - 'required',
324 - 'step',
325 - );
326 - foreach ( $html5attribs as $badAttr ) {
327 - unset( $attribs[$badAttr] );
328 - }
329 - }
330337 if ( $type != 'text' ) {
331338 $attribs['type'] = $type;
332339 }

Status & tagging log