env.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * Environment features information.
  53. *
  54. * @memberOf module:utils/env~env
  55. * @namespace
  56. */
  57. features: {
  58. /**
  59. * Indicates that the environment supports ES2018 Unicode property escapes — like `\p{P}` or `\p{L}`.
  60. * More information about unicode properties might be found
  61. * [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
  62. *
  63. * @type {Boolean}
  64. */
  65. isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
  66. }
  67. };
  68. export default env;
  69. /**
  70. * Checks if User Agent represented by the string is running on Macintosh.
  71. *
  72. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  73. * @returns {Boolean} Whether User Agent is running on Macintosh or not.
  74. */
  75. export function isMac( userAgent ) {
  76. return userAgent.indexOf( 'macintosh' ) > -1;
  77. }
  78. /**
  79. * Checks if User Agent represented by the string is Microsoft Edge.
  80. *
  81. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  82. * @returns {Boolean} Whether User Agent is Edge or not.
  83. */
  84. export function isEdge( userAgent ) {
  85. return !!userAgent.match( /edge\/(\d+.?\d*)/ );
  86. }
  87. /**
  88. * Checks if User Agent represented by the string is Firefox (Gecko).
  89. *
  90. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  91. * @returns {Boolean} Whether User Agent is Firefox or not.
  92. */
  93. export function isGecko( userAgent ) {
  94. return !!userAgent.match( /gecko\/\d+/ );
  95. }
  96. /**
  97. * Checks if User Agent represented by the string is Safari.
  98. *
  99. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  100. * @returns {Boolean} Whether User Agent is Safari or not.
  101. */
  102. export function isSafari( userAgent ) {
  103. return userAgent.indexOf( ' applewebkit/' ) > -1 && userAgent.indexOf( 'chrome' ) === -1;
  104. }
  105. /**
  106. * Checks if User Agent represented by the string is Android mobile device.
  107. *
  108. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  109. * @returns {Boolean} Whether User Agent is Safari or not.
  110. */
  111. export function isAndroid( userAgent ) {
  112. return userAgent.indexOf( 'android' ) > -1;
  113. }
  114. /**
  115. * Checks if the current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
  116. * More information about unicode properties might be found
  117. * [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
  118. *
  119. * @returns {Boolean}
  120. */
  121. export function isRegExpUnicodePropertySupported() {
  122. let isSupported = false;
  123. // Feature detection for Unicode properties. Added in ES2018. Currently Firefox and Edge do not support it.
  124. // See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.
  125. try {
  126. // Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
  127. isSupported = 'ć'.search( new RegExp( '[\\p{L}]', 'u' ) ) === 0;
  128. } catch ( error ) {
  129. // Firefox throws a SyntaxError when the group is unsupported.
  130. }
  131. return isSupported;
  132. }