* Moving jQuery extend into its own module (jquery.mwPrototypes) (bug 27149; Introduc...
authorKrinkle <krinkle@users.mediawiki.org>
Tue, 17 May 2011 17:19:12 +0000 (17:19 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Tue, 17 May 2011 17:19:12 +0000 (17:19 +0000)
resources/Resources.php
resources/jquery/jquery.mwPrototypes.js [new file with mode: 0644]
resources/mediawiki/mediawiki.js
resources/test/index.html
resources/test/unit/jquery/jquery.mwPrototypes.js [new file with mode: 0644]
resources/test/unit/mediawiki/mediawiki.js

index 28b12bb..3e95931 100644 (file)
@@ -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 (file)
index 0000000..e26a6be
--- /dev/null
@@ -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;
+       }
+});
+
index f0a725b..81a6f09 100644 (file)
@@ -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
  */
index 2bf5d36..8ce82fb 100644 (file)
@@ -22,6 +22,7 @@
        <script src="../jquery/jquery.cookie.js"></script>
        <script src="../jquery/jquery.messageBox.js"></script>
        <script src="../jquery/jquery.makeCollapsible.js"></script>
+       <script src="../jquery/jquery.mwPrototypes.js"></script>
        <script src="../jquery/jquery.placeholder.js"></script>
        <script src="../mediawiki.util/mediawiki.util.js"></script>
 
@@ -39,6 +40,7 @@
        <!-- Load test suitss -->
        <script src="unit/mediawiki/mediawiki.js"></script>
        <script src="unit/mediawiki/mediawiki.user.js"></script>
+       <script src="unit/jquery/jquery.mwPrototypes.js"></script>
        <script src="unit/mediawiki.util/mediawiki.util.js"></script>
        <script src="unit/jquery/jquery.colorUtil.js"></script>
 
diff --git a/resources/test/unit/jquery/jquery.mwPrototypes.js b/resources/test/unit/jquery/jquery.mwPrototypes.js
new file mode 100644 (file)
index 0000000..20ff8f1
--- /dev/null
@@ -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( '<!-- ([{+mW+}]) $^|?>' ),
+        '<!\\-\\- \\(\\[\\{\\+mW\\+\\}\\]\\) \\$\\^\\|\\?>', 'escapeRE - Escape specials' );
+       equal( $j.escapeRE( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ),
+        'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'escapeRE - Leave uppercase alone' );
+       equal( $j.escapeRE( 'abcdefghijklmnopqrstuvwxyz' ),
+        'abcdefghijklmnopqrstuvwxyz', 'escapeRE - Leave lowercase alone' );
+       equal( $j.escapeRE( '0123456789' ), '0123456789', 'escapeRE - Leave numbers alone' );
+
+});
+
+test( 'Is functions', function(){
+
+       deepEqual( $j.isDomElement( document.getElementById( 'qunit-header' ) ), true,
+        'isDomElement: #qunit-header Node' );
+       deepEqual( $j.isDomElement( document.getElementById( 'random-name' ) ), false,
+        'isDomElement: #random-name (null)' );
+       deepEqual( $j.isDomElement( document.getElementsByTagName( 'div' ) ), false,
+        'isDomElement: getElementsByTagName Array' );
+       deepEqual( $j.isDomElement( document.getElementsByTagName( 'div' )[0] ), true,
+        'isDomElement: getElementsByTagName(..)[0] Node' );
+       deepEqual( $j.isDomElement( $j( 'div' ) ), false,
+        'isDomElement: jQuery object' );
+       deepEqual( $j.isDomElement( $j( 'div' ).get(0) ), true,
+        'isDomElement: jQuery object > Get node' );
+       deepEqual( $j.isDomElement( document.createElement( 'div' ) ), true,
+        'isDomElement: createElement' );
+       deepEqual( $j.isDomElement( { foo: 1 } ), false,
+        'isDomElement: Object' );
+
+       equal( $j.isEmpty( 'string' ), false, 'isEmptry: "string"' );
+       equal( $j.isEmpty( '0' ), true, 'isEmptry: "0"' );
+       equal( $j.isEmpty( [] ), true, 'isEmptry: []' );
+       equal( $j.isEmpty( {} ), true, 'isEmptry: {}' );
+       // Documented behaviour
+       equal( $j.isEmpty( { length: 0 } ), true, 'isEmptry: { length: 0 }' );
+
+});
+
+test( 'Comparison functions', function(){
+
+       ok( $j.compareArray( [0, 'a', [], [2, 'b'] ], [0, "a", [], [2, "b"] ] ),
+        'compareArray: Two deep arrays that are excactly the same' );
+       ok( !$j.compareArray( [1], [2] ), 'compareArray: Two different arrays (false)' );
+
+       ok( $j.compareObject( {}, {} ), 'compareObject: Two empty objects' );
+       ok( $j.compareObject( { foo: 1 }, { foo: 1 } ), 'compareObject: Two the same objects' );
+       ok( !$j.compareObject( { bar: true }, { baz: false } ),
+        'compareObject: Two different objects (false)' );
+       
+});
\ No newline at end of file
index d7e84bd..0423887 100644 (file)
@@ -12,55 +12,6 @@ test( '-- Initial check', function(){
 
 });
 
-test( 'jQuery.extend', 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( '<!-- ([{+mW+}]) $^|?>' ),
-        '<!\\-\\- \\(\\[\\{\\+mW\\+\\}\\]\\) \\$\\^\\|\\?>', 'escapeRE - Escape specials' );
-       equal( $j.escapeRE( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ),
-        'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'escapeRE - Leave uppercase alone' );
-       equal( $j.escapeRE( 'abcdefghijklmnopqrstuvwxyz' ),
-        'abcdefghijklmnopqrstuvwxyz', 'escapeRE - Leave lowercase alone' );
-       equal( $j.escapeRE( '0123456789' ), '0123456789', 'escapeRE - Leave numbers alone' );
-
-       deepEqual( $j.isDomElement( document.getElementById( 'qunit-header' ) ), true,
-        'isDomElement: #qunit-header Node' );
-       deepEqual( $j.isDomElement( document.getElementById( 'random-name' ) ), false,
-        'isDomElement: #random-name (null)' );
-       deepEqual( $j.isDomElement( document.getElementsByTagName( 'div' ) ), false,
-        'isDomElement: getElementsByTagName Array' );
-       deepEqual( $j.isDomElement( document.getElementsByTagName( 'div' )[0] ), true,
-        'isDomElement: getElementsByTagName(..)[0] Node' );
-       deepEqual( $j.isDomElement( $j( 'div' ) ), false,
-        'isDomElement: jQuery object' );
-       deepEqual( $j.isDomElement( $j( 'div' ).get(0) ), true,
-        'isDomElement: jQuery object > Get node' );
-       deepEqual( $j.isDomElement( document.createElement( 'div' ) ), true,
-        'isDomElement: createElement' );
-       deepEqual( $j.isDomElement( { foo: 1 } ), false,
-        'isDomElement: Object' );
-
-       equal( $j.isEmpty( 'string' ), false, 'isEmptry: "string"' );
-       equal( $j.isEmpty( '0' ), true, 'isEmptry: "0"' );
-       equal( $j.isEmpty( [] ), true, 'isEmptry: []' );
-       equal( $j.isEmpty( {} ), true, 'isEmptry: {}' );
-       // Documented behaviour
-       equal( $j.isEmpty( { length: 0 } ), true, 'isEmptry: { length: 0 }' );
-
-       ok( $j.compareArray( [0, 'a', [], [2, 'b'] ], [0, "a", [], [2, "b"] ] ),
-        'compareArray: Two the same deep arrays' );
-       ok( !$j.compareArray( [1], [2] ), 'compareArray: Two different arrays' );
-
-       ok( $j.compareObject( {}, {} ), 'compareObject: Two empty objects' );
-       ok( $j.compareObject( { foo: 1 }, { foo: 1 } ), 'compareObject: Two the same objects' );
-       ok( !$j.compareObject( { bar: true }, { baz: false } ),
-        'compareObject: Two different objects' );
-       
-});
-
 test( 'mw.Map / mw.config', function(){
 
        ok( mw.config instanceof mw.Map, 'mw.config instance of mw.Map' );