8
0

env.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 Firefox (Gecko).
  25. *
  26. * @static
  27. * @type {Boolean}
  28. */
  29. isGecko: isGecko( userAgent ),
  30. /**
  31. * Indicates that the application is running in Safari.
  32. *
  33. * @static
  34. * @type {Boolean}
  35. */
  36. isSafari: isSafari( userAgent ),
  37. /**
  38. * Indicates that the application is running on Android mobile device.
  39. *
  40. * @static
  41. * @type {Boolean}
  42. */
  43. isAndroid: isAndroid( userAgent ),
  44. /**
  45. * Environment features information.
  46. *
  47. * @memberOf module:utils/env~env
  48. * @namespace
  49. */
  50. features: {
  51. /**
  52. * Indicates that the environment supports ES2018 Unicode property escapes — like `\p{P}` or `\p{L}`.
  53. * More information about unicode properties might be found
  54. * [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
  55. *
  56. * @type {Boolean}
  57. */
  58. isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
  59. }
  60. };
  61. export default env;
  62. /**
  63. * Checks if User Agent represented by the string is running on Macintosh.
  64. *
  65. * @param {String} userAgent **Lowercase** `navigator.userAgent` string.
  66. * @returns {Boolean} Whether User Agent is running on Macintosh or not.
  67. */
  68. export function isMac( userAgent ) {
  69. return userAgent.indexOf( 'macintosh' ) > -1;
  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. }
  98. /**
  99. * Checks if the current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
  100. * More information about unicode properties might be found
  101. * [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
  102. *
  103. * @returns {Boolean}
  104. */
  105. export function isRegExpUnicodePropertySupported() {
  106. let isSupported = false;
  107. // Feature detection for Unicode properties. Added in ES2018. Currently Firefox does not support it.
  108. // See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.
  109. try {
  110. // Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
  111. isSupported = 'ć'.search( new RegExp( '[\\p{L}]', 'u' ) ) === 0;
  112. } catch ( error ) {
  113. // Firefox throws a SyntaxError when the group is unsupported.
  114. }
  115. return isSupported;
  116. }