selectionobserver.js 8.9 KB

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