keyboard.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import env from '../src/env';
  6. import { keyCodes, getCode, parseKeystroke, getEnvKeystrokeText } from '../src/keyboard';
  7. import CKEditorError from '../src/ckeditorerror';
  8. describe( 'Keyboard', () => {
  9. describe( 'keyCodes', () => {
  10. it( 'contains numbers', () => {
  11. expect( keyCodes[ '0' ] ).to.equal( 48 );
  12. expect( keyCodes[ '9' ] ).to.equal( 57 );
  13. } );
  14. it( 'contains letters', () => {
  15. expect( keyCodes.a ).to.equal( 65 );
  16. expect( keyCodes.z ).to.equal( 90 );
  17. } );
  18. it( 'modifiers and other keys', () => {
  19. expect( keyCodes.delete ).to.equal( 46 );
  20. expect( keyCodes.ctrl ).to.equal( 0x110000 );
  21. expect( keyCodes.cmd ).to.equal( 0x110000 );
  22. expect( keyCodes.f1 ).to.equal( 112 );
  23. expect( keyCodes.f12 ).to.equal( 123 );
  24. expect( keyCodes ).to.include.keys(
  25. 'ctrl', 'cmd', 'shift', 'alt',
  26. 'arrowleft', 'arrowup', 'arrowright', 'arrowdown',
  27. 'backspace', 'delete', 'enter', 'space', 'esc', 'tab',
  28. 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'f11', 'f12'
  29. );
  30. } );
  31. } );
  32. describe( 'getCode', () => {
  33. it( 'gets code of a number', () => {
  34. expect( getCode( '0' ) ).to.equal( 48 );
  35. } );
  36. it( 'gets code of a letter', () => {
  37. expect( getCode( 'a' ) ).to.equal( 65 );
  38. } );
  39. it( 'gets code of a function key', () => {
  40. expect( getCode( 'f6' ) ).to.equal( 117 );
  41. } );
  42. it( 'is case insensitive', () => {
  43. expect( getCode( 'A' ) ).to.equal( 65 );
  44. expect( getCode( 'Ctrl' ) ).to.equal( 0x110000 );
  45. expect( getCode( 'ENTER' ) ).to.equal( 13 );
  46. } );
  47. it( 'throws when passed unknown key name', () => {
  48. expect( () => {
  49. getCode( 'foo' );
  50. } ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
  51. } );
  52. it( 'gets code of a keystroke info', () => {
  53. expect( getCode( { keyCode: 48 } ) ).to.equal( 48 );
  54. } );
  55. it( 'adds modifiers to the keystroke code', () => {
  56. expect( getCode( { keyCode: 48, altKey: true, ctrlKey: true, shiftKey: true } ) )
  57. .to.equal( 48 + 0x110000 + 0x220000 + 0x440000 );
  58. } );
  59. } );
  60. describe( 'parseKeystroke', () => {
  61. it( 'parses string', () => {
  62. expect( parseKeystroke( 'ctrl+a' ) ).to.equal( 0x110000 + 65 );
  63. } );
  64. it( 'allows spacing', () => {
  65. expect( parseKeystroke( 'ctrl + a' ) ).to.equal( 0x110000 + 65 );
  66. } );
  67. it( 'is case-insensitive', () => {
  68. expect( parseKeystroke( 'Ctrl+A' ) ).to.equal( 0x110000 + 65 );
  69. } );
  70. it( 'works with an array', () => {
  71. expect( parseKeystroke( [ 'ctrl', 'a' ] ) ).to.equal( 0x110000 + 65 );
  72. } );
  73. it( 'works with an array which contains numbers', () => {
  74. expect( parseKeystroke( [ 'shift', 33 ] ) ).to.equal( 0x220000 + 33 );
  75. } );
  76. it( 'works with two modifiers', () => {
  77. expect( parseKeystroke( 'ctrl+shift+a' ) ).to.equal( 0x110000 + 0x220000 + 65 );
  78. } );
  79. it( 'throws on unknown name', () => {
  80. expect( () => {
  81. parseKeystroke( 'foo' );
  82. } ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
  83. } );
  84. } );
  85. describe( 'getEnvKeystrokeText', () => {
  86. const initialEnvMac = env.isMac;
  87. afterEach( () => {
  88. env.isMac = initialEnvMac;
  89. } );
  90. describe( 'on Macintosh', () => {
  91. beforeEach( () => {
  92. env.isMac = true;
  93. } );
  94. it( 'replaces CTRL with ⌘', () => {
  95. expect( getEnvKeystrokeText( 'CTRL' ) ).to.equal( '⌘' );
  96. expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( '⌘A' );
  97. expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( '⌘A' );
  98. } );
  99. it( 'replaces SHIFT with ⇧', () => {
  100. expect( getEnvKeystrokeText( 'SHIFT' ) ).to.equal( '⇧' );
  101. expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( '⇧A' );
  102. expect( getEnvKeystrokeText( 'shift+A' ) ).to.equal( '⇧A' );
  103. } );
  104. it( 'replaces ALT with ⌥', () => {
  105. expect( getEnvKeystrokeText( 'ALT' ) ).to.equal( '⌥' );
  106. expect( getEnvKeystrokeText( 'ALT+A' ) ).to.equal( '⌥A' );
  107. expect( getEnvKeystrokeText( 'alt+A' ) ).to.equal( '⌥A' );
  108. } );
  109. it( 'work for multiple modifiers', () => {
  110. expect( getEnvKeystrokeText( 'CTRL+SHIFT+X' ) ).to.equal( '⌘⇧X' );
  111. expect( getEnvKeystrokeText( 'ALT+SHIFT+X' ) ).to.equal( '⌥⇧X' );
  112. } );
  113. it( 'does not touch other keys', () => {
  114. expect( getEnvKeystrokeText( 'ESC+A' ) ).to.equal( 'ESC+A' );
  115. expect( getEnvKeystrokeText( 'TAB' ) ).to.equal( 'TAB' );
  116. expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
  117. expect( getEnvKeystrokeText( 'A+CTRL+B' ) ).to.equal( 'A+⌘B' );
  118. } );
  119. } );
  120. describe( 'on non–Macintosh', () => {
  121. beforeEach( () => {
  122. env.isMac = false;
  123. } );
  124. it( 'does not touch anything', () => {
  125. expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( 'CTRL+A' );
  126. expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( 'ctrl+A' );
  127. expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
  128. expect( getEnvKeystrokeText( 'alt+A' ) ).to.equal( 'alt+A' );
  129. expect( getEnvKeystrokeText( 'CTRL+SHIFT+A' ) ).to.equal( 'CTRL+SHIFT+A' );
  130. expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
  131. } );
  132. } );
  133. } );
  134. } );