Merge "Whitelist the <wbr> element."
[mediawiki.git] / resources / jquery / jquery.client.js
blob5a95dc5b13c0e857b592d77cbf85386809c12195
1 /**
2  * User-agent detection
3  */
4 ( function ( $ ) {
6         /* Private Members */
8         /**
9          * @var {Object} profileCache Keyed by userAgent string,
10          * value is the parsed $.client.profile object for that user agent.
11          */
12         var profileCache = {};
14         /* Public Methods */
16         $.client = {
18                 /**
19                  * Get an object containing information about the client.
20                  *
21                  * @param {Object} nav An object with atleast a 'userAgent' and 'platform' key.
22                  * Defaults to the global Navigator object.
23                  * @returns {Object} The resulting client object will be in the following format:
24                  *  {
25                  *   'name': 'firefox',
26                  *   'layout': 'gecko',
27                  *   'layoutVersion': 20101026,
28                  *   'platform': 'linux'
29                  *   'version': '3.5.1',
30                  *   'versionBase': '3',
31                  *   'versionNumber': 3.5,
32                  *  }
33                  */
34                 profile: function ( nav ) {
35                         /*jshint boss: true */
37                         if ( nav === undefined ) {
38                                 nav = window.navigator;
39                         }
40                         // Use the cached version if possible
41                         if ( profileCache[nav.userAgent] === undefined ) {
43                                 var
44                                         versionNumber,
46                                         /* Configuration */
48                                         // Name of browsers or layout engines we don't recognize
49                                         uk = 'unknown',
50                                         // Generic version digit
51                                         x = 'x',
52                                         // Strings found in user agent strings that need to be conformed
53                                         wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
54                                         // Translations for conforming user agent strings
55                                         userAgentTranslations = [
56                                                 // Tons of browsers lie about being something they are not
57                                                 [/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
58                                                 // Chrome lives in the shadow of Safari still
59                                                 ['Chrome Safari', 'Chrome'],
60                                                 // KHTML is the layout engine not the browser - LIES!
61                                                 ['KHTML', 'Konqueror'],
62                                                 // Firefox nightly builds
63                                                 ['Minefield', 'Firefox'],
64                                                 // This helps keep different versions consistent
65                                                 ['Navigator', 'Netscape'],
66                                                 // This prevents version extraction issues, otherwise translation would happen later
67                                                 ['PLAYSTATION 3', 'PS3']
68                                         ],
69                                         // Strings which precede a version number in a user agent string - combined and used as
70                                         // match 1 in version detection
71                                         versionPrefixes = [
72                                                 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
73                                                 'lynx', 'msie', 'safari', 'ps3', 'android'
74                                         ],
75                                         // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
76                                         versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
77                                         // Names of known browsers
78                                         names = [
79                                                 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
80                                                 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
81                                         ],
82                                         // Tanslations for conforming browser names
83                                         nameTranslations = [],
84                                         // Names of known layout engines
85                                         layouts = ['gecko', 'konqueror', 'msie', 'trident', 'opera', 'webkit'],
86                                         // Translations for conforming layout names
87                                         layoutTranslations = [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
88                                         // Names of supported layout engines for version number
89                                         layoutVersions = ['applewebkit', 'gecko', 'trident'],
90                                         // Names of known operating systems
91                                         platforms = ['win', 'wow64', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
92                                         // Translations for conforming operating system names
93                                         platformTranslations = [ ['sunos', 'solaris'], ['wow64', 'win'] ],
95                                         /* Methods */
97                                         /**
98                                          * Performs multiple replacements on a string
99                                          */
100                                         translate = function ( source, translations ) {
101                                                 var i;
102                                                 for ( i = 0; i < translations.length; i++ ) {
103                                                         source = source.replace( translations[i][0], translations[i][1] );
104                                                 }
105                                                 return source;
106                                         },
108                                         /* Pre-processing */
110                                         ua = nav.userAgent,
111                                         match,
112                                         name = uk,
113                                         layout = uk,
114                                         layoutversion = uk,
115                                         platform = uk,
116                                         version = x;
118                                 if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( ua ) ) {
119                                         // Takes a userAgent string and translates given text into something we can more easily work with
120                                         ua = translate( ua, userAgentTranslations );
121                                 }
122                                 // Everything will be in lowercase from now on
123                                 ua = ua.toLowerCase();
125                                 /* Extraction */
127                                 if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
128                                         name = translate( match[1], nameTranslations );
129                                 }
130                                 if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
131                                         layout = translate( match[1], layoutTranslations );
132                                 }
133                                 if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
134                                         layoutversion = parseInt( match[2], 10 );
135                                 }
136                                 if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
137                                         platform = translate( match[1], platformTranslations );
138                                 }
139                                 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
140                                         version = match[3];
141                                 }
143                                 /* Edge Cases -- did I mention about how user agent string lie? */
145                                 // Decode Safari's crazy 400+ version numbers
146                                 if ( name === 'safari' && version > 400 ) {
147                                         version = '2.0';
148                                 }
149                                 // Expose Opera 10's lies about being Opera 9.8
150                                 if ( name === 'opera' && version >= 9.8 ) {
151                                         match = ua.match( /\bversion\/([0-9\.]*)/ );
152                                         if ( match && match[1] ) {
153                                                 version = match[1];
154                                         } else {
155                                                 version = '10';
156                                         }
157                                 }
158                                 // And Opera 15's lies about being Chrome
159                                 if ( name === 'chrome' && ( match = ua.match( /\bopr\/([0-9\.]*)/ ) ) ) {
160                                         if ( match[1] ) {
161                                                 name = 'opera';
162                                                 version = match[1];
163                                         }
164                                 }
165                                 // And IE 11's lies about being not being IE
166                                 if ( layout === 'trident' && layoutversion >= 7 && ( match = ua.match( /\brv[ :\/]([0-9\.]*)/ ) ) ) {
167                                         if ( match[1] ) {
168                                                 name = 'msie';
169                                                 version = match[1];
170                                         }
171                                 }
173                                 versionNumber = parseFloat( version, 10 ) || 0.0;
175                                 /* Caching */
177                                 profileCache[nav.userAgent] = {
178                                         name: name,
179                                         layout: layout,
180                                         layoutVersion: layoutversion,
181                                         platform: platform,
182                                         version: version,
183                                         versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
184                                         versionNumber: versionNumber
185                                 };
186                         }
187                         return profileCache[nav.userAgent];
188                 },
190                 /**
191                  * Checks the current browser against a support map object.
192                  *
193                  * A browser map is in the following format:
194                  * {
195                  *   // Multiple rules with configurable operators
196                  *   'msie': [['>=', 7], ['!=', 9]],
197                  *    // Match no versions
198                  *   'iphone': false,
199                  *    // Match any version
200                  *   'android': null
201                  * }
202                  *
203                  * It can optionally be split into ltr/rtl sections:
204                  * {
205                  *   'ltr': {
206                  *     'android': null,
207                  *     'iphone': false
208                  *   },
209                  *   'rtl': {
210                  *     'android': false,
211                  *     // rules are not inherited from ltr
212                  *     'iphone': false
213                  *   }
214                  * }
215                  *
216                  * @param {Object} map Browser support map
217                  * @param {Object} [profile] A client-profile object
218                  * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
219                  * returns true if the browser is not found.
220                  *
221                  * @returns {boolean} The current browser is in the support map
222                  */
223                 test: function ( map, profile, exactMatchOnly ) {
224                         /*jshint evil: true */
226                         var conditions, dir, i, op, val;
227                         profile = $.isPlainObject( profile ) ? profile : $.client.profile();
228                         if ( map.ltr && map.rtl ) {
229                                 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
230                                 map = map[dir];
231                         }
232                         // Check over each browser condition to determine if we are running in a compatible client
233                         if ( typeof map !== 'object' || map[profile.name] === undefined ) {
234                                 // Not found, return true if exactMatchOnly not set, false otherwise
235                                 return !exactMatchOnly;
236                         }
237                         conditions = map[profile.name];
238                         if ( conditions === false ) {
239                                 // Match no versions
240                                 return false;
241                         }
242                         if ( conditions === null ) {
243                                 // Match all versions
244                                 return true;
245                         }
246                         for ( i = 0; i < conditions.length; i++ ) {
247                                 op = conditions[i][0];
248                                 val = conditions[i][1];
249                                 if ( typeof val === 'string' ) {
250                                         if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
251                                                 return false;
252                                         }
253                                 } else if ( typeof val === 'number' ) {
254                                         if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
255                                                 return false;
256                                         }
257                                 }
258                         }
260                         return true;
261                 }
262         };
263 }( jQuery ) );