Index: trunk/phase3/resources/mediawiki/mediawiki.js |
— | — | @@ -50,6 +50,67 @@ |
51 | 51 | } |
52 | 52 | } |
53 | 53 | return true; |
| 54 | + }, |
| 55 | + compareObject : function( objectA, objectB ) { |
| 56 | + |
| 57 | + // Do a simple check if the types match |
| 58 | + if ( typeof( objectA ) == typeof( objectB ) ) { |
| 59 | + |
| 60 | + // Only loop over the contents if it really is an object |
| 61 | + if ( typeof( objectA ) == 'object' ) { |
| 62 | + // If they are aliases of the same object (ie. mw and mediaWiki) return now |
| 63 | + if ( objectA === objectB ) { |
| 64 | + return true; |
| 65 | + } else { |
| 66 | + // Iterate over each property |
| 67 | + for ( var prop in objectA ) { |
| 68 | + // Check if this property is also present in the other object |
| 69 | + if ( prop in objectB ) { |
| 70 | + // Compare the types of the properties |
| 71 | + var type = typeof( objectA[prop] ); |
| 72 | + if ( type == typeof( objectB[prop] ) ) { |
| 73 | + // Recursively check objects inside this one |
| 74 | + switch ( type ) { |
| 75 | + case 'object' : |
| 76 | + if ( !$.compareObject( objectA[prop], objectB[prop] ) ) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + break; |
| 80 | + case 'function' : |
| 81 | + // Functions need to be strings to compare them properly |
| 82 | + if ( objectA[prop].toString() !== objectB[prop].toString() ) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + break; |
| 86 | + default: |
| 87 | + // Strings, numbers |
| 88 | + if ( objectA[prop] !== objectB[prop] ) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + break; |
| 92 | + } |
| 93 | + } else { |
| 94 | + return false; |
| 95 | + } |
| 96 | + } else { |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + // Check for properties in B but not in A |
| 101 | + // This is about 15% faster (tested in Safari 5 and Firefox 3.6) |
| 102 | + // ...than incrementing a count variable in the above and below loops |
| 103 | + // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results |
| 104 | + for ( var prop in objectB ) { |
| 105 | + if ( !( prop in objectA ) ) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } else { |
| 112 | + return false; |
| 113 | + } |
| 114 | + return true; |
54 | 115 | } |
55 | 116 | }); |
56 | 117 | |