Wrapping core modules (FIXME from r79929)
[lhc/web/wiklou.git] / resources / jquery / jquery.client.js
1 /**
2 * User-agent detection
3 */
4 */
5 ( function( $ ) {
6 $.client = new ( function() {
7
8 /* Private Members */
9
10 var profile;
11
12 /* Public Functions */
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 this.profile = function() {
29 // Use the cached version if possible
30 if ( typeof 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 function translate( 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, match, name = uk, layout = uk, layoutversion = uk, platform = uk, version = x;
94 if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( userAgent ) ) {
95 // Takes a userAgent string and translates given text into something we can more easily work with
96 userAgent = translate( userAgent, userAgentTranslations );
97 }
98 // Everything will be in lowercase from now on
99 userAgent = userAgent.toLowerCase();
100
101 /* Extraction */
102
103 if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( userAgent ) ) {
104 name = translate( match[1], nameTranslations );
105 }
106 if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( userAgent ) ) {
107 layout = translate( match[1], layoutTranslations );
108 }
109 if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( navigator.userAgent.toLowerCase() ) ) {
110 layoutversion = parseInt(match[2]);
111 }
112 if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( navigator.platform.toLowerCase() ) ) {
113 platform = translate( match[1], platformTranslations );
114 }
115 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( userAgent ) ) {
116 version = match[3];
117 }
118
119 /* Edge Cases -- did I mention about how user agent string lie? */
120
121 // Decode Safari's crazy 400+ version numbers
122 if ( name.match( /safari/ ) && version > 400 ) {
123 version = '2.0';
124 }
125 // Expose Opera 10's lies about being Opera 9.8
126 if ( name === 'opera' && version >= 9.8) {
127 version = userAgent.match( /version\/([0-9\.]*)/i )[1] || 10;
128 }
129
130 /* Caching */
131
132 profile = {
133 'name': name,
134 'layout': layout,
135 'layoutVersion': layoutversion,
136 'platform': platform,
137 'version': version,
138 'versionBase': ( version !== x ? new String( version ).substr( 0, 1 ) : x ),
139 'versionNumber': ( parseFloat( version, 10 ) || 0.0 )
140 };
141 }
142 return profile;
143 };
144
145 /**
146 * Checks the current browser against a support map object to determine if the browser has been black-listed or
147 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
148 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
149 *
150 * A browser map is in the following format:
151 * {
152 * 'ltr': {
153 * // Multiple rules with configurable operators
154 * 'msie': [['>=', 7], ['!=', 9]],
155 * // Blocked entirely
156 * 'iphone': false
157 * },
158 * 'rtl': {
159 * // Test against a string
160 * 'msie': [['!==', '8.1.2.3']],
161 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
162 * 'iphone': false
163 * }
164 * }
165 *
166 * @param map Object of browser support map
167 *
168 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
169 */
170 this.test = function( map ) {
171 var profile = $.client.profile();
172 var dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
173 // Check over each browser condition to determine if we are running in a compatible client
174 if ( typeof map[dir] !== 'object' || typeof map[dir][profile.name] === 'undefined' ) {
175 // Unknown, so we assume it's working
176 return true;
177 }
178 var name = map[dir][profile.name];
179 for ( var condition in name ) {
180 var op = name[condition][0];
181 var val = name[condition][1];
182 if ( val === false ) {
183 return false;
184 } else if ( typeof val == 'string' ) {
185 if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
186 return false;
187 }
188 } else if ( typeof val == 'number' ) {
189 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
190 return false;
191 }
192 }
193 }
194 return true;
195 }
196 } )();
197
198 $( document ).ready( function() {
199 var profile = $.client.profile();
200 $( 'html' )
201 .addClass( 'client-' + profile.name )
202 .addClass( 'client-' + profile.name + '-' + profile.versionBase )
203 .addClass( 'client-' + profile.layout )
204 .addClass( 'client-' + profile.platform );
205 } );
206
207 } )( jQuery );