selectionobserver.js 11 KB

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