mediawiki.util.test.js: IE7 doesn't expose the CSSStyleSheet publicly (like it doesn...
[mediawiki.git] / resources / jquery / jquery.mwPrototypes.js
blobe26a6be6446bf2c97d815a7eb82803b3f58e89aa
1 /*
2  * JavaScript backwards-compatibility alternatives and other convenience functions
3  */
5 jQuery.extend({
6         trimLeft : function( str ) {
7                 return str === null ? '' : str.toString().replace( /^\s+/, '' );
8         },
9         trimRight : function( str ) {
10                 return str === null ?
11                                 '' : str.toString().replace( /\s+$/, '' );
12         },
13         ucFirst : function( str ) {
14                 return str.substr( 0, 1 ).toUpperCase() + str.substr( 1 );
15         },
16         escapeRE : function( str ) {
17                 return str.replace ( /([\\{}()|.?*+\-^$\[\]])/g, "\\$1" );
18         },
19         isDomElement : function( el ) {
20                 return !!el && !!el.nodeType;
21         },
22         isEmpty : function( v ) {
23                 var key;
24                 if ( v === "" || v === 0 || v === "0" || v === null
25                         || v === false || typeof v === 'undefined' )
26                 {
27                         return true;
28                 }
29                 // the for-loop could potentially contain prototypes
30                 // to avoid that we check it's length first
31                 if ( v.length === 0 ) {
32                         return true;
33                 }
34                 if ( typeof v === 'object' ) {
35                         for ( key in v ) {
36                                 return false;
37                         }
38                         return true;
39                 }
40                 return false;
41         },
42         compareArray : function( arrThis, arrAgainst ) {
43                 if ( arrThis.length != arrAgainst.length ) {
44                         return false;
45                 }
46                 for ( var i = 0; i < arrThis.length; i++ ) {
47                         if ( arrThis[i] instanceof Array ) {
48                                 if ( !$.compareArray( arrThis[i], arrAgainst[i] ) ) {
49                                         return false;
50                                 }
51                         } else if ( arrThis[i] !== arrAgainst[i] ) {
52                                 return false;
53                         }
54                 }
55                 return true;
56         },
57         compareObject : function( objectA, objectB ) {
59                 // Do a simple check if the types match
60                 if ( typeof objectA == typeof objectB ) {
62                         // Only loop over the contents if it really is an object
63                         if ( typeof objectA == 'object' ) {
64                                 // If they are aliases of the same object (ie. mw and mediaWiki) return now
65                                 if ( objectA === objectB ) {
66                                         return true;
67                                 } else {
68                                         var prop;
69                                         // Iterate over each property
70                                         for ( prop in objectA ) {
71                                                 // Check if this property is also present in the other object
72                                                 if ( prop in objectB ) {
73                                                         // Compare the types of the properties
74                                                         var type = typeof objectA[prop];
75                                                         if ( type == typeof objectB[prop] ) {
76                                                                 // Recursively check objects inside this one
77                                                                 switch ( type ) {
78                                                                         case 'object' :
79                                                                                 if ( !$.compareObject( objectA[prop], objectB[prop] ) ) {
80                                                                                         return false;
81                                                                                 }
82                                                                                 break;
83                                                                         case 'function' :
84                                                                                 // Functions need to be strings to compare them properly
85                                                                                 if ( objectA[prop].toString() !== objectB[prop].toString() ) {
86                                                                                         return false;
87                                                                                 }
88                                                                                 break;
89                                                                         default:
90                                                                                 // Strings, numbers
91                                                                                 if ( objectA[prop] !== objectB[prop] ) {
92                                                                                         return false;
93                                                                                 }
94                                                                                 break;
95                                                                 }
96                                                         } else {
97                                                                 return false;
98                                                         }
99                                                 } else {
100                                                         return false;
101                                                 }
102                                         }
103                                         // Check for properties in B but not in A
104                                         // This is about 15% faster (tested in Safari 5 and Firefox 3.6)
105                                         // ...than incrementing a count variable in the above and below loops
106                                         // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results
107                                         for ( prop in objectB ) {
108                                                 if ( !( prop in objectA ) ) {
109                                                         return false;
110                                                 }
111                                         }
112                                 }
113                         }
114                 } else {
115                         return false;
116                 }
117                 return true;
118         }