9 $.extend( mw
.utilities
, {
12 * Returns an object containing information about the browser
14 * The resulting client object will be in the following format:
21 * 'versionNumber': 3.5,
24 this.profile = function() {
25 // Use the cached version if possible
26 if ( typeof this.profile
=== 'undefined' ) {
30 // Name of browsers or layout engines we don't recognize
32 // Generic version digit
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'],
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'
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
61 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera', 'safari', 'ipod',
62 'iphone', 'blackberry', 'ps3'
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']];
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] );
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
);
92 // Everything will be in lowercase from now on
93 userAgent
= userAgent
.toLowerCase();
97 if ( match
= new RegExp( '(' + browserNames
.join( '|' ) + ')' ).exec( userAgent
) ) {
98 browser
= translate( match
[1], browserTranslations
);
100 if ( match
= new RegExp( '(' + layoutNames
.join( '|' ) + ')' ).exec( userAgent
) ) {
101 layout
= translate( match
[1], layoutTranslations
);
103 if ( match
= new RegExp( '(' + osNames
.join( '|' ) + ')' ).exec( navigator
.platform
.toLowerCase() ) ) {
104 var os
= translate( match
[1], osTranslations
);
106 if ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( userAgent
) ) {
110 /* Edge Cases -- did I mention about how user agent string lie? */
112 // Decode Safari's crazy 400+ version numbers
113 if ( name
.match( /safari/ ) && version
> 400 ) {
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;
128 'versionBase': ( version
!== x
? new String( version
).substr( 0, 1 ) : x
),
129 'versionNumber': ( parseFloat( version
, 10 ) || 0.0 )
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.
139 * A browser map is in the following format:
142 * // Multiple rules with configurable operators
143 * 'msie': [['>=', 7], ['!=', 9]],
144 * // Blocked entirely
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
155 * @param map Object of browser support map
157 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
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
167 for ( var condition
in browser
) {
168 var op
= browser
[condition
][0];
169 var val
= browser
[condition
][1];
170 if ( val
=== false ) {
172 } else if ( typeof val
== 'string' ) {
173 if ( !( eval( 'client.version' + op
+ '"' + val
+ '"' ) ) ) {
176 } else if ( typeof val
== 'number' ) {
177 if ( !( eval( 'client.versionNumber' + op
+ val
) ) ) {