selectionobserver.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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' ).callsFake( 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( {
  146. toFake: [ 'setInterval', 'clearInterval' ]
  147. } );
  148. const stub = testUtils.sinon.stub( log, 'warn' );
  149. // We need to recreate SelectionObserver, so it will use mocked setInterval.
  150. selectionObserver.disable();
  151. selectionObserver.destroy();
  152. viewDocument._observers.delete( SelectionObserver );
  153. viewDocument.addObserver( SelectionObserver );
  154. return doChanges()
  155. .then( doChanges )
  156. .then( () => {
  157. sinon.assert.notCalled( stub );
  158. clock.restore();
  159. } );
  160. // Selectionchange event is called twice per `changeDomSelection()` execution. We call it 25 times to get
  161. // 50 events. Infinite loop counter is reset, so calling this method twice should not show any warning.
  162. function doChanges() {
  163. return new Promise( resolve => {
  164. viewDocument.once( 'selectionChangeDone', () => {
  165. clock.tick( 1100 );
  166. resolve();
  167. } );
  168. for ( let i = 0; i < 30; i++ ) {
  169. changeDomSelection();
  170. }
  171. } );
  172. }
  173. } );
  174. it( 'should fire `selectionChangeDone` event after selection stop changing', done => {
  175. const spy = sinon.spy();
  176. viewDocument.on( 'selectionChangeDone', spy );
  177. // Disable focus observer to not re-render view on each focus.
  178. viewDocument.getObserver( FocusObserver ).disable();
  179. // Change selection.
  180. changeDomSelection();
  181. // Wait 100ms.
  182. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  183. // See: https://github.com/lodash/lodash/issues/304
  184. setTimeout( () => {
  185. // Check if spy was called.
  186. expect( spy.notCalled ).to.true;
  187. // Change selection one more time.
  188. changeDomSelection();
  189. // Wait 210ms (debounced function should be called).
  190. setTimeout( () => {
  191. const data = spy.firstCall.args[ 1 ];
  192. expect( spy.calledOnce ).to.true;
  193. expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
  194. expect( data ).to.have.property( 'oldSelection' ).to.instanceof( ViewSelection );
  195. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  196. expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
  197. expect( data.newSelection.rangeCount ).to.equal( 1 );
  198. const newViewRange = data.newSelection.getFirstRange();
  199. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  200. expect( newViewRange.start.parent ).to.equal( viewFoo );
  201. expect( newViewRange.start.offset ).to.equal( 3 );
  202. expect( newViewRange.end.parent ).to.equal( viewFoo );
  203. expect( newViewRange.end.offset ).to.equal( 3 );
  204. done();
  205. }, 210 );
  206. }, 100 );
  207. } );
  208. it( 'should not fire `selectionChangeDone` event when observer will be destroyed', done => {
  209. const spy = sinon.spy();
  210. viewDocument.on( 'selectionChangeDone', spy );
  211. // Change selection.
  212. changeDomSelection();
  213. // Wait 100ms.
  214. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  215. // See: https://github.com/lodash/lodash/issues/304
  216. setTimeout( () => {
  217. // And destroy observer.
  218. selectionObserver.destroy();
  219. // Wait another 110ms.
  220. setTimeout( () => {
  221. // Check that event won't be called.
  222. expect( spy.notCalled ).to.true;
  223. done();
  224. }, 110 );
  225. }, 100 );
  226. } );
  227. function changeDomSelection() {
  228. const domSelection = domDocument.getSelection();
  229. const domFoo = domMain.childNodes[ 0 ].childNodes[ 0 ];
  230. const offset = domSelection.anchorOffset;
  231. domSelection.removeAllRanges();
  232. domSelection.collapse( domFoo, offset == 2 ? 3 : 2 );
  233. }
  234. } );