selectionobserver.js 9.2 KB

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