Cleanup to deletion related code:
[mediawiki.git] / resources / jquery / jquery.client.js
blobae74a324118bb4e8afeda045ac87631d69589fa5
1 /**
2  * User-agent detection
3  */
4 ( function( $ ) {
6         /* Private Members */
8         /**
9          * @var profileCache {Object} Keyed by userAgent string,
10          * value is the parsed $.client.profile object for that user agent.
11          */
12         var profileCache = {};
14         /* Public Methods */
16         $.client = {
18                 /**
19                  * Get an object containing information about the client.
20                  *
21                  * @param nav {Object} An object with atleast a 'userAgent' and 'platform' key.=
22                  * Defaults to the global Navigator object.
23                  * @return {Object} The resulting client object will be in the following format:
24                  *  {
25                  *   'name': 'firefox',
26                  *   'layout': 'gecko',
27                  *   'layoutVersion': 20101026,
28                  *   'platform': 'linux'
29                  *   'version': '3.5.1',
30                  *   'versionBase': '3',
31                  *   'versionNumber': 3.5,
32                  *  }
33                  */
34                 profile: function( nav ) {
35                         if ( nav === undefined ) {
36                                 nav = window.navigator;
37                         }
38                         // Use the cached version if possible
39                         if ( profileCache[nav.userAgent] === undefined ) {
41                                 /* Configuration */
43                                 // Name of browsers or layout engines we don't recognize
44                                 var uk = 'unknown';
45                                 // Generic version digit
46                                 var x = 'x';
47                                 // Strings found in user agent strings that need to be conformed
48                                 var wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3'];
49                                 // Translations for conforming user agent strings
50                                 var userAgentTranslations = [
51                                         // Tons of browsers lie about being something they are not
52                                         [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
53                                         // Chrome lives in the shadow of Safari still
54                                         ['Chrome Safari', 'Chrome'],
55                                         // KHTML is the layout engine not the browser - LIES!
56                                         ['KHTML', 'Konqueror'],
57                                         // Firefox nightly builds
58                                         ['Minefield', 'Firefox'],
59                                         // This helps keep differnt versions consistent
60                                         ['Navigator', 'Netscape'],
61                                         // This prevents version extraction issues, otherwise translation would happen later
62                                         ['PLAYSTATION 3', 'PS3']
63                                 ];
64                                 // Strings which precede a version number in a user agent string - combined and used as match 1 in
65                                 // version detectection
66                                 var versionPrefixes = [
67                                         'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror', 'lynx',
68                                         'msie', 'safari', 'ps3'
69                                 ];
70                                 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
71                                 var versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)';
72                                 // Names of known browsers
73                                 var names = [
74                                         'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera', 'safari', 'ipod',
75                                         'iphone', 'blackberry', 'ps3'
76                                 ];
77                                 // Tanslations for conforming browser names
78                                 var nameTranslations = [];
79                                 // Names of known layout engines
80                                 var layouts = ['gecko', 'konqueror', 'msie', 'opera', 'webkit'];
81                                 // Translations for conforming layout names
82                                 var layoutTranslations = [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']];
83                                 // Names of supported layout engines for version number
84                                 var layoutVersions = ['applewebkit', 'gecko'];
85                                 // Names of known operating systems
86                                 var platforms = ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'];
87                                 // Translations for conforming operating system names
88                                 var platformTranslations = [['sunos', 'solaris']];
90                                 /* Methods */
92                                 // Performs multiple replacements on a string
93                                 var translate = function( source, translations ) {
94                                         for ( var i = 0; i < translations.length; i++ ) {
95                                                 source = source.replace( translations[i][0], translations[i][1] );
96                                         }
97                                         return source;
98                                 };
100                                 /* Pre-processing */
102                                 var     ua = nav.userAgent,
103                                         match,
104                                         name = uk,
105                                         layout = uk,
106                                         layoutversion = uk,
107                                         platform = uk,
108                                         version = x;
110                                 if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( ua ) ) {
111                                         // Takes a userAgent string and translates given text into something we can more easily work with
112                                         ua = translate( ua, userAgentTranslations );
113                                 }
114                                 // Everything will be in lowercase from now on
115                                 ua = ua.toLowerCase();
117                                 /* Extraction */
119                                 if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
120                                         name = translate( match[1], nameTranslations );
121                                 }
122                                 if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
123                                         layout = translate( match[1], layoutTranslations );
124                                 }
125                                 if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
126                                         layoutversion = parseInt( match[2], 10 );
127                                 }
128                                 if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
129                                         platform = translate( match[1], platformTranslations );
130                                 }
131                                 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
132                                         version = match[3];
133                                 }
135                                 /* Edge Cases -- did I mention about how user agent string lie? */
137                                 // Decode Safari's crazy 400+ version numbers
138                                 if ( name.match( /safari/ ) && version > 400 ) {
139                                         version = '2.0';
140                                 }
141                                 // Expose Opera 10's lies about being Opera 9.8
142                                 if ( name === 'opera' && version >= 9.8) {
143                                         version = ua.match( /version\/([0-9\.]*)/i )[1] || 10;
144                                 }
145                                 var versionNumber = parseFloat( version, 10 ) || 0.0;
147                                 /* Caching */
149                                 profileCache[nav.userAgent] = {
150                                         'name': name,
151                                         'layout': layout,
152                                         'layoutVersion': layoutversion,
153                                         'platform': platform,
154                                         'version': version,
155                                         'versionBase': ( version !== x ? Math.floor( versionNumber ).toString() : x ),
156                                         'versionNumber': versionNumber
157                                 };
158                         }
159                         return profileCache[nav.userAgent];
160                 },
162                 /**
163                  * Checks the current browser against a support map object to determine if the browser has been black-listed or
164                  * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
165                  * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
166                  *
167                  * A browser map is in the following format:
168                  * {
169                  *   'ltr': {
170                  *     // Multiple rules with configurable operators
171                  *     'msie': [['>=', 7], ['!=', 9]],
172                  *      // Blocked entirely
173                  *     'iphone': false
174                  *   },
175                  *   'rtl': {
176                  *     // Test against a string
177                  *     'msie': [['!==', '8.1.2.3']],
178                  *     // RTL rules do not fall through to LTR rules, you must explicity set each of them
179                  *     'iphone': false
180                  *   }
181                  * }
182                  *
183                  * @param map {Object} Browser support map
184                  * @param profile {Object} (optional) a client-profile object.
185                  *
186                  * @return Boolean true if browser known or assumed to be supported, false if blacklisted
187                  */
188                 test: function( map, profile ) {
189                         profile = $.isPlainObject( profile ) ? profile : $.client.profile();
191                         var dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
192                         // Check over each browser condition to determine if we are running in a compatible client
193                         if ( typeof map[dir] !== 'object' || typeof map[dir][profile.name] === 'undefined' ) {
194                                 // Unknown, so we assume it's working
195                                 return true;
196                         }
197                         var conditions = map[dir][profile.name];
198                         for ( var i = 0; i < conditions.length; i++ ) {
199                                 var op = conditions[i][0];
200                                 var val = conditions[i][1];
201                                 if ( val === false ) {
202                                         return false;
203                                 } else if ( typeof val == 'string' ) {
204                                         if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
205                                                 return false;
206                                         }
207                                 } else if ( typeof val == 'number' ) {
208                                         if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
209                                                 return false;
210                                         }
211                                 }
212                         }
213                         return true;
214                 }
215         };
216 } )( jQuery );