env.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, isBlink } 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( 'isBlink', () => {
  34. it( 'is a boolean', () => {
  35. expect( env.isBlink ).to.be.a( 'boolean' );
  36. } );
  37. } );
  38. describe( 'features', () => {
  39. it( 'is an object', () => {
  40. expect( env.features ).to.be.an( 'object' );
  41. } );
  42. describe( 'isRegExpUnicodePropertySupported', () => {
  43. it( 'is a boolean', () => {
  44. expect( env.features.isRegExpUnicodePropertySupported ).to.be.a( 'boolean' );
  45. } );
  46. } );
  47. } );
  48. describe( 'isMac()', () => {
  49. it( 'returns true for macintosh UA strings', () => {
  50. expect( isMac( 'macintosh' ) ).to.be.true;
  51. expect( isMac( 'foo macintosh bar' ) ).to.be.true;
  52. expect( isMac( toLowerCase(
  53. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) ' +
  54. 'Chrome/61.0.3163.100 Safari/537.36'
  55. ) ) ).to.be.true;
  56. } );
  57. it( 'returns false for non–macintosh UA strings', () => {
  58. expect( isMac( '' ) ).to.be.false;
  59. expect( isMac( 'mac' ) ).to.be.false;
  60. expect( isMac( 'foo' ) ).to.be.false;
  61. } );
  62. } );
  63. describe( 'isGecko()', () => {
  64. it( 'returns true for Firefox UA strings', () => {
  65. expect( isGecko( 'gecko/42' ) ).to.be.true;
  66. expect( isGecko( 'foo gecko/42 bar' ) ).to.be.true;
  67. expect( isGecko( toLowerCase(
  68. 'mozilla/5.0 (macintosh; intel mac os x 10.13; rv:62.0) gecko/20100101 firefox/62.0'
  69. ) ) ).to.be.true;
  70. } );
  71. it( 'returns false for non–Edge UA strings', () => {
  72. expect( isGecko( '' ) ).to.be.false;
  73. expect( isGecko( 'foo' ) ).to.be.false;
  74. expect( isGecko( 'Mozilla' ) ).to.be.false;
  75. // Chrome
  76. expect( isGecko( toLowerCase(
  77. 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
  78. ) ) ).to.be.false;
  79. } );
  80. } );
  81. describe( 'isSafari()', () => {
  82. /* eslint-disable max-len */
  83. it( 'returns true for Safari UA strings', () => {
  84. expect( isSafari( toLowerCase(
  85. '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'
  86. ) ) ).to.be.true;
  87. expect( isSafari( toLowerCase(
  88. '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'
  89. ) ) ).to.be.true;
  90. } );
  91. it( 'returns false for non-Safari UA strings', () => {
  92. expect( isSafari( toLowerCase(
  93. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
  94. ) ) ).to.be.false;
  95. expect( isSafari( toLowerCase(
  96. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
  97. ) ) ).to.be.false;
  98. expect( isSafari( toLowerCase(
  99. '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'
  100. ) ) ).to.be.false;
  101. } );
  102. /* eslint-enable max-len */
  103. } );
  104. describe( 'isAndroid()', () => {
  105. /* eslint-disable max-len */
  106. it( 'returns true for Android UA strings', () => {
  107. // Strings taken from https://developer.chrome.com/multidevice/user-agent.
  108. expect( isAndroid( toLowerCase(
  109. 'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>'
  110. ) ) ).to.be.true;
  111. expect( isAndroid( toLowerCase(
  112. 'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev>(KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev>'
  113. ) ) ).to.be.true;
  114. } );
  115. it( 'returns false for non-Android UA strings', () => {
  116. expect( isAndroid( toLowerCase(
  117. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
  118. ) ) ).to.be.false;
  119. expect( isAndroid( toLowerCase(
  120. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
  121. ) ) ).to.be.false;
  122. expect( isAndroid( toLowerCase(
  123. 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
  124. ) ) ).to.be.false;
  125. } );
  126. /* eslint-enable max-len */
  127. } );
  128. describe( 'isBlink()', () => {
  129. /* eslint-disable max-len */
  130. it( 'returns true for Blink UA strings', () => {
  131. expect( isBlink( toLowerCase(
  132. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
  133. ) ) ).to.be.true;
  134. expect( isBlink( toLowerCase(
  135. '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'
  136. ) ) ).to.be.true;
  137. expect( isBlink( toLowerCase(
  138. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52'
  139. ) ) ).to.be.true;
  140. } );
  141. it( 'returns false for non-Blink UA strings', () => {
  142. expect( isBlink( toLowerCase(
  143. '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'
  144. ) ) ).to.be.false;
  145. expect( isBlink( toLowerCase(
  146. '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'
  147. ) ) ).to.be.false;
  148. expect( isBlink( toLowerCase(
  149. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
  150. ) ) ).to.be.false;
  151. expect( isBlink( toLowerCase(
  152. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134'
  153. ) ) ).to.be.false;
  154. } );
  155. /* eslint-enable max-len */
  156. } );
  157. describe( 'isRegExpUnicodePropertySupported()', () => {
  158. it( 'should detect accessibility of unicode properties', () => {
  159. // Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534)
  160. const testFn = () => ( new RegExp( '\\p{L}', 'u' ) ).test( 'ć' );
  161. if ( isRegExpUnicodePropertySupported() ) {
  162. expect( testFn() ).to.be.true;
  163. } else {
  164. expect( testFn ).to.throw();
  165. }
  166. } );
  167. } );
  168. } );