PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/lib/jquery.client/jquery.client.js

https://gitlab.com/link233/bootmw
JavaScript | 320 lines | 170 code | 23 blank | 127 comment | 46 complexity | f9f911ce24544575e59bd6087fe75a59 MD5 | raw file
  1. /*!
  2. * jQuery Client v2.0.0
  3. * https://www.mediawiki.org/wiki/JQuery_Client
  4. *
  5. * Copyright 2010-2015 jquery-client maintainers and other contributors.
  6. * Released under the MIT license
  7. * http://jquery-client.mit-license.org
  8. */
  9. /**
  10. * User-agent detection
  11. *
  12. * @class jQuery.client
  13. * @singleton
  14. */
  15. ( function ( $ ) {
  16. /**
  17. * @private
  18. * @property {Object} profileCache Keyed by userAgent string,
  19. * value is the parsed $.client.profile object for that user agent.
  20. */
  21. var profileCache = {};
  22. $.client = {
  23. /**
  24. * Get an object containing information about the client.
  25. *
  26. * @param {Object} [nav] An object with a 'userAgent' and 'platform' property.
  27. * Defaults to the global `navigator` object.
  28. * @return {Object} The resulting client object will be in the following format:
  29. *
  30. * {
  31. * 'name': 'firefox',
  32. * 'layout': 'gecko',
  33. * 'layoutVersion': 20101026,
  34. * 'platform': 'linux'
  35. * 'version': '3.5.1',
  36. * 'versionBase': '3',
  37. * 'versionNumber': 3.5,
  38. * }
  39. */
  40. profile: function ( nav ) {
  41. /*jshint boss:true */
  42. if ( nav === undefined ) {
  43. nav = window.navigator;
  44. }
  45. // Use the cached version if possible
  46. if ( profileCache[ nav.userAgent + '|' + nav.platform ] !== undefined ) {
  47. return profileCache[ nav.userAgent + '|' + nav.platform ];
  48. }
  49. var
  50. versionNumber,
  51. key = nav.userAgent + '|' + nav.platform,
  52. // Configuration
  53. // Name of browsers or layout engines we don't recognize
  54. uk = 'unknown',
  55. // Generic version digit
  56. x = 'x',
  57. // Strings found in user agent strings that need to be conformed
  58. wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
  59. // Translations for conforming user agent strings
  60. userAgentTranslations = [
  61. // Tons of browsers lie about being something they are not
  62. [/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
  63. // Chrome lives in the shadow of Safari still
  64. ['Chrome Safari', 'Chrome'],
  65. // KHTML is the layout engine not the browser - LIES!
  66. ['KHTML', 'Konqueror'],
  67. // Firefox nightly builds
  68. ['Minefield', 'Firefox'],
  69. // This helps keep different versions consistent
  70. ['Navigator', 'Netscape'],
  71. // This prevents version extraction issues, otherwise translation would happen later
  72. ['PLAYSTATION 3', 'PS3']
  73. ],
  74. // Strings which precede a version number in a user agent string - combined and used as
  75. // match 1 in version detection
  76. versionPrefixes = [
  77. 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
  78. 'lynx', 'msie', 'safari', 'ps3', 'android'
  79. ],
  80. // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
  81. versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
  82. // Names of known browsers
  83. names = [
  84. 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
  85. 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
  86. ],
  87. // Tanslations for conforming browser names
  88. nameTranslations = [],
  89. // Names of known layout engines
  90. layouts = ['gecko', 'konqueror', 'msie', 'trident', 'edge', 'opera', 'webkit'],
  91. // Translations for conforming layout names
  92. layoutTranslations = [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
  93. // Names of supported layout engines for version number
  94. layoutVersions = ['applewebkit', 'gecko', 'trident', 'edge'],
  95. // Names of known operating systems
  96. platforms = ['win', 'wow64', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
  97. // Translations for conforming operating system names
  98. platformTranslations = [ ['sunos', 'solaris'], ['wow64', 'win'] ],
  99. /**
  100. * Performs multiple replacements on a string
  101. * @ignore
  102. */
  103. translate = function ( source, translations ) {
  104. var i;
  105. for ( i = 0; i < translations.length; i++ ) {
  106. source = source.replace( translations[i][0], translations[i][1] );
  107. }
  108. return source;
  109. },
  110. // Pre-processing
  111. ua = nav.userAgent,
  112. match,
  113. name = uk,
  114. layout = uk,
  115. layoutversion = uk,
  116. platform = uk,
  117. 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();
  124. // Extraction
  125. if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
  126. name = translate( match[1], nameTranslations );
  127. }
  128. if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
  129. layout = translate( match[1], layoutTranslations );
  130. }
  131. if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
  132. layoutversion = parseInt( match[2], 10 );
  133. }
  134. if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
  135. platform = translate( match[1], platformTranslations );
  136. }
  137. if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
  138. version = match[3];
  139. }
  140. // Edge Cases -- did I mention about how user agent string lie?
  141. // Decode Safari's crazy 400+ version numbers
  142. if ( name === 'safari' && version > 400 ) {
  143. version = '2.0';
  144. }
  145. // Expose Opera 10's lies about being Opera 9.8
  146. if ( name === 'opera' && version >= 9.8 ) {
  147. match = ua.match( /\bversion\/([0-9\.]*)/ );
  148. if ( match && match[1] ) {
  149. version = match[1];
  150. } else {
  151. version = '10';
  152. }
  153. }
  154. // And Opera 15's lies about being Chrome
  155. if ( name === 'chrome' && ( match = ua.match( /\bopr\/([0-9\.]*)/ ) ) ) {
  156. if ( match[1] ) {
  157. name = 'opera';
  158. version = match[1];
  159. }
  160. }
  161. // And IE 11's lies about being not being IE
  162. if ( layout === 'trident' && layoutversion >= 7 && ( match = ua.match( /\brv[ :\/]([0-9\.]*)/ ) ) ) {
  163. if ( match[1] ) {
  164. name = 'msie';
  165. version = match[1];
  166. }
  167. }
  168. // And MS Edge's lies about being Chrome
  169. //
  170. // It's different enough from classic IE Trident engine that they do this
  171. // to avoid getting caught by MSIE-specific browser sniffing.
  172. if ( name === 'chrome' && ( match = ua.match( /\bedge\/([0-9\.]*)/ ) ) ) {
  173. name = 'edge';
  174. version = match[1];
  175. layout = 'edge';
  176. layoutversion = parseInt( match[1], 10 );
  177. }
  178. // And Amazon Silk's lies about being Android on mobile or Safari on desktop
  179. if ( match = ua.match( /\bsilk\/([0-9.\-_]*)/ ) ) {
  180. if ( match[1] ) {
  181. name = 'silk';
  182. version = match[1];
  183. }
  184. }
  185. versionNumber = parseFloat( version, 10 ) || 0.0;
  186. // Caching
  187. return profileCache[ key ] = {
  188. name: name,
  189. layout: layout,
  190. layoutVersion: layoutversion,
  191. platform: platform,
  192. version: version,
  193. versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
  194. versionNumber: versionNumber
  195. };
  196. },
  197. /**
  198. * Checks the current browser against a support map object.
  199. *
  200. * Version numbers passed as numeric values will be compared like numbers (1.2 > 1.11).
  201. * Version numbers passed as string values will be compared using a simple component-wise
  202. * algorithm, similar to PHP's version_compare ('1.2' < '1.11').
  203. *
  204. * A browser map is in the following format:
  205. *
  206. * {
  207. * // Multiple rules with configurable operators
  208. * 'msie': [['>=', 7], ['!=', 9]],
  209. * // Match no versions
  210. * 'iphone': false,
  211. * // Match any version
  212. * 'android': null
  213. * }
  214. *
  215. * It can optionally be split into ltr/rtl sections:
  216. *
  217. * {
  218. * 'ltr': {
  219. * 'android': null,
  220. * 'iphone': false
  221. * },
  222. * 'rtl': {
  223. * 'android': false,
  224. * // rules are not inherited from ltr
  225. * 'iphone': false
  226. * }
  227. * }
  228. *
  229. * @param {Object} map Browser support map
  230. * @param {Object} [profile] A client-profile object
  231. * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
  232. * returns true if the browser is not found.
  233. *
  234. * @return {boolean} The current browser is in the support map
  235. */
  236. test: function ( map, profile, exactMatchOnly ) {
  237. /*jshint evil:true */
  238. var conditions, dir, i, op, val, j, pieceVersion, pieceVal, compare;
  239. profile = $.isPlainObject( profile ) ? profile : $.client.profile();
  240. if ( map.ltr && map.rtl ) {
  241. dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
  242. map = map[dir];
  243. }
  244. // Check over each browser condition to determine if we are running in a compatible client
  245. if ( typeof map !== 'object' || map[profile.name] === undefined ) {
  246. // Not found, return true if exactMatchOnly not set, false otherwise
  247. return !exactMatchOnly;
  248. }
  249. conditions = map[profile.name];
  250. if ( conditions === false ) {
  251. // Match no versions
  252. return false;
  253. }
  254. if ( conditions === null ) {
  255. // Match all versions
  256. return true;
  257. }
  258. for ( i = 0; i < conditions.length; i++ ) {
  259. op = conditions[i][0];
  260. val = conditions[i][1];
  261. if ( typeof val === 'string' ) {
  262. // Perform a component-wise comparison of versions, similar to PHP's version_compare
  263. // but simpler. '1.11' is larger than '1.2'.
  264. pieceVersion = profile.version.toString().split( '.' );
  265. pieceVal = val.split( '.' );
  266. // Extend with zeroes to equal length
  267. while ( pieceVersion.length < pieceVal.length ) {
  268. pieceVersion.push( '0' );
  269. }
  270. while ( pieceVal.length < pieceVersion.length ) {
  271. pieceVal.push( '0' );
  272. }
  273. // Compare components
  274. compare = 0;
  275. for ( j = 0; j < pieceVersion.length; j++ ) {
  276. if ( Number( pieceVersion[j] ) < Number( pieceVal[j] ) ) {
  277. compare = -1;
  278. break;
  279. } else if ( Number( pieceVersion[j] ) > Number( pieceVal[j] ) ) {
  280. compare = 1;
  281. break;
  282. }
  283. }
  284. // compare will be -1, 0 or 1, depending on comparison result
  285. if ( !( eval( String( compare + op + '0' ) ) ) ) {
  286. return false;
  287. }
  288. } else if ( typeof val === 'number' ) {
  289. if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
  290. return false;
  291. }
  292. }
  293. }
  294. return true;
  295. }
  296. };
  297. }( jQuery ) );