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