env.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. import env, { isMac, isGecko, isSafari, isAndroid, isRegExpUnicodePropertySupported } from '../src/env';
  6. function toLowerCase( str ) {
  7. return str.toLowerCase();
  8. }
  9. describe( 'Env', () => {
  10. it( 'is an object', () => {
  11. expect( env ).to.be.an( 'object' );
  12. } );
  13. describe( 'isMac', () => {
  14. it( 'is a boolean', () => {
  15. expect( env.isMac ).to.be.a( 'boolean' );
  16. } );
  17. } );
  18. describe( 'isGecko', () => {
  19. it( 'is a boolean', () => {
  20. expect( env.isGecko ).to.be.a( 'boolean' );
  21. } );
  22. } );
  23. describe( 'isSafari', () => {
  24. it( 'is a boolean', () => {
  25. expect( env.isSafari ).to.be.a( 'boolean' );
  26. } );
  27. } );
  28. describe( 'isAndroid', () => {
  29. it( 'is a boolean', () => {
  30. expect( env.isAndroid ).to.be.a( 'boolean' );
  31. } );
  32. } );
  33. describe( 'features', () => {
  34. it( 'is an object', () => {
  35. expect( env.features ).to.be.an( 'object' );
  36. } );
  37. describe( 'isRegExpUnicodePropertySupported', () => {
  38. it( 'is a boolean', () => {
  39. expect( env.features.isRegExpUnicodePropertySupported ).to.be.a( 'boolean' );
  40. } );
  41. } );
  42. } );
  43. describe( 'isMac()', () => {
  44. it( 'returns true for macintosh UA strings', () => {
  45. expect( isMac( 'macintosh' ) ).to.be.true;
  46. expect( isMac( 'foo macintosh bar' ) ).to.be.true;
  47. expect( isMac( toLowerCase(
  48. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) ' +
  49. 'Chrome/61.0.3163.100 Safari/537.36'
  50. ) ) ).to.be.true;
  51. } );
  52. it( 'returns false for non–macintosh UA strings', () => {
  53. expect( isMac( '' ) ).to.be.false;
  54. expect( isMac( 'mac' ) ).to.be.false;
  55. expect( isMac( 'foo' ) ).to.be.false;
  56. } );
  57. } );
  58. describe( 'isGecko()', () => {
  59. it( 'returns true for Firefox UA strings', () => {
  60. expect( isGecko( 'gecko/42' ) ).to.be.true;
  61. expect( isGecko( 'foo gecko/42 bar' ) ).to.be.true;
  62. expect( isGecko( toLowerCase(
  63. 'mozilla/5.0 (macintosh; intel mac os x 10.13; rv:62.0) gecko/20100101 firefox/62.0'
  64. ) ) ).to.be.true;
  65. } );
  66. it( 'returns false for non–Edge UA strings', () => {
  67. expect( isGecko( '' ) ).to.be.false;
  68. expect( isGecko( 'foo' ) ).to.be.false;
  69. expect( isGecko( 'Mozilla' ) ).to.be.false;
  70. // Chrome
  71. expect( isGecko( toLowerCase(
  72. 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
  73. ) ) ).to.be.false;
  74. } );
  75. } );
  76. describe( 'isSafari()', () => {
  77. /* eslint-disable max-len */
  78. it( 'returns true for Safari UA strings', () => {
  79. expect( isSafari( toLowerCase(
  80. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15'
  81. ) ) ).to.be.true;
  82. expect( isSafari( toLowerCase(
  83. 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1'
  84. ) ) ).to.be.true;
  85. } );
  86. it( 'returns false for non-Safari UA strings', () => {
  87. expect( isSafari( toLowerCase(
  88. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
  89. ) ) ).to.be.false;
  90. expect( isSafari( toLowerCase(
  91. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
  92. ) ) ).to.be.false;
  93. expect( isSafari( toLowerCase(
  94. 'Mozilla/5.0 (Linux; Android 7.1; Mi A1 Build/N2G47H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36'
  95. ) ) ).to.be.false;
  96. } );
  97. /* eslint-enable max-len */
  98. } );
  99. describe( 'isAndroid()', () => {
  100. /* eslint-disable max-len */
  101. it( 'returns true for Android UA strings', () => {
  102. // Strings taken from https://developer.chrome.com/multidevice/user-agent.
  103. expect( isAndroid( toLowerCase(
  104. 'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>'
  105. ) ) ).to.be.true;
  106. expect( isAndroid( toLowerCase(
  107. 'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev>(KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev>'
  108. ) ) ).to.be.true;
  109. } );
  110. it( 'returns false for non-Android UA strings', () => {
  111. expect( isAndroid( toLowerCase(
  112. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
  113. ) ) ).to.be.false;
  114. expect( isAndroid( toLowerCase(
  115. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
  116. ) ) ).to.be.false;
  117. expect( isAndroid( toLowerCase(
  118. 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
  119. ) ) ).to.be.false;
  120. } );
  121. /* eslint-enable max-len */
  122. } );
  123. describe( 'isRegExpUnicodePropertySupported()', () => {
  124. it( 'should detect accessibility of unicode properties', () => {
  125. // Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534)
  126. const testFn = () => ( new RegExp( '\\p{L}', 'u' ) ).test( 'ć' );
  127. if ( isRegExpUnicodePropertySupported() ) {
  128. expect( testFn() ).to.be.true;
  129. } else {
  130. expect( testFn ).to.throw();
  131. }
  132. } );
  133. } );
  134. } );