selectionobserver.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 fired if there is no focus but document is read-only', done => {
  98. const spy = sinon.spy();
  99. viewDocument.isFocused = false;
  100. viewDocument.isReadOnly = true;
  101. // changeDomSelection() may focus the editable element (happens on Chrome)
  102. // so cancel this because it sets the isFocused flag.
  103. viewDocument.on( 'focus', evt => evt.stop(), { priority: 'highest' } );
  104. viewDocument.on( 'selectionChange', spy );
  105. setTimeout( () => {
  106. sinon.assert.calledOnce( spy );
  107. done();
  108. }, 70 );
  109. changeDomSelection();
  110. } );
  111. it( 'should warn and not enter infinite loop', () => {
  112. // Selectionchange event is called twice per `changeDomSelection()` execution.
  113. let counter = 35;
  114. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  115. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  116. return new Promise( ( resolve, reject ) => {
  117. testUtils.sinon.stub( log, 'warn', msg => {
  118. expect( msg ).to.match( /^selectionchange-infinite-loop/ );
  119. resolve();
  120. } );
  121. viewDocument.on( 'selectionChangeDone', () => {
  122. if ( !counter ) {
  123. reject( new Error( 'Infinite loop warning was not logged.' ) );
  124. }
  125. } );
  126. while ( counter > 0 ) {
  127. changeDomSelection();
  128. counter--;
  129. }
  130. } );
  131. } );
  132. it( 'should not be treated as an infinite loop if selection is changed only few times', done => {
  133. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  134. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  135. const spy = testUtils.sinon.spy( log, 'warn' );
  136. viewDocument.on( 'selectionChangeDone', () => {
  137. expect( spy.called ).to.be.false;
  138. done();
  139. } );
  140. for ( let i = 0; i < 10; i++ ) {
  141. changeDomSelection();
  142. }
  143. } );
  144. it( 'should not be treated as an infinite loop if changes are not often', () => {
  145. const clock = testUtils.sinon.useFakeTimers( 'setInterval', 'clearInterval' );
  146. const stub = testUtils.sinon.stub( log, 'warn' );
  147. // We need to recreate SelectionObserver, so it will use mocked setInterval.
  148. selectionObserver.disable();
  149. selectionObserver.destroy();
  150. viewDocument._observers.delete( SelectionObserver );
  151. viewDocument.addObserver( SelectionObserver );
  152. return doChanges()
  153. .then( doChanges )
  154. .then( () => {
  155. sinon.assert.notCalled( stub );
  156. clock.restore();
  157. } );
  158. // Selectionchange event is called twice per `changeDomSelection()` execution. We call it 25 times to get
  159. // 50 events. Infinite loop counter is reset, so calling this method twice should not show any warning.
  160. function doChanges() {
  161. return new Promise( resolve => {
  162. viewDocument.once( 'selectionChangeDone', () => {
  163. clock.tick( 1100 );
  164. resolve();
  165. } );
  166. for ( let i = 0; i < 30; i++ ) {
  167. changeDomSelection();
  168. }
  169. } );
  170. }
  171. } );
  172. it( 'should fire `selectionChangeDone` event after selection stop changing', done => {
  173. const spy = sinon.spy();
  174. viewDocument.on( 'selectionChangeDone', spy );
  175. // Disable focus observer to not re-render view on each focus.
  176. viewDocument.getObserver( FocusObserver ).disable();
  177. // Change selection.
  178. changeDomSelection();
  179. // Wait 100ms.
  180. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  181. // See: https://github.com/lodash/lodash/issues/304
  182. setTimeout( () => {
  183. // Check if spy was called.
  184. expect( spy.notCalled ).to.true;
  185. // Change selection one more time.
  186. changeDomSelection();
  187. // Wait 210ms (debounced function should be called).
  188. setTimeout( () => {
  189. const data = spy.firstCall.args[ 1 ];
  190. expect( spy.calledOnce ).to.true;
  191. expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
  192. expect( data ).to.have.property( 'oldSelection' ).to.instanceof( ViewSelection );
  193. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  194. expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
  195. expect( data.newSelection.rangeCount ).to.equal( 1 );
  196. const newViewRange = data.newSelection.getFirstRange();
  197. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  198. expect( newViewRange.start.parent ).to.equal( viewFoo );
  199. expect( newViewRange.start.offset ).to.equal( 3 );
  200. expect( newViewRange.end.parent ).to.equal( viewFoo );
  201. expect( newViewRange.end.offset ).to.equal( 3 );
  202. done();
  203. }, 210 );
  204. }, 100 );
  205. } );
  206. it( 'should not fire `selectionChangeDone` event when observer will be destroyed', done => {
  207. const spy = sinon.spy();
  208. viewDocument.on( 'selectionChangeDone', spy );
  209. // Change selection.
  210. changeDomSelection();
  211. // Wait 100ms.
  212. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  213. // See: https://github.com/lodash/lodash/issues/304
  214. setTimeout( () => {
  215. // And destroy observer.
  216. selectionObserver.destroy();
  217. // Wait another 110ms.
  218. setTimeout( () => {
  219. // Check that event won't be called.
  220. expect( spy.notCalled ).to.true;
  221. done();
  222. }, 110 );
  223. }, 100 );
  224. } );
  225. function changeDomSelection() {
  226. const domSelection = domDocument.getSelection();
  227. const domFoo = domMain.childNodes[ 0 ].childNodes[ 0 ];
  228. const offset = domSelection.anchorOffset;
  229. domSelection.removeAllRanges();
  230. domSelection.collapse( domFoo, offset == 2 ? 3 : 2 );
  231. }
  232. } );