env.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals navigator:false */
  6. /**
  7. * @module utils/env
  8. */
  9. const userAgent = navigator.userAgent.toLowerCase();
  10. /**
  11. * A namespace containing environment and browser information.
  12. *
  13. * @namespace
  14. */
  15. const env = {
  16. /**
  17. * Indicates that the application is running on Macintosh.
  18. *
  19. * @static
  20. * @type {Boolean}
  21. */
  22. isMac: isMac( userAgent ),
  23. /**
  24. * Indicates that the application is running in Microsoft Edge.
  25. *
  26. * @static
  27. * @type {Boolean}
  28. */
  29. isEdge: isEdge( userAgent ),
  30. /**
  31. * Indicates that the application is running in Firefox (Gecko).
  32. *
  33. * @static
  34. * @type {Boolean}
  35. */
  36. isGecko: isGecko( userAgent ),
  37. /**
  38. * Indicates that the application is running in Safari.
  39. *
  40. * @static
  41. * @type {Boolean}
  42. */
  43. isSafari: isSafari( userAgent ),
  44. /**
  45. * Indicates that the application is running on Android mobile device.
  46. *
  47. * @static
  48. * @type {Boolean}
  49. */
  50. isAndroid: isAndroid( userAgent )
  51. };
  52. export default env;
  53. /**
  54. * Checks if User Agent represented by the string is running on Macintosh.
  55. *
  56. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  57. * @returns {Boolean} Whether User Agent is running on Macintosh or not.
  58. */
  59. export function isMac( userAgent ) {
  60. return userAgent.indexOf( 'macintosh' ) > -1;
  61. }
  62. /**
  63. * Checks if User Agent represented by the string is Microsoft Edge.
  64. *
  65. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  66. * @returns {Boolean} Whether User Agent is Edge or not.
  67. */
  68. export function isEdge( userAgent ) {
  69. return !!userAgent.match( /edge\/(\d+.?\d*)/ );
  70. }
  71. /**
  72. * Checks if User Agent represented by the string is Firefox (Gecko).
  73. *
  74. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  75. * @returns {Boolean} Whether User Agent is Firefox or not.
  76. */
  77. export function isGecko( userAgent ) {
  78. return !!userAgent.match( /gecko\/\d+/ );
  79. }
  80. /**
  81. * Checks if User Agent represented by the string is Safari.
  82. *
  83. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  84. * @returns {Boolean} Whether User Agent is Safari or not.
  85. */
  86. export function isSafari( userAgent ) {
  87. return userAgent.indexOf( ' applewebkit/' ) > -1 && userAgent.indexOf( 'chrome' ) === -1;
  88. }
  89. /**
  90. * Checks if User Agent represented by the string is Android mobile device.
  91. *
  92. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  93. * @returns {Boolean} Whether User Agent is Safari or not.
  94. */
  95. export function isAndroid( userAgent ) {
  96. return userAgent.indexOf( 'android' ) > -1;
  97. }