r87329 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87328‎ | r87329 | r87330 >
Date:15:38, 3 May 2011
Author:maxsem
Status:ok
Tags:
Comment:
Proper handling of inputs
Modified paths:
  • /trunk/extensions/ApiSandbox/ext.apiSandbox.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ApiSandbox/ext.apiSandbox.js
@@ -49,28 +49,34 @@
5050 $prop.change( updateBasics );
5151
5252 $submit.click( function() {
53 - var url = apiPhp + '?action=' + $action.val();
 53+ var url = apiPhp + '?action=' + $action.val(),
 54+ info = currentInfo; // in case it changes later
5455 if ( $action.val() == 'query' ) {
5556 url += '&prop=' + $prop.val();
5657 }
5758 url += '&format=json'; // @todo:
5859 var params = '';
59 - for ( var i = 0; i < currentInfo.parameters.length; i++ ) {
60 - var param = currentInfo.parameters[i];
61 - var name = currentInfo.prefix + param.name;
62 - var value = $( '#param-' + name ).val();
63 - if ( param.value == ''
64 - && ( param.type != 'boolean' && param.type != 'bool' )
65 - && !isset( param.required ) )
66 - {
67 - value = null;
68 - }
69 - if ( typeof value != 'undefined' ) {
 60+ for ( var i = 0; i < info.parameters.length; i++ ) {
 61+ var param = info.parameters[i],
 62+ name = info.prefix + param.name,
 63+ $node = $( '#param-' + name );
 64+ if ( param.type == 'boolean' ) {
 65+ if ( $node.attr( 'checked' ) ) {
 66+ params += '&' + name;
 67+ }
 68+ } else {
 69+ var value = $node.val();
 70+ if ( !isset( value ) || value == '' ) {
 71+ continue;
 72+ }
 73+ if ( $.isArray( value ) ) {
 74+ value = value.join( '|' );
 75+ }
7076 params += '&' + name + '=' + encodeURIComponent( value );
7177 }
7278 }
7379 showLoading( $output );
74 - if ( isset( currentInfo.mustbeposted ) ) {
 80+ if ( isset( info.mustbeposted ) ) {
7581 $requestUrl.val( url );
7682 $requestPost.val( params );
7783 $postRow.show();
@@ -83,7 +89,7 @@
8490 url: url,
8591 data: params,
8692 dataType: 'text',
87 - type: isset( currentInfo.mustbeposted ) ? 'POST' : 'GET',
 93+ type: isset( info.mustbeposted ) ? 'POST' : 'GET',
8894 success: function( data ) {
8995 data = data.match( /<pre>[\s\S]*<\/pre>/ )[0];
9096 $output.html( data );
@@ -206,6 +212,7 @@
207213 s = '<input class="api-sandbox-input" id="param-' + name + '" value="' + value + '"/>';
208214 break;
209215 case 'bool':
 216+ param.type = 'boolean'; // normalisation for later use
210217 case 'boolean':
211218 s = '<input id="param-' + name + '" type="checkbox"/>';
212219 break;

Status & tagging log