From 7e8f5f1e8f6b0de969bb5da76c5879af5bd43b75 Mon Sep 17 00:00:00 2001 From: Daniel Friesen Date: Fri, 12 Aug 2011 09:27:16 +0000 Subject: [PATCH] Fix usage of the jQuery global in a few spots. - jQuery changed to $ in some files because there is a closure that creates a locally scoped $, but the jQuery var is globally scoped, meaning using jQuery instead of $ inside that closure could result in interacting with a different instance of jQuery than the uses of $ in that same closure. - In mwExtension wrap the code inside a closure which it is missing. Also take this chance to fix the whitespace style `fn( arg )` instead of `fn(arg)` on the isArray I added. This is partially a followup to r94331. Note: The jquery plugins inside the jquery/ folder look fine for use of jQuery within closures, except for mockjax. --- resources/jquery/jquery.makeCollapsible.js | 4 +- resources/jquery/jquery.mwExtension.js | 196 +++++++++++---------- resources/mediawiki/mediawiki.js | 2 +- resources/mediawiki/mediawiki.util.js | 2 +- 4 files changed, 103 insertions(+), 101 deletions(-) diff --git a/resources/jquery/jquery.makeCollapsible.js b/resources/jquery/jquery.makeCollapsible.js index a84f405cee..d5b76382ee 100644 --- a/resources/jquery/jquery.makeCollapsible.js +++ b/resources/jquery/jquery.makeCollapsible.js @@ -38,7 +38,7 @@ $.fn.makeCollapsible = function() { if ( typeof $defaultToggle == 'undefined' ) { $defaultToggle = null; } - if ( $defaultToggle !== null && !($defaultToggle instanceof jQuery) ) { + if ( $defaultToggle !== null && !($defaultToggle instanceof $) ) { // is optional (may be undefined), but if defined it must be an instance of jQuery. // If it's not, abort right away. // After this $defaultToggle is either null or a valid jQuery instance. @@ -336,4 +336,4 @@ $.fn.makeCollapsible = function() { } } ); }; -} )( jQuery, mediaWiki ); \ No newline at end of file +} )( jQuery, mediaWiki ); diff --git a/resources/jquery/jquery.mwExtension.js b/resources/jquery/jquery.mwExtension.js index 39273c1d99..7b37bb7d09 100644 --- a/resources/jquery/jquery.mwExtension.js +++ b/resources/jquery/jquery.mwExtension.js @@ -1,119 +1,121 @@ /* * JavaScript backwards-compatibility alternatives and other convenience functions */ +( function( $ ) { -jQuery.extend({ - trimLeft: function( str ) { - return str === null ? '' : str.toString().replace( /^\s+/, '' ); - }, - trimRight: function( str ) { - return str === null ? - '' : str.toString().replace( /\s+$/, '' ); - }, - ucFirst: function( str ) { - return str.substr( 0, 1 ).toUpperCase() + str.substr( 1 ); - }, - escapeRE: function( str ) { - return str.replace ( /([\\{}()|.?*+\-^$\[\]])/g, "\\$1" ); - }, - isDomElement: function( el ) { - return !!el && !!el.nodeType; - }, - isEmpty: function( v ) { - if ( v === '' || v === 0 || v === '0' || v === null - || v === false || v === undefined ) - { - return true; - } - // the for-loop could potentially contain prototypes - // to avoid that we check it's length first - if ( v.length === 0 ) { - return true; - } - if ( typeof v === 'object' ) { - for ( var key in v ) { - return false; + $.extend({ + trimLeft: function( str ) { + return str === null ? '' : str.toString().replace( /^\s+/, '' ); + }, + trimRight: function( str ) { + return str === null ? + '' : str.toString().replace( /\s+$/, '' ); + }, + ucFirst: function( str ) { + return str.substr( 0, 1 ).toUpperCase() + str.substr( 1 ); + }, + escapeRE: function( str ) { + return str.replace ( /([\\{}()|.?*+\-^$\[\]])/g, "\\$1" ); + }, + isDomElement: function( el ) { + return !!el && !!el.nodeType; + }, + isEmpty: function( v ) { + if ( v === '' || v === 0 || v === '0' || v === null + || v === false || v === undefined ) + { + return true; } - return true; - } - return false; - }, - compareArray: function( arrThis, arrAgainst ) { - if ( arrThis.length != arrAgainst.length ) { - return false; - } - for ( var i = 0; i < arrThis.length; i++ ) { - if ( $.isArray(arrThis[i]) ) { - if ( !$.compareArray( arrThis[i], arrAgainst[i] ) ) { + // the for-loop could potentially contain prototypes + // to avoid that we check it's length first + if ( v.length === 0 ) { + return true; + } + if ( typeof v === 'object' ) { + for ( var key in v ) { return false; } - } else if ( arrThis[i] !== arrAgainst[i] ) { + return true; + } + return false; + }, + compareArray: function( arrThis, arrAgainst ) { + if ( arrThis.length != arrAgainst.length ) { return false; } - } - return true; - }, - compareObject: function( objectA, objectB ) { + for ( var i = 0; i < arrThis.length; i++ ) { + if ( $.isArray( arrThis[i] ) ) { + if ( !$.compareArray( arrThis[i], arrAgainst[i] ) ) { + return false; + } + } else if ( arrThis[i] !== arrAgainst[i] ) { + return false; + } + } + return true; + }, + compareObject: function( objectA, objectB ) { - // Do a simple check if the types match - if ( typeof objectA == typeof objectB ) { + // Do a simple check if the types match + if ( typeof objectA == typeof objectB ) { - // Only loop over the contents if it really is an object - if ( typeof objectA == 'object' ) { - // If they are aliases of the same object (ie. mw and mediaWiki) return now - if ( objectA === objectB ) { - return true; - } else { - var prop; - // Iterate over each property - for ( prop in objectA ) { - // Check if this property is also present in the other object - if ( prop in objectB ) { - // Compare the types of the properties - var type = typeof objectA[prop]; - if ( type == typeof objectB[prop] ) { - // Recursively check objects inside this one - switch ( type ) { - case 'object' : - if ( !$.compareObject( objectA[prop], objectB[prop] ) ) { - return false; - } - break; - case 'function' : - // Functions need to be strings to compare them properly - if ( objectA[prop].toString() !== objectB[prop].toString() ) { - return false; - } - break; - default: - // Strings, numbers - if ( objectA[prop] !== objectB[prop] ) { - return false; - } - break; + // Only loop over the contents if it really is an object + if ( typeof objectA == 'object' ) { + // If they are aliases of the same object (ie. mw and mediaWiki) return now + if ( objectA === objectB ) { + return true; + } else { + var prop; + // Iterate over each property + for ( prop in objectA ) { + // Check if this property is also present in the other object + if ( prop in objectB ) { + // Compare the types of the properties + var type = typeof objectA[prop]; + if ( type == typeof objectB[prop] ) { + // Recursively check objects inside this one + switch ( type ) { + case 'object' : + if ( !$.compareObject( objectA[prop], objectB[prop] ) ) { + return false; + } + break; + case 'function' : + // Functions need to be strings to compare them properly + if ( objectA[prop].toString() !== objectB[prop].toString() ) { + return false; + } + break; + default: + // Strings, numbers + if ( objectA[prop] !== objectB[prop] ) { + return false; + } + break; + } + } else { + return false; } } else { return false; } - } else { - return false; } - } - // Check for properties in B but not in A - // This is about 15% faster (tested in Safari 5 and Firefox 3.6) - // ...than incrementing a count variable in the above and below loops - // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results - for ( prop in objectB ) { - if ( !( prop in objectA ) ) { - return false; + // Check for properties in B but not in A + // This is about 15% faster (tested in Safari 5 and Firefox 3.6) + // ...than incrementing a count variable in the above and below loops + // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results + for ( prop in objectB ) { + if ( !( prop in objectA ) ) { + return false; + } } } } + } else { + return false; } - } else { - return false; + return true; } - return true; - } -}); + }); +} )( jQuery ); diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 2ae11e84ef..6f9e789719 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -558,7 +558,7 @@ window.mw = window.mediaWiki = new ( function( $ ) { } ); } } else if ( $.isFunction( script ) ) { - script( jQuery ); + script( $ ); markModuleReady(); } } catch ( e ) { diff --git a/resources/mediawiki/mediawiki.util.js b/resources/mediawiki/mediawiki.util.js index a720f58333..e6bb9fb6ff 100644 --- a/resources/mediawiki/mediawiki.util.js +++ b/resources/mediawiki/mediawiki.util.js @@ -267,7 +267,7 @@ return; - } else if ( nodeList instanceof jQuery ) { + } else if ( nodeList instanceof $ ) { $nodes = nodeList; } else { $nodes = $( nodeList ); -- 2.20.1