r68447 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68446‎ | r68447 | r68448 >
Date:10:59, 23 June 2010
Author:catrope
Status:deferred
Tags:
Comment:
resourceloader: Some fixes. Calling importScriptURI('/path/to/load.php?modules=core'); in Common.js works now, even in IE7.
* Extend window.mw rather than overriding it. Temporary fix so the disappearance of mw.usability doesn't make stuff blow up.
* Implement indexOf() for arrays on browsers that don't have it, inspired by bug 24083
* Don't export loop variables to the global scope (or closure scope or wherever they end up)
* Use for ( var k = 0; k < arr.length; k++ ) to loop through arrays rather than for ( var k in arr ) . The latter also loops over any functions added to Array.prototype like some libraries do (not jQuery); we do it too for the indexOf() fix mentioned above.
* Use splice() the right way and adjust the loop counter so we don't skip over the next item. The for..in loop was broken here as well because not all browsers behave consistently when keys are added/removed from inside such a loop, see bug 20668 comment 10.
* Move the document ready hook all the way down to the end. When this script is injected later and the document is ready already, the callback is executed immediately and fails because work() hasn't been defined yet.
* Fix undefined variable warnings in ResourceLoader.php
Modified paths:
  • /branches/resourceloader/phase3/includes/ResourceLoader.php (modified) (history)
  • /branches/resourceloader/phase3/resources/core/mw.js (modified) (history)

Diff [purge]

Index: branches/resourceloader/phase3/includes/ResourceLoader.php
@@ -113,8 +113,6 @@
114114 // Because these are keyed by module, in the case that more than one module asked for the same script only the
115115 // first will end up being registered - the client loader can't handle multiple modules per implementation yet,
116116 // so this is fine, but causes silent failure it strange abusive cases
117 - $this->scripts = array_unique( $this->scripts );
118 - $this->styles = array_unique( $this->styles );
119117 $this->loadedModules = array_unique( $this->loadedModules );
120118 $retval = '';
121119
Index: branches/resourceloader/phase3/resources/core/mw.js
@@ -2,7 +2,21 @@
33 * Core MediaWiki JavaScript Library
44 */
55
6 -window.mw = {
 6+// Make calling .indexOf() on an array work on older browsers
 7+if ( typeof Array.prototype.indexOf === 'undefined' ) {
 8+ Array.prototype.indexOf = function( needle ) {
 9+ for ( var i = 0; i < this.length; i++ ) {
 10+ if ( this[i] === needle ) {
 11+ return i;
 12+ }
 13+ }
 14+ return -1;
 15+ };
 16+}
 17+
 18+// Extend window.mw rather than overriding it. This is a temporary fix designed to prevent
 19+// stuff from blowing up when usability.js (setting mw.usability) is run before this file.
 20+window.mw = $.extend( typeof window.mw === 'undefined' ? {} : window.mw, {
721
822 /* Public Members */
923
@@ -67,7 +81,7 @@
6882 this.buildQueryString = function( parameters ) {
6983 if ( typeof parameters === 'object' ) {
7084 var parts = [];
71 - for ( p in parameters ) {
 85+ for ( var p in parameters ) {
7286 parts[parts.length] = that.urlencode( p ) + '=' + that.urlencode( parameters[p] );
7387 }
7488 return parts.join( '&' );
@@ -93,7 +107,7 @@
94108 */
95109 this.set = function( keys, value ) {
96110 if ( typeof keys === 'object' ) {
97 - for ( key in keys ) {
 111+ for ( var key in keys ) {
98112 values[key] = keys[key];
99113 }
100114 } else if ( typeof keys === 'string' && typeof value !== 'undefined' ) {
@@ -106,7 +120,7 @@
107121 this.get = function( keys, fallback ) {
108122 if ( typeof keys === 'object' ) {
109123 var result = {};
110 - for ( k in keys ) {
 124+ for ( var k = 0; k < keys.length; k++ ) {
111125 if ( typeof values[keys[k]] !== 'undefined' ) {
112126 result[keys[k]] = values[keys[k]];
113127 }
@@ -134,7 +148,7 @@
135149
136150 this.set = function( keys, value ) {
137151 if ( typeof keys === 'object' ) {
138 - for ( key in keys ) {
 152+ for ( var key in keys ) {
139153 messages[key] = keys[key];
140154 }
141155 } else if ( typeof keys === 'string' && typeof value !== 'undefined' ) {
@@ -181,13 +195,6 @@
182196 // True after document ready occurs
183197 var ready = false;
184198
185 - /* Event Bindings */
186 -
187 - $( document ).ready( function() {
188 - ready = true;
189 - that.work();
190 - } );
191 -
192199 /* Private Functions */
193200
194201 /**
@@ -196,7 +203,7 @@
197204 function pending( requirements ) {
198205 // Collect the names of pending modules
199206 var list = [];
200 - for ( r in requirements ) {
 207+ for ( var r = 0; r < requirements.length; r++ ) {
201208 if (
202209 typeof registry[requirements[r]] === 'undefined' ||
203210 typeof registry[requirements[r]].state === 'undefined' ||
@@ -235,8 +242,8 @@
236243 * Processes the queue, loading and executing when things when ready.
237244 */
238245 this.work = function() {
239 - for ( q in queue ) {
240 - for ( p in queue[q].pending ) {
 246+ for ( var q = 0; q < queue.length; q++ ) {
 247+ for ( var p = 0; p < queue[q].pending.length; p++ ) {
241248 var requirement = queue[q].pending[p];
242249 // If it's not in the registry yet, we're certainly not ready to execute
243250 if (
@@ -259,13 +266,15 @@
260267 break;
261268 case 'ready':
262269 // This doesn't belong in the queue item's pending list
263 - queue[q].pending.splice( p );
 270+ queue[q].pending.splice( p, 1 );
 271+ // Correct the array index
 272+ p--;
264273 break;
265274 }
266275 }
267276 }
268277 // If all pending requirements have been satisfied, we're ready to execute the callback
269 - if ( typeof queue[q].pending.length == 0 ) {
 278+ if ( queue[q].pending.length == 0 ) {
270279 queue[q].callback();
271280 // Clean up the queue
272281 queue.splice( q, 1 );
@@ -381,5 +390,12 @@
382391 that.work();
383392 }
384393 };
 394+
 395+ /* Event Bindings */
 396+
 397+ $( document ).ready( function() {
 398+ ready = true;
 399+ that.work();
 400+ } );
385401 } )()
386 -};
 402+} );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r56588EditToolbar: (bug 20668) Fix infinite loop and OOM death on IE8catrope09:12, 18 September 2009

Status & tagging log