8
0

selectionobserver.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 View from '../../../src/view/view';
  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 createViewRoot from '../_utils/createroot';
  14. import { parse } from '../../../src/dev-utils/view';
  15. testUtils.createSinonSandbox();
  16. describe( 'SelectionObserver', () => {
  17. let view, viewDocument, viewRoot, selectionObserver, domRoot, domMain, domDocument;
  18. beforeEach( done => {
  19. domDocument = document;
  20. domRoot = domDocument.createElement( 'div' );
  21. domRoot.innerHTML = '<div contenteditable="true"></div><div contenteditable="true" id="additional"></div>';
  22. domMain = domRoot.childNodes[ 0 ];
  23. domDocument.body.appendChild( domRoot );
  24. view = new View();
  25. viewDocument = view.document;
  26. createViewRoot( viewDocument );
  27. view.attachDomRoot( domMain );
  28. selectionObserver = view.getObserver( SelectionObserver );
  29. viewRoot = viewDocument.getRoot();
  30. viewRoot.appendChildren( parse(
  31. '<container:p>xxx<ui:span></ui:span></container:p>' +
  32. '<container:p>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</container:p>' ) );
  33. view.render();
  34. viewDocument.selection._setTo( null );
  35. domDocument.getSelection().removeAllRanges();
  36. viewDocument.isFocused = true;
  37. domMain.focus();
  38. selectionObserver.enable();
  39. // Ensure selectionchange will not be fired.
  40. setTimeout( () => done(), 100 );
  41. } );
  42. afterEach( () => {
  43. domRoot.parentElement.removeChild( domRoot );
  44. view.destroy();
  45. } );
  46. it( 'should fire selectionChange when it is the only change', done => {
  47. viewDocument.on( 'selectionChange', ( evt, data ) => {
  48. expect( data ).to.have.property( 'domSelection' ).that.equals( domDocument.getSelection() );
  49. expect( data ).to.have.property( 'oldSelection' ).that.is.instanceof( ViewSelection );
  50. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  51. expect( data ).to.have.property( 'newSelection' ).that.is.instanceof( ViewSelection );
  52. expect( data.newSelection.rangeCount ).to.equal( 1 );
  53. const newViewRange = data.newSelection.getFirstRange();
  54. const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  55. expect( newViewRange.start.parent ).to.equal( viewFoo );
  56. expect( newViewRange.start.offset ).to.equal( 2 );
  57. expect( newViewRange.end.parent ).to.equal( viewFoo );
  58. expect( newViewRange.end.offset ).to.equal( 2 );
  59. done();
  60. } );
  61. changeDomSelection();
  62. } );
  63. it( 'should add only one listener to one document', done => {
  64. // Add second roots to ensure that listener is added once.
  65. createViewRoot( viewDocument, 'div', 'additional' );
  66. view.attachDomRoot( domDocument.getElementById( 'additional' ), 'additional' );
  67. viewDocument.on( 'selectionChange', () => {
  68. done();
  69. } );
  70. changeDomSelection();
  71. } );
  72. it( 'should not fire selectionChange on render', done => {
  73. viewDocument.on( 'selectionChange', () => {
  74. throw 'selectionChange on render';
  75. } );
  76. setTimeout( done, 70 );
  77. const viewBar = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  78. viewDocument.selection._setTo( ViewRange.createFromParentsAndOffsets( viewBar, 1, viewBar, 2 ) );
  79. view.render();
  80. } );
  81. it( 'should not fired if observer is disabled', done => {
  82. view.getObserver( SelectionObserver ).disable();
  83. viewDocument.on( 'selectionChange', () => {
  84. throw 'selectionChange on render';
  85. } );
  86. setTimeout( done, 70 );
  87. changeDomSelection();
  88. } );
  89. it( 'should not fired if there is no focus', done => {
  90. viewDocument.isFocused = false;
  91. // changeDomSelection() may focus the editable element (happens on Chrome)
  92. // so cancel this because it sets the isFocused flag.
  93. viewDocument.on( 'focus', evt => evt.stop(), { priority: 'highest' } );
  94. viewDocument.on( 'selectionChange', () => {
  95. // Validate the correctness of the test. May help tracking issue with this test.
  96. expect( viewDocument.isFocused ).to.be.false;
  97. throw 'selectionChange on render';
  98. } );
  99. setTimeout( done, 70 );
  100. changeDomSelection();
  101. } );
  102. it( 'should fired if there is no focus but document is read-only', done => {
  103. const spy = sinon.spy();
  104. viewDocument.isFocused = false;
  105. viewDocument.isReadOnly = true;
  106. // changeDomSelection() may focus the editable element (happens on Chrome)
  107. // so cancel this because it sets the isFocused flag.
  108. viewDocument.on( 'focus', evt => evt.stop(), { priority: 'highest' } );
  109. viewDocument.on( 'selectionChange', spy );
  110. setTimeout( () => {
  111. sinon.assert.called( spy );
  112. done();
  113. }, 70 );
  114. changeDomSelection();
  115. } );
  116. it( 'should warn and not enter infinite loop', () => {
  117. let counter = 70;
  118. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  119. viewDocument.selection._setTo( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  120. return new Promise( ( resolve, reject ) => {
  121. testUtils.sinon.stub( log, 'warn' ).callsFake( msg => {
  122. expect( msg ).to.match( /^selectionchange-infinite-loop/ );
  123. resolve();
  124. } );
  125. viewDocument.on( 'selectionChangeDone', () => {
  126. if ( !counter ) {
  127. reject( new Error( 'Infinite loop warning was not logged.' ) );
  128. }
  129. } );
  130. while ( counter > 0 ) {
  131. changeDomSelection();
  132. counter--;
  133. }
  134. } );
  135. } );
  136. it( 'should not be treated as an infinite loop if selection is changed only few times', done => {
  137. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  138. viewDocument.selection._setTo( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  139. const spy = testUtils.sinon.spy( log, 'warn' );
  140. viewDocument.on( 'selectionChangeDone', () => {
  141. expect( spy.called ).to.be.false;
  142. done();
  143. } );
  144. for ( let i = 0; i < 10; i++ ) {
  145. changeDomSelection();
  146. }
  147. } );
  148. it( 'should not be treated as an infinite loop if changes are not often', () => {
  149. const clock = testUtils.sinon.useFakeTimers( {
  150. toFake: [ 'setInterval', 'clearInterval' ]
  151. } );
  152. const stub = testUtils.sinon.stub( log, 'warn' );
  153. // We need to recreate SelectionObserver, so it will use mocked setInterval.
  154. selectionObserver.disable();
  155. selectionObserver.destroy();
  156. view._observers.delete( SelectionObserver );
  157. view.addObserver( SelectionObserver );
  158. return doChanges()
  159. .then( doChanges )
  160. .then( () => {
  161. sinon.assert.notCalled( stub );
  162. clock.restore();
  163. } );
  164. function doChanges() {
  165. return new Promise( resolve => {
  166. viewDocument.once( 'selectionChangeDone', () => {
  167. clock.tick( 1100 );
  168. resolve();
  169. } );
  170. for ( let i = 0; i < 50; i++ ) {
  171. changeDomSelection();
  172. }
  173. } );
  174. }
  175. } );
  176. it( 'should fire `selectionChangeDone` event after selection stop changing', done => {
  177. const spy = sinon.spy();
  178. viewDocument.on( 'selectionChangeDone', spy );
  179. // Disable focus observer to not re-render view on each focus.
  180. view.getObserver( FocusObserver ).disable();
  181. // Change selection.
  182. changeDomSelection();
  183. // Wait 100ms.
  184. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  185. // See: https://github.com/lodash/lodash/issues/304
  186. setTimeout( () => {
  187. // Check if spy was called.
  188. expect( spy.notCalled ).to.true;
  189. // Change selection one more time.
  190. changeDomSelection();
  191. // Wait 210ms (debounced function should be called).
  192. setTimeout( () => {
  193. const data = spy.firstCall.args[ 1 ];
  194. expect( spy.calledOnce ).to.true;
  195. expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
  196. expect( data ).to.have.property( 'oldSelection' ).to.instanceof( ViewSelection );
  197. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  198. expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
  199. expect( data.newSelection.rangeCount ).to.equal( 1 );
  200. const newViewRange = data.newSelection.getFirstRange();
  201. const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  202. expect( newViewRange.start.parent ).to.equal( viewFoo );
  203. expect( newViewRange.start.offset ).to.equal( 3 );
  204. expect( newViewRange.end.parent ).to.equal( viewFoo );
  205. expect( newViewRange.end.offset ).to.equal( 3 );
  206. done();
  207. }, 210 );
  208. }, 100 );
  209. } );
  210. it( 'should not fire `selectionChangeDone` event when observer will be destroyed', done => {
  211. const spy = sinon.spy();
  212. viewDocument.on( 'selectionChangeDone', spy );
  213. // Change selection.
  214. changeDomSelection();
  215. // Wait 100ms.
  216. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  217. // See: https://github.com/lodash/lodash/issues/304
  218. setTimeout( () => {
  219. // And destroy observer.
  220. selectionObserver.destroy();
  221. // Wait another 110ms.
  222. setTimeout( () => {
  223. // Check that event won't be called.
  224. expect( spy.notCalled ).to.true;
  225. done();
  226. }, 110 );
  227. }, 100 );
  228. } );
  229. it( 'should re-render view if selections are similar if DOM selection is in incorrect place', done => {
  230. const sel = domDocument.getSelection();
  231. const domParagraph = domMain.childNodes[ 0 ];
  232. const domText = domParagraph.childNodes[ 0 ];
  233. const domUI = domParagraph.childNodes[ 1 ];
  234. // Add rendering on selectionChange event to check this feature.
  235. viewDocument.on( 'selectionChange', () => {
  236. // Manually set selection because no handlers are set for selectionChange event in this test.
  237. // Normally this is handled by view -> model -> view selection converters chain.
  238. const viewSel = viewDocument.selection;
  239. const viewAnchor = view.domConverter.domPositionToView( sel.anchorNode, sel.anchorOffset );
  240. const viewFocus = view.domConverter.domPositionToView( sel.focusNode, sel.focusOffset );
  241. viewSel._setTo( viewAnchor );
  242. viewSel._setFocus( viewFocus );
  243. view.render();
  244. } );
  245. viewDocument.once( 'selectionChange', () => {
  246. // 2. Selection change has been handled.
  247. selectionObserver.listenTo( domDocument, 'selectionchange', () => {
  248. // 4. Check if view was re-rendered.
  249. expect( view.render.called ).to.be.true;
  250. done();
  251. }, { priority: 'lowest' } );
  252. // 3. Now, collapse selection in similar position, but in UI element.
  253. // Current and new selection position are similar in view (but not equal!).
  254. // Also add a spy to `viewDocument#render` to see if view will be re-rendered.
  255. sel.collapse( domUI, 0 );
  256. sinon.spy( view, 'render' );
  257. // Some browsers like Safari won't allow to put selection inside empty ui element.
  258. // In that situation selection should stay in correct place.
  259. if ( sel.anchorNode !== domUI ) {
  260. expect( sel.anchorNode ).to.equal( domText );
  261. expect( sel.anchorOffset ).to.equal( 3 );
  262. expect( sel.isCollapsed ).to.be.true;
  263. done();
  264. }
  265. }, { priority: 'lowest' } );
  266. // 1. Collapse in a text node, before ui element, and wait for async selectionchange to fire selection change handling.
  267. sel.collapse( domText, 3 );
  268. } );
  269. function changeDomSelection() {
  270. const domSelection = domDocument.getSelection();
  271. const domFoo = domMain.childNodes[ 1 ].childNodes[ 0 ];
  272. const offset = domSelection.anchorOffset;
  273. domSelection.collapse( domFoo, offset == 2 ? 3 : 2 );
  274. }
  275. } );