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