(bug 39067) Add support for HTML5 <mark> element.
[lhc/web/wiklou.git] / resources / jquery / jquery.client.js
1 /**
2 * User-agent detection
3 */
4 ( function ( $ ) {
5
6 /* Private Members */
7
8 /**
9 * @var profileCache {Object} Keyed by userAgent string,
10 * value is the parsed $.client.profile object for that user agent.
11 */
12 var profileCache = {};
13
14 /* Public Methods */
15
16 $.client = {
17
18 /**
19 * Get an object containing information about the client.
20 *
21 * @param nav {Object} An object with atleast a 'userAgent' and 'platform' key.
22 * Defaults to the global Navigator object.
23 * @return {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 */
36
37 if ( nav === undefined ) {
38 nav = window.navigator;
39 }
40 // Use the cached version if possible
41 if ( profileCache[nav.userAgent] === undefined ) {
42
43 var
44 versionNumber,
45
46 /* Configuration */
47
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'],
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 differnt 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 match 1 in
70 // version detectection
71 versionPrefixes = [
72 'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
73 'lynx', 'msie', 'safari', 'ps3'
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', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
80 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq'
81 ],
82 // Tanslations for conforming browser names
83 nameTranslations = [],
84 // Names of known layout engines
85 layouts = ['gecko', 'konqueror', 'msie', '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'],
90 // Names of known operating systems
91 platforms = ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
92 // Translations for conforming operating system names
93 platformTranslations = [ ['sunos', 'solaris'] ],
94
95 /* Methods */
96
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 },
107
108 /* Pre-processing */
109
110 ua = nav.userAgent,
111 match,
112 name = uk,
113 layout = uk,
114 layoutversion = uk,
115 platform = uk,
116 version = x;
117
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();
124
125 /* Extraction */
126
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 }
142
143 /* Edge Cases -- did I mention about how user agent string lie? */
144
145 // Decode Safari's crazy 400+ version numbers
146 if ( name.match( /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 version = ua.match( /version\/([0-9\.]*)/i )[1] || 10;
152 }
153 versionNumber = parseFloat( version, 10 ) || 0.0;
154
155 /* Caching */
156
157 profileCache[nav.userAgent] = {
158 name: name,
159 layout: layout,
160 layoutVersion: layoutversion,
161 platform: platform,
162 version: version,
163 versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
164 versionNumber: versionNumber
165 };
166 }
167 return profileCache[nav.userAgent];
168 },
169
170 /**
171 * Checks the current browser against a support map object to determine if the browser has been black-listed or
172 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
173 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
174 *
175 * A browser map is in the following format:
176 * {
177 * 'ltr': {
178 * // Multiple rules with configurable operators
179 * 'msie': [['>=', 7], ['!=', 9]],
180 * // Blocked entirely
181 * 'iphone': false
182 * },
183 * 'rtl': {
184 * // Test against a string
185 * 'msie': [['!==', '8.1.2.3']],
186 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
187 * 'iphone': false
188 * }
189 * }
190 *
191 * @param map {Object} Browser support map
192 * @param profile {Object} (optional) a client-profile object.
193 *
194 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
195 */
196 test: function ( map, profile ) {
197 /*jshint evil: true */
198
199 var conditions, dir, i, op, val;
200 profile = $.isPlainObject( profile ) ? profile : $.client.profile();
201
202 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
203 // Check over each browser condition to determine if we are running in a compatible client
204 if ( typeof map[dir] !== 'object' || map[dir][profile.name] === undefined ) {
205 // Unknown, so we assume it's working
206 return true;
207 }
208 conditions = map[dir][profile.name];
209 for ( i = 0; i < conditions.length; i++ ) {
210 op = conditions[i][0];
211 val = conditions[i][1];
212 if ( val === false ) {
213 return false;
214 }
215 if ( typeof val === 'string' ) {
216 if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
217 return false;
218 }
219 } else if ( typeof val === 'number' ) {
220 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
221 return false;
222 }
223 }
224 }
225 return true;
226 }
227 };
228 }( jQuery ) );