r107402 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107401‎ | r107402 | r107403 >
Date:18:25, 27 December 2011
Author:krinkle
Status:ok
Tags:
Comment:
[mediawiki.js] code quality and clean up
* using local mw variable in file instead of reaching to global scope
* exposing to global object after initialization
* moving var statements to the top of the function, this uncovered a few risky re-use of variables. Fixed by using different names (in nested for-loops and unconnected if/else statements). This also made several awkward indentions go away (where the first definition would be indented for 'var', and later ones not).
* remove unused "messageQueue" variable
* enable strict mode in modern browsers (loose string ignored in other browsers)
* where looking to check for array, use $.isArray() (directly pointed to native code) instead of a typeof operation with string comparison for "object" (slightly faster and also semantically more correct)
* other best practices as popularized by JSLint/JSHint
* removed 'delete' operator for startUp, didn't' do anything in most modern browsers, only for object members.

@note: mw.loader.work really is big, now it's more obvious:
<code>
+var reqBase, splits, maxQueryLength, q, b, bSource, bGroup, bSourceGroup,
+ source, group, g, i, modules, maxVersion, sourceLoadScript,
+ currReqBase, currReqBaseLength, moduleMap, l,
+ lastDotIndex, prefix, suffix, bytesAdded;
</code>
Modified paths:
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -2,15 +2,12 @@
33 * Core MediaWiki JavaScript Library
44 */
55
6 -// Attach to window and globally alias
7 -window.mw = window.mediaWiki = new ( function( $, undefined ) {
 6+var mw = new ( function( $, undefined ) {
 7+"use strict";
88
99 /* Private Members */
1010
11 - /**
12 - * @var object List of messages that have been requested to be loaded.
13 - */
14 - var messageQueue = {};
 11+ var hasOwn = Object.prototype.hasOwnProperty;
1512
1613 /* Object constructors */
1714
@@ -25,7 +22,7 @@
2623 * @return Map
2724 */
2825 function Map( global ) {
29 - this.values = ( global === true ) ? window : {};
 26+ this.values = global === true ? window : {};
3027 return this;
3128 }
3229
@@ -44,10 +41,12 @@
4542 * @return Values as a string or object, null if invalid/inexistant.
4643 */
4744 get: function( selection, fallback ) {
 45+ var results, i;
 46+
4847 if ( $.isArray( selection ) ) {
4948 selection = $.makeArray( selection );
50 - var results = {};
51 - for ( var i = 0; i < selection.length; i++ ) {
 49+ results = {};
 50+ for ( i = 0; i < selection.length; i += 1 ) {
5251 results[selection[i]] = this.get( selection[i], fallback );
5352 }
5453 return results;
@@ -70,13 +69,15 @@
7170 /**
7271 * Sets one or multiple key/value pairs.
7372 *
74 - * @param selection mixed String key or array of keys to set values for.
75 - * @param value mixed Value to set (optional, only in use when key is a string)
76 - * @return bool This returns true on success, false on failure.
 73+ * @param selection {mixed} String key or array of keys to set values for.
 74+ * @param value {mixed} Value to set (optional, only in use when key is a string)
 75+ * @return {Boolean} This returns true on success, false on failure.
7776 */
7877 set: function( selection, value ) {
 78+ var s;
 79+
7980 if ( $.isPlainObject( selection ) ) {
80 - for ( var s in selection ) {
 81+ for ( s in selection ) {
8182 this.values[s] = selection[s];
8283 }
8384 return true;
@@ -90,19 +91,21 @@
9192 /**
9293 * Checks if one or multiple keys exist.
9394 *
94 - * @param selection mixed String key or array of keys to check
95 - * @return boolean Existence of key(s)
 95+ * @param selection {mixed} String key or array of keys to check
 96+ * @return {Boolean} Existence of key(s)
9697 */
9798 exists: function( selection ) {
98 - if ( typeof selection === 'object' ) {
99 - for ( var s = 0; s < selection.length; s++ ) {
100 - if ( !( selection[s] in this.values ) ) {
 99+ var s;
 100+
 101+ if ( $.isArray( selection ) ) {
 102+ for ( s = 0; s < selection.length; s += 1 ) {
 103+ if ( this.values[selection[s]] === undefined ) {
101104 return false;
102105 }
103106 }
104107 return true;
105108 } else {
106 - return selection in this.values;
 109+ return this.values[selection] !== undefined;
107110 }
108111 }
109112 };
@@ -134,7 +137,8 @@
135138 * @return Message
136139 */
137140 params: function( parameters ) {
138 - for ( var i = 0; i < parameters.length; i++ ) {
 141+ var i;
 142+ for ( i = 0; i < parameters.length; i += 1 ) {
139143 this.parameters.push( parameters[i] );
140144 }
141145 return this;
@@ -146,7 +150,6 @@
147151 * @return string Message as a string in the current form or <key> if key does not exist.
148152 */
149153 toString: function() {
150 -
151154 if ( !this.map.exists( this.key ) ) {
152155 // Use <key> as text if key does not exist
153156 if ( this.format !== 'plain' ) {
@@ -158,9 +161,9 @@
159162 var text = this.map.get( this.key ),
160163 parameters = this.parameters;
161164
162 - text = text.replace( /\$(\d+)/g, function( string, match ) {
 165+ text = text.replace( /\$(\d+)/g, function( str, match ) {
163166 var index = parseInt( match, 10 ) - 1;
164 - return index in parameters ? parameters[index] : '$' + match;
 167+ return parameters[index] !== undefined ? parameters[index] : '$' + match;
165168 } );
166169
167170 if ( this.format === 'plain' ) {
@@ -361,10 +364,11 @@
362365 }
363366
364367 function compare( a, b ) {
 368+ var i;
365369 if ( a.length !== b.length ) {
366370 return false;
367371 }
368 - for ( var i = 0; i < b.length; i++ ) {
 372+ for ( i = 0; i < b.length; i += 1 ) {
369373 if ( $.isArray( a[i] ) ) {
370374 if ( !compare( a[i], b[i] ) ) {
371375 return false;
@@ -396,6 +400,8 @@
397401 * Recursively resolves dependencies and detects circular references
398402 */
399403 function recurse( module, resolved, unresolved ) {
 404+ var n, deps, len;
 405+
400406 if ( registry[module] === undefined ) {
401407 throw new Error( 'Unknown dependency: ' + module );
402408 }
@@ -408,15 +414,17 @@
409415 }
410416 }
411417 // Tracks down dependencies
412 - for ( var n = 0; n < registry[module].dependencies.length; n++ ) {
413 - if ( $.inArray( registry[module].dependencies[n], resolved ) === -1 ) {
414 - if ( $.inArray( registry[module].dependencies[n], unresolved ) !== -1 ) {
 418+ deps = registry[module].dependencies;
 419+ len = deps.length;
 420+ for ( n = 0; n < len; n += 1 ) {
 421+ if ( $.inArray( deps[n], resolved ) === -1 ) {
 422+ if ( $.inArray( deps[n], unresolved ) !== -1 ) {
415423 throw new Error(
416424 'Circular reference detected: ' + module +
417 - ' -> ' + registry[module].dependencies[n]
 425+ ' -> ' + deps[n]
418426 );
419427 }
420 - recurse( registry[module].dependencies[n], resolved, unresolved );
 428+ recurse( deps[n], resolved, unresolved );
421429 }
422430 }
423431 resolved[resolved.length] = module;
@@ -431,22 +439,24 @@
432440 * @throws Error if circular reference is detected
433441 */
434442 function resolve( module ) {
 443+ var modules, m, deps, n, resolved;
 444+
435445 // Allow calling with an array of module names
436 - if ( typeof module === 'object' ) {
437 - var modules = [];
438 - for ( var m = 0; m < module.length; m++ ) {
439 - var dependencies = resolve( module[m] );
440 - for ( var n = 0; n < dependencies.length; n++ ) {
441 - modules[modules.length] = dependencies[n];
 446+ if ( $.isArray( module ) ) {
 447+ modules = [];
 448+ for ( m = 0; m < module.length; m += 1 ) {
 449+ deps = resolve( module[m] );
 450+ for ( n = 0; n < deps.length; n += 1 ) {
 451+ modules[modules.length] = deps[n];
442452 }
443453 }
444454 return modules;
445455 } else if ( typeof module === 'string' ) {
446456 // Undefined modules have no dependencies
447 - if ( !( module in registry ) ) {
 457+ if ( registry[module] === undefined ) {
448458 return [];
449459 }
450 - var resolved = [];
 460+ resolved = [];
451461 recurse( module, resolved, [] );
452462 return resolved;
453463 }
@@ -464,12 +474,14 @@
465475 * @return array list of filtered module names
466476 */
467477 function filter( states, modules ) {
 478+ var list, module, s, m;
 479+
468480 // Allow states to be given as a string
469481 if ( typeof states === 'string' ) {
470482 states = [states];
471483 }
472484 // If called without a list of modules, build and use a list of all modules
473 - var list = [], module;
 485+ list = [];
474486 if ( modules === undefined ) {
475487 modules = [];
476488 for ( module in registry ) {
@@ -477,8 +489,8 @@
478490 }
479491 }
480492 // Build a list of modules which are in one of the specified states
481 - for ( var s = 0; s < states.length; s++ ) {
482 - for ( var m = 0; m < modules.length; m++ ) {
 493+ for ( s = 0; s < states.length; s += 1 ) {
 494+ for ( m = 0; m < modules.length; m += 1 ) {
483495 if ( registry[modules[m]] === undefined ) {
484496 // Module does not exist
485497 if ( states[s] === 'undefined' ) {
@@ -503,6 +515,8 @@
504516 * @param module string module name to execute
505517 */
506518 function execute( module, callback ) {
 519+ var style, media, i, script, markModuleReady, nestedAddScript;
 520+
507521 if ( registry[module] === undefined ) {
508522 throw new Error( 'Module has not been registered yet: ' + module );
509523 } else if ( registry[module].state === 'registered' ) {
@@ -512,13 +526,13 @@
513527 } else if ( registry[module].state === 'ready' ) {
514528 throw new Error( 'Module has already been loaded: ' + module );
515529 }
 530+
516531 // Add styles
517 - var style;
518532 if ( $.isPlainObject( registry[module].style ) ) {
519 - for ( var media in registry[module].style ) {
 533+ for ( media in registry[module].style ) {
520534 style = registry[module].style[media];
521535 if ( $.isArray( style ) ) {
522 - for ( var i = 0; i < style.length; i++ ) {
 536+ for ( i = 0; i < style.length; i += 1 ) {
523537 getMarker().before( mw.html.element( 'link', {
524538 'type': 'text/css',
525539 'media': media,
@@ -540,27 +554,27 @@
541555 }
542556 // Execute script
543557 try {
544 - var script = registry[module].script,
545 - markModuleReady = function() {
546 - registry[module].state = 'ready';
547 - handlePending( module );
548 - if ( $.isFunction( callback ) ) {
549 - callback();
550 - }
551 - },
552 - nestedAddScript = function( arr, callback, i ) {
553 - // Recursively call addScript() in its own callback
554 - // for each element of arr.
555 - if ( i >= arr.length ) {
556 - // We're at the end of the array
557 - callback();
558 - return;
559 - }
 558+ script = registry[module].script;
 559+ markModuleReady = function() {
 560+ registry[module].state = 'ready';
 561+ handlePending( module );
 562+ if ( $.isFunction( callback ) ) {
 563+ callback();
 564+ }
 565+ };
 566+ nestedAddScript = function( arr, callback, i ) {
 567+ // Recursively call addScript() in its own callback
 568+ // for each element of arr.
 569+ if ( i >= arr.length ) {
 570+ // We're at the end of the array
 571+ callback();
 572+ return;
 573+ }
560574
561 - addScript( arr[i], function() {
562 - nestedAddScript( arr, callback, i + 1 );
563 - } );
564 - };
 575+ addScript( arr[i], function() {
 576+ nestedAddScript( arr, callback, i + 1 );
 577+ } );
 578+ };
565579
566580 if ( $.isArray( script ) ) {
567581 registry[module].state = 'loading';
@@ -586,9 +600,11 @@
587601 * This is used when dependencies are satisfied, such as when a module is executed.
588602 */
589603 function handlePending( module ) {
 604+ var j, r;
 605+
590606 try {
591607 // Run jobs who's dependencies have just been met
592 - for ( var j = 0; j < jobs.length; j++ ) {
 608+ for ( j = 0; j < jobs.length; j += 1 ) {
593609 if ( compare(
594610 filter( 'ready', jobs[j].dependencies ),
595611 jobs[j].dependencies ) )
@@ -597,11 +613,11 @@
598614 jobs[j].ready();
599615 }
600616 jobs.splice( j, 1 );
601 - j--;
 617+ j -= 1;
602618 }
603619 }
604620 // Execute modules who's dependencies have just been met
605 - for ( var r in registry ) {
 621+ for ( r in registry ) {
606622 if ( registry[r].state === 'loaded' ) {
607623 if ( compare(
608624 filter( ['ready'], registry[r].dependencies ),
@@ -613,13 +629,13 @@
614630 }
615631 } catch ( e ) {
616632 // Run error callbacks of jobs affected by this condition
617 - for ( var j = 0; j < jobs.length; j++ ) {
 633+ for ( j = 0; j < jobs.length; j += 1 ) {
618634 if ( $.inArray( module, jobs[j].dependencies ) !== -1 ) {
619635 if ( $.isFunction( jobs[j].error ) ) {
620636 jobs[j].error( e, module );
621637 }
622638 jobs.splice( j, 1 );
623 - j--;
 639+ j -= 1;
624640 }
625641 }
626642 }
@@ -634,15 +650,17 @@
635651 * @param error function callback to execute when any dependency fails
636652 */
637653 function request( dependencies, ready, error ) {
 654+ var regItemDeps, regItemDepLen, n;
 655+
638656 // Allow calling by single module name
639657 if ( typeof dependencies === 'string' ) {
640658 dependencies = [dependencies];
641 - if ( dependencies[0] in registry ) {
 659+ if ( registry[dependencies[0]] !== undefined ) {
642660 // Cache repetitively accessed deep level object member
643 - var regItemDeps = registry[dependencies[0]].dependencies,
644 - // Cache to avoid looped access to length property
645 - regItemDepLen = regItemDeps.length;
646 - for ( var n = 0; n < regItemDepLen; n++ ) {
 661+ regItemDeps = registry[dependencies[0]].dependencies;
 662+ // Cache to avoid looped access to length property
 663+ regItemDepLen = regItemDeps.length;
 664+ for ( n = 0; n < regItemDepLen; n += 1 ) {
647665 dependencies[dependencies.length] = regItemDeps[n];
648666 }
649667 }
@@ -660,7 +678,7 @@
661679 }
662680 // Queue up any dependencies that are undefined or registered
663681 dependencies = filter( ['undefined', 'registered'], dependencies );
664 - for ( var n = 0; n < dependencies.length; n++ ) {
 682+ for ( n = 0; n < dependencies.length; n += 1 ) {
665683 if ( $.inArray( dependencies[n], queue ) === -1 ) {
666684 queue[queue.length] = dependencies[n];
667685 }
@@ -672,12 +690,12 @@
673691 function sortQuery(o) {
674692 var sorted = {}, key, a = [];
675693 for ( key in o ) {
676 - if ( o.hasOwnProperty( key ) ) {
 694+ if ( hasOwn.call( o, key ) ) {
677695 a.push( key );
678696 }
679697 }
680698 a.sort();
681 - for ( key = 0; key < a.length; key++ ) {
 699+ for ( key = 0; key < a.length; key += 1 ) {
682700 sorted[a[key]] = o[a[key]];
683701 }
684702 return sorted;
@@ -688,8 +706,8 @@
689707 * to a query string of the form foo.bar,baz|bar.baz,quux
690708 */
691709 function buildModulesString( moduleMap ) {
692 - var arr = [], p;
693 - for ( var prefix in moduleMap ) {
 710+ var arr = [], p, prefix;
 711+ for ( prefix in moduleMap ) {
694712 p = prefix === '' ? '' : prefix + '.';
695713 arr.push( p + moduleMap[prefix].join( ',' ) );
696714 }
@@ -713,7 +731,7 @@
714732 script.setAttribute( 'src', src );
715733 script.setAttribute( 'type', 'text/javascript' );
716734 if ( $.isFunction( callback ) ) {
717 - // Attach handlers for all browsers -- this is based on jQuery.ajax
 735+ // Attach handlers for all browsers (based on jQuery.ajax)
718736 script.onload = script.onreadystatechange = function() {
719737
720738 if (
@@ -776,25 +794,30 @@
777795 * Requests dependencies from server, loading and executing when things when ready.
778796 */
779797 this.work = function() {
780 - // Build a list of request parameters common to all requests.
781 - var reqBase = {
782 - skin: mw.config.get( 'skin' ),
783 - lang: mw.config.get( 'wgUserLanguage' ),
784 - debug: mw.config.get( 'debug' )
785 - },
786 - // Split module batch by source and by group.
787 - splits = {},
788 - maxQueryLength = mw.config.get( 'wgResourceLoaderMaxQueryLength', -1 );
 798+ var reqBase, splits, maxQueryLength, q, b, bSource, bGroup, bSourceGroup,
 799+ source, group, g, i, modules, maxVersion, sourceLoadScript,
 800+ currReqBase, currReqBaseLength, moduleMap, l,
 801+ lastDotIndex, prefix, suffix, bytesAdded;
789802
 803+ // Build a list of request parameters common to all requests.
 804+ reqBase = {
 805+ skin: mw.config.get( 'skin' ),
 806+ lang: mw.config.get( 'wgUserLanguage' ),
 807+ debug: mw.config.get( 'debug' )
 808+ };
 809+ // Split module batch by source and by group.
 810+ splits = {};
 811+ maxQueryLength = mw.config.get( 'wgResourceLoaderMaxQueryLength', -1 );
 812+
790813 // Appends a list of modules from the queue to the batch
791 - for ( var q = 0; q < queue.length; q++ ) {
 814+ for ( q = 0; q < queue.length; q += 1 ) {
792815 // Only request modules which are undefined or registered
793 - if ( !( queue[q] in registry ) || registry[queue[q]].state === 'registered' ) {
 816+ if ( registry[queue[q]] === undefined || registry[queue[q]].state === 'registered' ) {
794817 // Prevent duplicate entries
795818 if ( $.inArray( queue[q], batch ) === -1 ) {
796819 batch[batch.length] = queue[q];
797820 // Mark registered modules as loading
798 - if ( queue[q] in registry ) {
 821+ if ( registry[queue[q]] !== undefined ) {
799822 registry[queue[q]].state = 'loading';
800823 }
801824 }
@@ -813,16 +836,16 @@
814837 batch.sort();
815838
816839 // Split batch by source and by group.
817 - for ( var b = 0; b < batch.length; b++ ) {
818 - var bSource = registry[batch[b]].source,
819 - bGroup = registry[batch[b]].group;
820 - if ( !( bSource in splits ) ) {
 840+ for ( b = 0; b < batch.length; b += 1 ) {
 841+ bSource = registry[batch[b]].source;
 842+ bGroup = registry[batch[b]].group;
 843+ if ( splits[bSource] === undefined ) {
821844 splits[bSource] = {};
822845 }
823 - if ( !( bGroup in splits[bSource] ) ) {
 846+ if ( splits[bSource][bGroup] === undefined ) {
824847 splits[bSource][bGroup] = [];
825848 }
826 - var bSourceGroup = splits[bSource][bGroup];
 849+ bSourceGroup = splits[bSource][bGroup];
827850 bSourceGroup[bSourceGroup.length] = batch[b];
828851 }
829852
@@ -833,8 +856,6 @@
834857 // include modules which are already loaded.
835858 batch = [];
836859
837 - var source, group, modules, maxVersion, sourceLoadScript;
838 -
839860 for ( source in splits ) {
840861
841862 sourceLoadScript = sources[source].loadScript;
@@ -847,30 +868,30 @@
848869
849870 // Calculate the highest timestamp
850871 maxVersion = 0;
851 - for ( var g = 0; g < modules.length; g++ ) {
 872+ for ( g = 0; g < modules.length; g += 1 ) {
852873 if ( registry[modules[g]].version > maxVersion ) {
853874 maxVersion = registry[modules[g]].version;
854875 }
855876 }
856877
857 - var currReqBase = $.extend( { 'version': formatVersionNumber( maxVersion ) }, reqBase ),
858 - currReqBaseLength = $.param( currReqBase ).length,
859 - moduleMap = {},
860 - // We may need to split up the request to honor the query string length limit,
861 - // so build it piece by piece.
862 - l = currReqBaseLength + 9; // '&modules='.length == 9
 878+ currReqBase = $.extend( { 'version': formatVersionNumber( maxVersion ) }, reqBase );
 879+ currReqBaseLength = $.param( currReqBase ).length;
 880+ moduleMap = {};
 881+ // We may need to split up the request to honor the query string length limit,
 882+ // so build it piece by piece.
 883+ l = currReqBaseLength + 9; // '&modules='.length == 9
863884
864885 moduleMap = {}; // { prefix: [ suffixes ] }
865886
866 - for ( var i = 0; i < modules.length; i++ ) {
867 - // Determine how many bytes this module would add to the query string
868 - var lastDotIndex = modules[i].lastIndexOf( '.' ),
869 - // Note that these substr() calls work even if lastDotIndex == -1
870 - prefix = modules[i].substr( 0, lastDotIndex ),
871 - suffix = modules[i].substr( lastDotIndex + 1 ),
872 - bytesAdded = prefix in moduleMap
873 - ? suffix.length + 3 // '%2C'.length == 3
874 - : modules[i].length + 3; // '%7C'.length == 3
 887+ for ( i = 0; i < modules.length; i += 1 ) {
 888+ // Determine how many bytes this module would add to the query string
 889+ lastDotIndex = modules[i].lastIndexOf( '.' );
 890+ // Note that these substr() calls work even if lastDotIndex == -1
 891+ prefix = modules[i].substr( 0, lastDotIndex );
 892+ suffix = modules[i].substr( lastDotIndex + 1 );
 893+ bytesAdded = moduleMap[prefix] !== undefined
 894+ ? suffix.length + 3 // '%2C'.length == 3
 895+ : modules[i].length + 3; // '%7C'.length == 3
875896
876897 // If the request would become too long, create a new one,
877898 // but don't create empty requests
@@ -881,7 +902,7 @@
882903 moduleMap = {};
883904 l = currReqBaseLength + 9;
884905 }
885 - if ( !( prefix in moduleMap ) ) {
 906+ if ( moduleMap[prefix] === undefined ) {
886907 moduleMap[prefix] = [];
887908 }
888909 moduleMap[prefix].push( suffix );
@@ -904,9 +925,10 @@
905926 * @return {Boolean}
906927 */
907928 this.addSource = function( id, props ) {
 929+ var source;
908930 // Allow multiple additions
909931 if ( typeof id === 'object' ) {
910 - for ( var source in id ) {
 932+ for ( source in id ) {
911933 mw.loader.addSource( source, id[source] );
912934 }
913935 return true;
@@ -933,9 +955,10 @@
934956 * @param source {String}: Name of the source. Defaults to local.
935957 */
936958 this.register = function( module, version, dependencies, group, source ) {
 959+ var m;
937960 // Allow multiple registration
938961 if ( typeof module === 'object' ) {
939 - for ( var m = 0; m < module.length; m++ ) {
 962+ for ( m = 0; m < module.length; m += 1 ) {
940963 // module is an array of module names
941964 if ( typeof module[m] === 'string' ) {
942965 mw.loader.register( module[m] );
@@ -1027,10 +1050,10 @@
10281051 /**
10291052 * Executes a function as soon as one or more required modules are ready
10301053 *
1031 - * @param dependencies string or array of strings of modules names the callback
1032 - * dependencies to be ready before executing
1033 - * @param ready function callback to execute when all dependencies are ready (optional)
1034 - * @param error function callback to execute when if dependencies have a errors (optional)
 1054+ * @param dependencies {String|Array} Module name or array of modules names the callback
 1055+ * dependends on to be ready before executing
 1056+ * @param ready {Function} callback to execute when all dependencies are ready (optional)
 1057+ * @param error {Function} callback to execute when if dependencies have a errors (optional)
10351058 */
10361059 this.using = function( dependencies, ready, error ) {
10371060 var tod = typeof dependencies;
@@ -1066,9 +1089,9 @@
10671090 /**
10681091 * Loads an external script or one or more modules for future use
10691092 *
1070 - * @param modules mixed either the name of a module, array of modules,
 1093+ * @param modules {mixed} Either the name of a module, array of modules,
10711094 * or a URL of an external script or style
1072 - * @param type string mime-type to use if calling with a URL of an
 1095+ * @param type {String} mime-type to use if calling with a URL of an
10731096 * external script or style; acceptable values are "text/css" and
10741097 * "text/javascript"; if no type is provided, text/javascript is assumed.
10751098 */
@@ -1118,17 +1141,18 @@
11191142 /**
11201143 * Changes the state of a module
11211144 *
1122 - * @param module string module name or object of module name/state pairs
1123 - * @param state string state name
 1145+ * @param module {String|Object} module name or object of module name/state pairs
 1146+ * @param state {String} state name
11241147 */
11251148 this.state = function( module, state ) {
 1149+ var m;
11261150 if ( typeof module === 'object' ) {
1127 - for ( var m in module ) {
 1151+ for ( m in module ) {
11281152 mw.loader.state( m, module[m] );
11291153 }
11301154 return;
11311155 }
1132 - if ( !( module in registry ) ) {
 1156+ if ( registry[module] === undefined ) {
11331157 mw.loader.register( module );
11341158 }
11351159 registry[module].state = state;
@@ -1140,14 +1164,14 @@
11411165 * @param module string name of module to get version for
11421166 */
11431167 this.getVersion = function( module ) {
1144 - if ( module in registry && 'version' in registry[module] ) {
 1168+ if ( registry[module] !== undefined && registry[module].version !== undefined ) {
11451169 return formatVersionNumber( registry[module].version );
11461170 }
11471171 return null;
11481172 };
11491173
11501174 /**
1151 - * @deprecated use mw.loader.getVersion() instead
 1175+ * @deprecated since 1.18 use mw.loader.getVersion() instead
11521176 */
11531177 this.version = function() {
11541178 return mw.loader.getVersion.apply( mw.loader, arguments );
@@ -1159,7 +1183,7 @@
11601184 * @param module string name of module to get state for
11611185 */
11621186 this.getState = function( module ) {
1163 - if ( module in registry && 'state' in registry[module] ) {
 1187+ if ( registry[module] !== undefined && registry[module].state !== undefined ) {
11641188 return registry[module].state;
11651189 }
11661190 return null;
@@ -1171,10 +1195,9 @@
11721196 * @return {Array}
11731197 */
11741198 this.getModuleNames = function() {
1175 - var names = $.map( registry, function( i, key ) {
 1199+ return $.map( registry, function( i, key ) {
11761200 return key;
11771201 } );
1178 - return names;
11791202 };
11801203
11811204 /**
@@ -1246,8 +1269,9 @@
12471270 * Returns <div><img src="&lt;"/></div>
12481271 */
12491272 this.element = function( name, attrs, contents ) {
1250 - var v, s = '<' + name;
1251 - for ( var attrName in attrs ) {
 1273+ var v, attrName, s = '<' + name;
 1274+
 1275+ for ( attrName in attrs ) {
12521276 v = attrs[attrName];
12531277 // Convert name=true, to name=name
12541278 if ( v === true ) {
@@ -1256,7 +1280,7 @@
12571281 } else if ( v === false ) {
12581282 continue;
12591283 }
1260 - s += ' ' + attrName + '="' + this.escape( '' + v ) + '"';
 1284+ s += ' ' + attrName + '="' + this.escape( String( v ) ) + '"';
12611285 }
12621286 if ( contents === undefined || contents === null ) {
12631287 // Self close tag
@@ -1273,7 +1297,7 @@
12741298 case 'number':
12751299 case 'boolean':
12761300 // Convert to string
1277 - s += '' + contents;
 1301+ s += String( contents );
12781302 break;
12791303 default:
12801304 if ( contents instanceof this.Raw ) {
@@ -1303,8 +1327,11 @@
13041328 // Alias $j to jQuery for backwards compatibility
13051329 window.$j = jQuery;
13061330
 1331+// Attach to window and globally alias
 1332+window.mw = window.mediaWiki = mw;
 1333+
13071334 // Auto-register from pre-loaded startup scripts
13081335 if ( typeof startUp !== 'undefined' && jQuery.isFunction( startUp ) ) {
13091336 startUp();
1310 - delete startUp;
 1337+ startUp = undefined;
13111338 }

Sign-offs

UserFlagDate
Nikerabbitinspected07:12, 28 December 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r107405[mediawiki.js] use simple IIFE closure with object literal...krinkle18:48, 27 December 2011
r107407[mediawiki.js] function order...krinkle18:51, 27 December 2011

Status & tagging log