From 238ed13911af4d9e001cc5bc69d14be99fbfc56e Mon Sep 17 00:00:00 2001 From: Krinkle Date: Tue, 17 May 2011 17:19:12 +0000 Subject: [PATCH] * Moving jQuery extend into its own module (jquery.mwPrototypes) (bug 27149; Introduced in r) --- resources/Resources.php | 4 + resources/jquery/jquery.mwPrototypes.js | 120 ++++++++++++++++++ resources/mediawiki/mediawiki.js | 120 ------------------ resources/test/index.html | 2 + .../test/unit/jquery/jquery.mwPrototypes.js | 58 +++++++++ resources/test/unit/mediawiki/mediawiki.js | 49 ------- 6 files changed, 184 insertions(+), 169 deletions(-) create mode 100644 resources/jquery/jquery.mwPrototypes.js create mode 100644 resources/test/unit/jquery/jquery.mwPrototypes.js diff --git a/resources/Resources.php b/resources/Resources.php index 28b12bb53a..3e959319ee 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -132,6 +132,9 @@ return array( 'styles' => 'resources/jquery/jquery.makeCollapsible.css', 'messages' => array( 'collapsible-expand', 'collapsible-collapse' ), ), + 'jquery.mwPrototypes' => array( + 'scripts' => 'resources/jquery/jquery.mwPrototypes.js', + ), 'jquery.qunit' => array( 'scripts' => 'resources/jquery/jquery.qunit.js', 'styles' => 'resources/jquery/jquery.qunit.css', @@ -448,6 +451,7 @@ return array( 'jquery.cookie', 'jquery.messageBox', 'jquery.makeCollapsible', + 'jquery.mwPrototypes', 'jquery.placeholder', ), ), diff --git a/resources/jquery/jquery.mwPrototypes.js b/resources/jquery/jquery.mwPrototypes.js new file mode 100644 index 0000000000..e26a6be644 --- /dev/null +++ b/resources/jquery/jquery.mwPrototypes.js @@ -0,0 +1,120 @@ +/* + * JavaScript backwards-compatibility alternatives and other convenience functions + */ + +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 ) { + var key; + if ( v === "" || v === 0 || v === "0" || v === null + || v === false || typeof 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 ( key in v ) { + return false; + } + return true; + } + return false; + }, + compareArray : function( arrThis, arrAgainst ) { + if ( arrThis.length != arrAgainst.length ) { + return false; + } + for ( var i = 0; i < arrThis.length; i++ ) { + if ( arrThis[i] instanceof Array ) { + 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 ) { + + // 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; + } + } + // 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; + } + return true; + } +}); + diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index f0a725b448..81a6f098e8 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -1,123 +1,3 @@ -/* - * JavaScript backwards-compatibility alternatives and other convenience functions - */ - -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 ) { - var key; - if ( v === "" || v === 0 || v === "0" || v === null - || v === false || typeof 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 ( key in v ) { - return false; - } - return true; - } - return false; - }, - compareArray : function( arrThis, arrAgainst ) { - if ( arrThis.length != arrAgainst.length ) { - return false; - } - for ( var i = 0; i < arrThis.length; i++ ) { - if ( arrThis[i] instanceof Array ) { - 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 ) { - - // 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; - } - } - // 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; - } - return true; - } -}); - /* * Core MediaWiki JavaScript Library */ diff --git a/resources/test/index.html b/resources/test/index.html index 2bf5d36748..8ce82fb850 100644 --- a/resources/test/index.html +++ b/resources/test/index.html @@ -22,6 +22,7 @@ + @@ -39,6 +40,7 @@ + diff --git a/resources/test/unit/jquery/jquery.mwPrototypes.js b/resources/test/unit/jquery/jquery.mwPrototypes.js new file mode 100644 index 0000000000..20ff8f1791 --- /dev/null +++ b/resources/test/unit/jquery/jquery.mwPrototypes.js @@ -0,0 +1,58 @@ +module( 'jquery.mwPrototypes' ); + +test( 'String functions', function(){ + + equal( $j.trimLeft( ' foo bar ' ), 'foo bar ', 'trimLeft' ); + equal( $j.trimRight( ' foo bar ' ), ' foo bar', 'trimRight' ); + equal( $j.ucFirst( 'foo'), 'Foo', 'ucFirst' ); + + equal( $j.escapeRE( '