fakeselectionobserver.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import createElement from 'ckeditor5/utils/dom/createelement.js';
  7. import FakeSelectionObserver from 'ckeditor5/engine/view/observer/fakeselectionobserver.js';
  8. import ViewDocument from 'ckeditor5/engine/view/document.js';
  9. import DomEventData from 'ckeditor5/engine/view/observer/domeventdata.js';
  10. import { keyCodes } from 'ckeditor5/utils/keyboard.js';
  11. import { setData, stringify } from 'ckeditor5/engine/dev-utils/view.js';
  12. describe( 'FakeSelectionObserver', () => {
  13. let observer;
  14. let viewDocument;
  15. let root;
  16. let domRoot;
  17. before( () => {
  18. domRoot = createElement( document, 'div', {
  19. contenteditable: 'true'
  20. } );
  21. document.body.appendChild( domRoot );
  22. } );
  23. after( () => {
  24. domRoot.parentElement.removeChild( domRoot );
  25. } );
  26. beforeEach( () => {
  27. viewDocument = new ViewDocument();
  28. root = viewDocument.createRoot( domRoot );
  29. observer = viewDocument.getObserver( FakeSelectionObserver );
  30. viewDocument.selection.setFake();
  31. } );
  32. afterEach( () => {
  33. viewDocument.destroy();
  34. } );
  35. it( 'should do nothing if selection is not fake', () => {
  36. viewDocument.selection.setFake( false );
  37. return checkEventPrevention( keyCodes.arrowleft, false );
  38. } );
  39. it( 'should do nothing if is disabled', () => {
  40. observer.disable();
  41. return checkEventPrevention( keyCodes.arrowleft, false );
  42. } );
  43. it( 'should prevent default for left arrow key', ( ) => {
  44. return checkEventPrevention( keyCodes.arrowleft );
  45. } );
  46. it( 'should prevent default for right arrow key', ( ) => {
  47. return checkEventPrevention( keyCodes.arrowright );
  48. } );
  49. it( 'should prevent default for up arrow key', ( ) => {
  50. return checkEventPrevention( keyCodes.arrowup );
  51. } );
  52. it( 'should prevent default for down arrow key', ( ) => {
  53. return checkEventPrevention( keyCodes.arrowdown );
  54. } );
  55. it( 'should fire selectionChange event with new selection when left arrow key is pressed', () => {
  56. return checkSelectionChange(
  57. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  58. keyCodes.arrowleft,
  59. '<container:p>foo[]<strong>bar</strong>baz</container:p>'
  60. );
  61. } );
  62. it( 'should fire selectionChange event with new selection when right arrow key is pressed', () => {
  63. return checkSelectionChange(
  64. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  65. keyCodes.arrowright,
  66. '<container:p>foo<strong>bar</strong>[]baz</container:p>'
  67. );
  68. } );
  69. it( 'should fire selectionChange event with new selection when up arrow key is pressed', () => {
  70. return checkSelectionChange(
  71. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  72. keyCodes.arrowup,
  73. '<container:p>foo[]<strong>bar</strong>baz</container:p>'
  74. );
  75. } );
  76. it( 'should fire selectionChange event with new selection when down arrow key is pressed', () => {
  77. return checkSelectionChange(
  78. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  79. keyCodes.arrowdown,
  80. '<container:p>foo<strong>bar</strong>[]baz</container:p>'
  81. );
  82. } );
  83. // Checks if preventDefault method was called by FakeSelectionObserver for specified key code.
  84. //
  85. // @param {Number} keyCode
  86. // @param {Boolean} shouldPrevent If set to true method checks if event was prevented.
  87. // @returns {Promise}
  88. function checkEventPrevention( keyCode, shouldPrevent = true ) {
  89. return new Promise( resolve => {
  90. const data = {
  91. keyCode,
  92. preventDefault: sinon.spy(),
  93. };
  94. viewDocument.once( 'keydown', () => {
  95. if ( shouldPrevent ) {
  96. sinon.assert.calledOnce( data.preventDefault );
  97. } else {
  98. sinon.assert.notCalled( data.preventDefault );
  99. }
  100. resolve();
  101. }, { priority: 'lowest' } );
  102. viewDocument.fire( 'keydown', new DomEventData( viewDocument, { target: document.body }, data ) );
  103. } );
  104. }
  105. // Checks if proper selectionChange event is fired by FakeSelectionObserver for specified key.
  106. //
  107. // @param {String} initialData
  108. // @param {Number} keyCode
  109. // @param {String} output
  110. // @returns {Promise}
  111. function checkSelectionChange( initialData, keyCode, output ) {
  112. return new Promise( resolve => {
  113. viewDocument.once( 'selectionChange', ( eventInfo, data ) => {
  114. expect( stringify( root.getChild( 0 ), data.newSelection, { showType: true } ) ).to.equal( output );
  115. resolve();
  116. } );
  117. setData( viewDocument, initialData );
  118. viewDocument.selection.setFake();
  119. const data = {
  120. keyCode,
  121. preventDefault: sinon.spy(),
  122. };
  123. viewDocument.fire( 'keydown', new DomEventData( viewDocument, { target: document.body }, data ) );
  124. } );
  125. }
  126. } );