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