selectionobserver.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals setTimeout, setInterval, clearInterval, document */
  6. import ViewRange from '../../../src/view/range';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import ViewSelection from '../../../src/view/selection';
  9. import ViewDocument from '../../../src/view/document';
  10. import SelectionObserver from '../../../src/view/observer/selectionobserver';
  11. import MutationObserver from '../../../src/view/observer/mutationobserver';
  12. import FocusObserver from '../../../src/view/observer/focusobserver';
  13. import log from '@ckeditor/ckeditor5-utils/src/log';
  14. import { parse } from '../../../src/dev-utils/view';
  15. testUtils.createSinonSandbox();
  16. describe( 'SelectionObserver', () => {
  17. let viewDocument, viewRoot, mutationObserver, selectionObserver, domRoot, domMain, domDocument;
  18. beforeEach( ( done ) => {
  19. domDocument = document;
  20. domRoot = domDocument.createElement( 'div' );
  21. domRoot.innerHTML = `<div contenteditable="true"></div><div contenteditable="true" id="additional"></div>`;
  22. domMain = domRoot.childNodes[ 0 ];
  23. domDocument.body.appendChild( domRoot );
  24. viewDocument = new ViewDocument();
  25. viewDocument.createRoot( domMain );
  26. mutationObserver = viewDocument.getObserver( MutationObserver );
  27. selectionObserver = viewDocument.getObserver( SelectionObserver );
  28. viewRoot = viewDocument.getRoot();
  29. viewRoot.appendChildren( parse(
  30. '<container:p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</container:p>' +
  31. '<container:p>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</container:p>' ) );
  32. viewDocument.render();
  33. viewDocument.selection.removeAllRanges();
  34. domDocument.getSelection().removeAllRanges();
  35. viewDocument.isFocused = true;
  36. selectionObserver.enable();
  37. // Ensure selectionchange will not be fired.
  38. setTimeout( () => done(), 100 );
  39. } );
  40. afterEach( () => {
  41. domRoot.parentElement.removeChild( domRoot );
  42. viewDocument.destroy();
  43. } );
  44. it( 'should fire selectionChange when it is the only change', ( done ) => {
  45. viewDocument.on( 'selectionChange', ( evt, data ) => {
  46. expect( data ).to.have.property( 'domSelection' ).that.equals( domDocument.getSelection() );
  47. expect( data ).to.have.property( 'oldSelection' ).that.is.instanceof( ViewSelection );
  48. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  49. expect( data ).to.have.property( 'newSelection' ).that.is.instanceof( ViewSelection );
  50. expect( data.newSelection.rangeCount ).to.equal( 1 );
  51. const newViewRange = data.newSelection.getFirstRange();
  52. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  53. expect( newViewRange.start.parent ).to.equal( viewFoo );
  54. expect( newViewRange.start.offset ).to.equal( 2 );
  55. expect( newViewRange.end.parent ).to.equal( viewFoo );
  56. expect( newViewRange.end.offset ).to.equal( 2 );
  57. done();
  58. } );
  59. changeDomSelection();
  60. } );
  61. it( 'should add only one listener to one document', ( done ) => {
  62. // Add second roots to ensure that listener is added once.
  63. viewDocument.createRoot( domDocument.getElementById( 'additional' ), 'additional' );
  64. viewDocument.on( 'selectionChange', () => {
  65. done();
  66. } );
  67. changeDomSelection();
  68. } );
  69. it( 'should not fire selectionChange on render', ( done ) => {
  70. viewDocument.on( 'selectionChange', () => {
  71. throw 'selectionChange on render';
  72. } );
  73. setTimeout( done, 70 );
  74. const viewBar = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  75. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewBar, 1, viewBar, 2 ) );
  76. viewDocument.render();
  77. } );
  78. it( 'should not fired if observer is disabled', ( done ) => {
  79. viewDocument.getObserver( SelectionObserver ).disable();
  80. viewDocument.on( 'selectionChange', () => {
  81. throw 'selectionChange on render';
  82. } );
  83. setTimeout( done, 70 );
  84. changeDomSelection();
  85. } );
  86. it( 'should not fired if there is no focus', ( done ) => {
  87. viewDocument.isFocused = false;
  88. // changeDomSelection() may focus the editable element (happens on Chrome)
  89. // so cancel this because it sets the isFocused flag.
  90. viewDocument.on( 'focus', ( evt ) => evt.stop(), { priority: 'highest' } );
  91. viewDocument.on( 'selectionChange', () => {
  92. // Validate the correctness of the test. May help tracking issue with this test.
  93. expect( viewDocument.isFocused ).to.be.false;
  94. throw 'selectionChange on render';
  95. } );
  96. setTimeout( done, 70 );
  97. changeDomSelection();
  98. } );
  99. it( 'should warn and not enter infinite loop', ( done ) => {
  100. // Reset infinite loop counters so other tests won't mess up with this test.
  101. selectionObserver._clearInfiniteLoop();
  102. clearInterval( selectionObserver._clearInfiniteLoopInterval );
  103. selectionObserver._clearInfiniteLoopInterval = setInterval( () => selectionObserver._clearInfiniteLoop(), 2000 );
  104. let counter = 100;
  105. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  106. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  107. viewDocument.on( 'selectionChange', () => {
  108. counter--;
  109. if ( counter > 0 ) {
  110. setTimeout( changeDomSelection );
  111. } else {
  112. throw 'Infinite loop!';
  113. }
  114. } );
  115. let warnedOnce = false;
  116. testUtils.sinon.stub( log, 'warn', ( msg ) => {
  117. if ( !warnedOnce ) {
  118. warnedOnce = true;
  119. setTimeout( () => {
  120. expect( msg ).to.match( /^selectionchange-infinite-loop/ );
  121. done();
  122. }, 200 );
  123. }
  124. } );
  125. changeDomSelection();
  126. } );
  127. it( 'should not be treated as an infinite loop if selection is changed only few times', ( done ) => {
  128. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  129. // Reset infinite loop counters so other tests won't mess up with this test.
  130. selectionObserver._clearInfiniteLoop();
  131. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  132. const spy = testUtils.sinon.spy( log, 'warn' );
  133. for ( let i = 0; i < 10; i++ ) {
  134. changeDomSelection();
  135. }
  136. setTimeout( () => {
  137. expect( spy.called ).to.be.false;
  138. done();
  139. }, 400 );
  140. } );
  141. it( 'should not be treated as an infinite loop if changes are not often', ( done ) => {
  142. const clock = testUtils.sinon.useFakeTimers( 'setInterval', 'clearInterval' );
  143. const spy = testUtils.sinon.spy( log, 'warn' );
  144. // We need to recreate SelectionObserver, so it will use mocked setInterval.
  145. selectionObserver.disable();
  146. selectionObserver.destroy();
  147. viewDocument._observers.delete( SelectionObserver );
  148. viewDocument.addObserver( SelectionObserver );
  149. // Inf-loop kicks in after 50th time the selection is changed in 2s.
  150. // We will test 30 times, tick sinon clock to clean counter and then test 30 times again.
  151. // Note that `changeDomSelection` fires two events.
  152. let changeCount = 15;
  153. for ( let i = 0; i < changeCount; i++ ) {
  154. setTimeout( () => {
  155. changeDomSelection();
  156. }, i * 20 );
  157. }
  158. setTimeout( () => {
  159. // Move the clock by 2100ms which will trigger callback added to `setInterval` and reset the inf-loop counter.
  160. clock.tick( 2100 );
  161. for ( let i = 0; i < changeCount; i++ ) {
  162. changeDomSelection();
  163. }
  164. setTimeout( () => {
  165. expect( spy.called ).to.be.false;
  166. clock.restore();
  167. done();
  168. }, 200 );
  169. }, 400 );
  170. } );
  171. it( 'should fire `selectionChangeDone` event after selection stop changing', ( done ) => {
  172. const spy = sinon.spy();
  173. viewDocument.on( 'selectionChangeDone', spy );
  174. // Disable focus observer to not re-render view on each focus.
  175. viewDocument.getObserver( FocusObserver ).disable();
  176. // Change selection.
  177. changeDomSelection();
  178. // Wait 100ms.
  179. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  180. // See: https://github.com/lodash/lodash/issues/304
  181. setTimeout( () => {
  182. // Check if spy was called.
  183. expect( spy.notCalled ).to.true;
  184. // Change selection one more time.
  185. changeDomSelection();
  186. // Wait 210ms (debounced function should be called).
  187. setTimeout( () => {
  188. const data = spy.firstCall.args[ 1 ];
  189. expect( spy.calledOnce ).to.true;
  190. expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
  191. expect( data ).to.have.property( 'oldSelection' ).to.instanceof( ViewSelection );
  192. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  193. expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
  194. expect( data.newSelection.rangeCount ).to.equal( 1 );
  195. const newViewRange = data.newSelection.getFirstRange();
  196. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  197. expect( newViewRange.start.parent ).to.equal( viewFoo );
  198. expect( newViewRange.start.offset ).to.equal( 3 );
  199. expect( newViewRange.end.parent ).to.equal( viewFoo );
  200. expect( newViewRange.end.offset ).to.equal( 3 );
  201. done();
  202. }, 210 );
  203. }, 100 );
  204. } );
  205. it( 'should not fire `selectionChangeDone` event when observer will be destroyed', ( done ) => {
  206. const spy = sinon.spy();
  207. viewDocument.on( 'selectionChangeDone', spy );
  208. // Change selection.
  209. changeDomSelection();
  210. // Wait 100ms.
  211. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  212. // See: https://github.com/lodash/lodash/issues/304
  213. setTimeout( () => {
  214. // And destroy observer.
  215. selectionObserver.destroy();
  216. // Wait another 110ms.
  217. setTimeout( () => {
  218. // Check that event won't be called.
  219. expect( spy.notCalled ).to.true;
  220. done();
  221. }, 110 );
  222. }, 100 );
  223. } );
  224. function changeDomSelection() {
  225. const domSelection = domDocument.getSelection();
  226. const domFoo = domMain.childNodes[ 0 ].childNodes[ 0 ];
  227. const offset = domSelection.anchorOffset;
  228. domSelection.removeAllRanges();
  229. domSelection.collapse( domFoo, offset == 2 ? 3 : 2 );
  230. }
  231. } );