keyboard.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import env from 'ckeditor5/utils/env.js';
  6. import { keyCodes, getCode, parseKeystroke, getEnvKeystrokeText } from 'ckeditor5/utils/keyboard.js';
  7. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  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 ).to.include.keys(
  23. 'ctrl', 'cmd', 'shift', 'alt',
  24. 'arrowleft', 'arrowup', 'arrowright', 'arrowdown',
  25. 'backspace', 'delete', 'enter', 'esc', 'tab'
  26. );
  27. } );
  28. } );
  29. describe( 'getCode', () => {
  30. it( 'gets code of a number', () => {
  31. expect( getCode( '0' ) ).to.equal( 48 );
  32. } );
  33. it( 'gets code of a letter', () => {
  34. expect( getCode( 'a' ) ).to.equal( 65 );
  35. } );
  36. it( 'is case insensitive', () => {
  37. expect( getCode( 'A' ) ).to.equal( 65 );
  38. expect( getCode( 'Ctrl' ) ).to.equal( 0x110000 );
  39. expect( getCode( 'ENTER' ) ).to.equal( 13 );
  40. } );
  41. it( 'throws when passed unknown key name', () => {
  42. expect( () => {
  43. getCode( 'foo' );
  44. } ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
  45. } );
  46. it( 'gets code of a keystroke info', () => {
  47. expect( getCode( { keyCode: 48 } ) ).to.equal( 48 );
  48. } );
  49. it( 'adds modifiers to the keystroke code', () => {
  50. expect( getCode( { keyCode: 48, altKey: true, ctrlKey: true, shiftKey: true } ) )
  51. .to.equal( 48 + 0x110000 + 0x220000 + 0x440000 );
  52. } );
  53. } );
  54. describe( 'parseKeystroke', () => {
  55. it( 'parses string', () => {
  56. expect( parseKeystroke( 'ctrl+a' ) ).to.equal( 0x110000 + 65 );
  57. } );
  58. it( 'allows spacing', () => {
  59. expect( parseKeystroke( 'ctrl + a' ) ).to.equal( 0x110000 + 65 );
  60. } );
  61. it( 'is case-insensitive', () => {
  62. expect( parseKeystroke( 'Ctrl+A' ) ).to.equal( 0x110000 + 65 );
  63. } );
  64. it( 'works with an array', () => {
  65. expect( parseKeystroke( [ 'ctrl', 'a' ] ) ).to.equal( 0x110000 + 65 );
  66. } );
  67. it( 'works with an array which contains numbers', () => {
  68. expect( parseKeystroke( [ 'shift', 33 ] ) ).to.equal( 0x220000 + 33 );
  69. } );
  70. it( 'works with two modifiers', () => {
  71. expect( parseKeystroke( 'ctrl+shift+a' ) ).to.equal( 0x110000 + 0x220000 + 65 );
  72. } );
  73. it( 'throws on unknown name', () => {
  74. expect( () => {
  75. parseKeystroke( 'foo' );
  76. } ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
  77. } );
  78. } );
  79. describe( 'getEnvKeystrokeText', () => {
  80. let initialEnvMac = env.mac;
  81. afterEach( () => {
  82. env.mac = initialEnvMac;
  83. } );
  84. describe( 'on Macintosh', () => {
  85. beforeEach( () => {
  86. env.mac = true;
  87. } );
  88. it( 'replaces CTRL with ⌘', () => {
  89. expect( getEnvKeystrokeText( 'CTRL' ) ).to.equal( '⌘' );
  90. expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( '⌘A' );
  91. expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( '⌘A' );
  92. } );
  93. it( 'does not touch other keys', () => {
  94. expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
  95. expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
  96. } );
  97. } );
  98. describe( 'on non–Macintosh', () => {
  99. beforeEach( () => {
  100. env.mac = false;
  101. } );
  102. it( 'does not touch anything', () => {
  103. expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( 'CTRL+A' );
  104. expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( 'ctrl+A' );
  105. expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
  106. expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
  107. } );
  108. } );
  109. } );
  110. } );