8
0

selectionobserver.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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>xxx<ui:span></ui:span></container:p>' +
  29. '<container:p>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</container:p>' ) );
  30. viewDocument.render();
  31. viewDocument.selection.removeAllRanges();
  32. domDocument.getSelection().removeAllRanges();
  33. viewDocument.isFocused = true;
  34. domMain.focus();
  35. selectionObserver.enable();
  36. // Ensure selectionchange will not be fired.
  37. setTimeout( () => done(), 100 );
  38. } );
  39. afterEach( () => {
  40. domRoot.parentElement.removeChild( domRoot );
  41. viewDocument.destroy();
  42. } );
  43. it( 'should fire selectionChange when it is the only change', done => {
  44. viewDocument.on( 'selectionChange', ( evt, data ) => {
  45. expect( data ).to.have.property( 'domSelection' ).that.equals( domDocument.getSelection() );
  46. expect( data ).to.have.property( 'oldSelection' ).that.is.instanceof( ViewSelection );
  47. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  48. expect( data ).to.have.property( 'newSelection' ).that.is.instanceof( ViewSelection );
  49. expect( data.newSelection.rangeCount ).to.equal( 1 );
  50. const newViewRange = data.newSelection.getFirstRange();
  51. const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  52. expect( newViewRange.start.parent ).to.equal( viewFoo );
  53. expect( newViewRange.start.offset ).to.equal( 2 );
  54. expect( newViewRange.end.parent ).to.equal( viewFoo );
  55. expect( newViewRange.end.offset ).to.equal( 2 );
  56. done();
  57. } );
  58. changeDomSelection();
  59. } );
  60. it( 'should add only one listener to one document', done => {
  61. // Add second roots to ensure that listener is added once.
  62. viewDocument.createRoot( domDocument.getElementById( 'additional' ), 'additional' );
  63. viewDocument.on( 'selectionChange', () => {
  64. done();
  65. } );
  66. changeDomSelection();
  67. } );
  68. it( 'should not fire selectionChange on render', done => {
  69. viewDocument.on( 'selectionChange', () => {
  70. throw 'selectionChange on render';
  71. } );
  72. setTimeout( done, 70 );
  73. const viewBar = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  74. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewBar, 1, viewBar, 2 ) );
  75. viewDocument.render();
  76. } );
  77. it( 'should not fired if observer is disabled', done => {
  78. viewDocument.getObserver( SelectionObserver ).disable();
  79. viewDocument.on( 'selectionChange', () => {
  80. throw 'selectionChange on render';
  81. } );
  82. setTimeout( done, 70 );
  83. changeDomSelection();
  84. } );
  85. it( 'should not fired if there is no focus', done => {
  86. viewDocument.isFocused = false;
  87. // changeDomSelection() may focus the editable element (happens on Chrome)
  88. // so cancel this because it sets the isFocused flag.
  89. viewDocument.on( 'focus', evt => evt.stop(), { priority: 'highest' } );
  90. viewDocument.on( 'selectionChange', () => {
  91. // Validate the correctness of the test. May help tracking issue with this test.
  92. expect( viewDocument.isFocused ).to.be.false;
  93. throw 'selectionChange on render';
  94. } );
  95. setTimeout( done, 70 );
  96. changeDomSelection();
  97. } );
  98. it( 'should fired if there is no focus but document is read-only', done => {
  99. const spy = sinon.spy();
  100. viewDocument.isFocused = false;
  101. viewDocument.isReadOnly = true;
  102. // changeDomSelection() may focus the editable element (happens on Chrome)
  103. // so cancel this because it sets the isFocused flag.
  104. viewDocument.on( 'focus', evt => evt.stop(), { priority: 'highest' } );
  105. viewDocument.on( 'selectionChange', spy );
  106. setTimeout( () => {
  107. sinon.assert.called( spy );
  108. done();
  109. }, 70 );
  110. changeDomSelection();
  111. } );
  112. it( 'should warn and not enter infinite loop', () => {
  113. let counter = 70;
  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. function doChanges() {
  161. return new Promise( resolve => {
  162. viewDocument.once( 'selectionChangeDone', () => {
  163. clock.tick( 1100 );
  164. resolve();
  165. } );
  166. for ( let i = 0; i < 50; 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( 1 ).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. it( 'should re-render view if selections are similar if DOM selection is in incorrect place', done => {
  226. const sel = domDocument.getSelection();
  227. const domParagraph = domMain.childNodes[ 0 ];
  228. const domText = domParagraph.childNodes[ 0 ];
  229. const domUI = domParagraph.childNodes[ 1 ];
  230. // Add rendering on selectionChange event to check this feature.
  231. viewDocument.on( 'selectionChange', () => {
  232. // Manually set selection because no handlers are set for selectionChange event in this test.
  233. // Normally this is handled by view -> model -> view selection converters chain.
  234. const viewSel = viewDocument.selection;
  235. const viewAnchor = viewDocument.domConverter.domPositionToView( sel.anchorNode, sel.anchorOffset );
  236. const viewFocus = viewDocument.domConverter.domPositionToView( sel.focusNode, sel.focusOffset );
  237. viewSel.setCollapsedAt( viewAnchor );
  238. viewSel.moveFocusTo( viewFocus );
  239. viewDocument.render();
  240. } );
  241. viewDocument.once( 'selectionChange', () => {
  242. // 2. Selection change has been handled.
  243. selectionObserver.listenTo( domDocument, 'selectionchange', () => {
  244. // 4. Check if view was re-rendered.
  245. expect( viewDocument.render.called ).to.be.true;
  246. done();
  247. }, { priority: 'lowest' } );
  248. // 3. Now, collapse selection in similar position, but in UI element.
  249. // Current and new selection position are similar in view (but not equal!).
  250. // Also add a spy to `viewDocument#render` to see if view will be re-rendered.
  251. sel.collapse( domUI, 0 );
  252. sinon.spy( viewDocument, 'render' );
  253. // Some browsers like Safari won't allow to put selection inside empty ui element.
  254. // In that situation selection should stay in correct place.
  255. if ( sel.anchorNode !== domUI ) {
  256. expect( sel.anchorNode ).to.equal( domText );
  257. expect( sel.anchorOffset ).to.equal( 3 );
  258. expect( sel.isCollapsed ).to.be.true;
  259. done();
  260. }
  261. }, { priority: 'lowest' } );
  262. // 1. Collapse in a text node, before ui element, and wait for async selectionchange to fire selection change handling.
  263. sel.collapse( domText, 3 );
  264. } );
  265. function changeDomSelection() {
  266. const domSelection = domDocument.getSelection();
  267. const domFoo = domMain.childNodes[ 1 ].childNodes[ 0 ];
  268. const offset = domSelection.anchorOffset;
  269. domSelection.collapse( domFoo, offset == 2 ? 3 : 2 );
  270. }
  271. } );